def site_search(request): query = request.GET.get("query") state = request.GET.get("state") bills = [] people = [] if query: bills = search_bills(state=state, query=query) bills = bills.order_by("-billstatus__latest_action_date") # pagination page_num = int(request.GET.get("page", 1)) bills_paginator = Paginator(bills, 20) try: bills = bills_paginator.page(page_num) except EmptyPage: raise Http404() # people search people = [ p.as_dict() for p in PersonProxy.search_people(query, state=state) ] return render( request, "public/views/search.html", { "query": query, "state": state, "bills": bills, "people": people }, )
def get_bills(self, request, state): # query parameter filtering query = request.GET.get("query", "") chamber = request.GET.get("chamber") session = request.GET.get("session") sponsor = request.GET.get("sponsor") classification = request.GET.get("classification") q_subjects = request.GET.getlist("subjects") status = request.GET.getlist("status") sort = request.GET.get("sort", "-latest_action") form = { "query": query, "chamber": chamber, "session": session, "sponsor": sponsor, "classification": classification, "subjects": q_subjects, "status": status, } bills = search_bills( state=state, query=query, chamber=chamber, session=session, sponsor=sponsor, classification=classification, exclude_classifications=EXCLUDED_CLASSIFICATIONS, subjects=q_subjects, status=status, sort=sort, ) return bills, form
def site_search(request): query = request.GET.get("query") state = request.GET.get("state") bills = [] people = [] if query: bills = search_bills(state=state, query=query, sort="-latest_action") # pagination page_num = int(request.GET.get("page", 1)) bills_paginator = Paginator(bills, 20) try: bills = bills_paginator.page(page_num) except EmptyPage: raise Http404() # people search people = [] for p in Person.objects.search(query, state=state): pd = person_as_dict(p) pd["current_state"] = jid_to_abbr( p.current_jurisdiction_id).upper() people.append(pd) return render( request, "public/views/search.html", { "query": query, "state": state, "bills": bills, "people": people }, )
def get_bills(self, request, state): # query parameter filtering query = request.GET.get("query", "") chamber = request.GET.get("chamber") session = request.GET.get("session") sponsor = request.GET.get("sponsor") classification = request.GET.get("classification") q_subjects = request.GET.getlist("subjects") status = request.GET.getlist("status") form = { "query": query, "chamber": chamber, "session": session, "sponsor": sponsor, "classification": classification, "subjects": q_subjects, "status": status, } bills = search_bills( state=state, query=query, chamber=chamber, session=session, sponsor=sponsor, classification=classification, subjects=q_subjects, status=status, ) bills = bills.order_by("-billstatus__latest_action_date") return bills, form
def handle(self, *args, **options): bundle, created = Bundle.objects.get_or_create( slug=options["slug"], defaults=dict(name=options["name"])) bills = list(search_bills(query=options["search"], sort=None)) for b in bills: bundle.bills.add(b) if created: print(f"created bundle {options['slug']} with {len(bills)} bills") else: print(f"updated bundle {options['slug']} with {len(bills)} bills")
def process_query_sub(sub, since): """ given a query subscription, return a list of bills created since then """ bills = list( search_bills( query=sub.query, state=sub.state, chamber=sub.chamber, session=sub.session, sponsor=sub.sponsor, classification=sub.classification, status=sub.status, ).filter(created_at__gte=since).order_by("-latest_action_date")) return bills
def bill_list(request): state = request.GET.get("state") chamber = request.GET.get("chamber") bill_id = request.GET.get("bill_id") query = request.GET.get("q") search_window = request.GET.get("search_window", "all") updated_since = request.GET.get("updated_since") sort = request.GET.get("sort", "last") too_big = True bills = bill_qs(include_votes=False) if state in ("ne", "dc") and chamber == "upper": chamber = "legislature" bills = search_bills(bills=bills, state=state, chamber=chamber, query=query) if query: too_big = False if updated_since: updated_since = pytz.utc.localize( datetime.datetime.strptime(updated_since[:10], "%Y-%m-%d")) bills = bills.filter(updated_at__gt=updated_since) too_big = False if bill_id: bills = bills.filter(identifier=bill_id) too_big = False # search_window only ever really worked w/ state- and judging by analytics that's how # it was used in every case if state: if search_window == "session" or search_window == "term": latest_session = (LegislativeSession.objects.filter( jurisdiction_id=abbr_to_jid(state.lower())).order_by( "-start_date").values_list("identifier", flat=True)[0]) bills = bills.filter( legislative_session__identifier=latest_session) too_big = False elif search_window.startswith("session:"): bills = bills.filter(legislative_session__identifier=search_window. split("session:")[1]) too_big = False elif search_window != "all": return JsonResponse( 'invalid search_window. valid choices are "term", "session", "all"', status=400, safe=False, ) # first, last, created if sort == "created_at": bills = bills.order_by("-created_at") elif sort == "updated_at": bills = bills.order_by("-updated_at") else: bills = bills.order_by("-last_action") # pagination page = request.GET.get("page") per_page = request.GET.get("per_page") if page and not per_page: per_page = 50 if per_page and not page: page = 1 if page and int(page) > 0: page = int(page) per_page = int(per_page) start = per_page * (page - 1) end = start + per_page bills = bills[start:end] too_big = per_page > 100 elif too_big or bills.count() > 200: return JsonResponse( "Bad Request: request too large, try narrowing your search by " "adding more filters or using pagination.", status=400, safe=False, ) return JsonResponse([convert_bill(b, include_votes=False) for b in bills], safe=False)
def resolve_bills( self, info, before=None, after=None, first=None, last=None, jurisdiction=None, chamber=None, session=None, updated_since=None, classification=None, subject=None, sponsor=None, action_since=None, search_query=None, ): # q (full text) bills = Bill.objects.all() if jurisdiction: bills = bills.filter(**jurisdiction_query(jurisdiction)) subjects = [subject] if subject else [] bills = search_bills( bills=bills, query=search_query, chamber=chamber, session=session, classification=classification, subjects=subjects, sort="-updated", ) if updated_since: bills = bills.filter(updated_at__gte=updated_since) if action_since: bills = bills.filter(latest_action_date__gte=action_since) if sponsor: sponsor_args = {} if "primary" in sponsor: sponsor_args["sponsorships__primary"] = sponsor["primary"] if sponsor.get("person"): sponsor_args["sponsorships__person_id"] = sponsor["person"] elif sponsor.get("name"): sponsor_args["sponsorships__name"] = sponsor["name"] bills = bills.filter(**sponsor_args) bills = optimize( bills, info, [ ".abstracts", ".otherTitles", ".otherIdentifiers", ".actions", ".actions.organization", ".actions.relatedEntities", ".actions.relatedEntities.organization", ".actions.relatedEntities.person", ".sponsorships", ".documents", ".versions", ".documents.links", ".versions.links", ".sources", ".relatedBills", ".votes", ".votes.counts", ( ".votes.votes", Prefetch("votes__votes", PersonVote.objects.all().select_related("voter")), ), ], [".legislativeSession" ".legislativeSession.jurisdiction"], ) return bills