コード例 #1
0
def legislator_list(request, geo=False):
    abbr = request.GET.get("state")
    chamber = request.GET.get("chamber")
    district = request.GET.get("district")

    today = datetime.date.today().isoformat()
    filter_params = [
        Q(memberships__start_date="") | Q(memberships__start_date__lte=today),
        Q(memberships__end_date="") | Q(memberships__end_date__gte=today),
    ]

    if geo:
        latitude = request.GET.get("lat")
        longitude = request.GET.get("long")

        if not latitude or not longitude:
            return JsonResponse(
                "Bad Request: must include lat & long", status=400, safe=False
            )
        try:
            latitude = float(latitude)
            longitude = float(longitude)
        except ValueError:
            return JsonResponse("Bad Request: invalid lat, lon", status=400, safe=False)

        divisions = coords_to_divisions(latitude, longitude)
        filter_params.append(Q(memberships__post__division__id__in=divisions))

    if abbr:
        jid = abbr_to_jid(abbr)
        filter_params.append(Q(memberships__organization__jurisdiction_id=jid))
    if chamber:
        filter_params.append(Q(memberships__organization__classification=chamber))
    if district:
        filter_params.append(Q(memberships__post__label=district))

    people = person_qs().filter(*filter_params).distinct()

    return JsonResponse([convert_legislator(leg) for leg in people], safe=False)
コード例 #2
0
    def resolve_people(
        self,
        info,
        first=None,
        last=None,
        before=None,
        after=None,
        member_of=None,
        ever_member_of=None,
        district=None,
        division_id=None,
        name=None,
        updated_since=None,
        latitude=None,
        longitude=None,
    ):
        qs = Person.objects.all()
        today = datetime.date.today()

        if name:
            qs = qs.filter(
                Q(name__icontains=name) | Q(other_names__name__icontains=name))
        if division_id:
            qs = qs.filter(
                Q(memberships__post__division_id=division_id),
                Q(memberships__end_date="")
                | Q(memberships__end_date__gt=today),
            )
        if member_of:
            qs = qs.member_of(member_of, post=district)
        if ever_member_of:
            qs = qs.member_of(ever_member_of,
                              current_only=False,
                              post=district)
        if updated_since:
            qs = qs.filter(updated_at__gte=updated_since)
        if district and not (member_of or ever_member_of):
            raise ValueError("'district' parameter requires specifying either "
                             "'memberOf' or 'everMemberOf'")

        if latitude and longitude:
            try:
                latitude = float(latitude)
                longitude = float(longitude)
            except ValueError:
                raise ValueError("invalid lat or lon")

            divisions = coords_to_divisions(latitude, longitude)
            qs = qs.filter(
                Q(memberships__post__division__id__in=divisions),
                Q(memberships__end_date="")
                | Q(memberships__end_date__gt=today),
            )

        elif latitude or longitude:
            raise ValueError("must provide lat & lon together")

        qs = optimize(
            qs,
            info,
            [
                ".identifiers",
                ".otherNames",
                ".links",
                ".sources",
                ".contactDetails",
                (
                    ".currentMemberships",
                    Prefetch(
                        "memberships",
                        queryset=_membership_filter(
                            Membership.objects,
                            info,
                            prefix=".currentMemberships",
                            current=True,
                        ),
                        to_attr="current_memberships",
                    ),
                ),
                (
                    ".oldMemberships",
                    Prefetch(
                        "memberships",
                        queryset=_membership_filter(
                            Membership.objects,
                            info,
                            prefix=".oldMemberships",
                            current=False,
                        ),
                        to_attr="old_memberships",
                    ),
                ),
            ],
        )

        return qs