def create_dqr(state, session): chambers = get_chambers_from_abbr(state) for chamber in chambers: print(f"creating report for {chamber} in {state} {session}") if bill_qs(state, session, chamber).count() > 0: bills_per_session_data = total_bills_per_session( state, session, chamber) average_num_data = average_number_data(state, session, chamber) bill_version_data = bills_versions(state, session, chamber) no_sources_data = no_sources(state, session, chamber) bill_subjects_data = bill_subjects(state, session, chamber) bill_vote_data = vote_data(state, session, chamber) # update or save the report leg_session = LegislativeSession.objects.get( identifier=session, jurisdiction_id=abbr_to_jid(state)) DataQualityReport.objects.update_or_create( session=leg_session, chamber=chamber.classification, defaults={ **bills_per_session_data, **average_num_data, **bill_version_data, **no_sources_data, **bill_vote_data, **bill_subjects_data, }, )
def get_filter_options(self, state): options = {} jid = abbr_to_jid(state) bills = Bill.objects.all().filter( legislative_session__jurisdiction_id=jid) chambers = get_chambers_from_abbr(state) options["chambers"] = {c.classification: c.name for c in chambers} options["sessions"] = { s.identifier: s.name for s in sessions_with_bills(jid) } options["sponsors"] = { p.id: p.name for p in Person.objects.filter( memberships__organization__jurisdiction_id=jid).order_by( "name").distinct() } options["classifications"] = sorted( bills.annotate( type=Unnest("classification", distinct=True)).values_list( "type", flat=True).distinct()) options["subjects"] = sorted( bills.annotate(sub=Unnest("subject", distinct=True)).values_list( "sub", flat=True).distinct()) return options
def create_dqr(state, session): bills = load_bills(state, session) chambers = get_chambers_from_abbr(state) for chamber in chambers: if bills.filter(from_organization=chamber).count() > 0: bills_per_session_data = total_bills_per_session(bills, chamber) average_num_data = average_number_data(bills, chamber) bill_version_data = bills_versions(bills, chamber) no_sources_data = no_sources(bills, chamber) bill_subjects_data = bill_subjects(bills, chamber) bill_vote_data = vote_data(bills, chamber) # Grabbing the Legislative Session object leg_session = LegislativeSession.objects.get( identifier=session, jurisdiction_id=abbr_to_jid(state)) DataQualityReport.objects.update_or_create( session=leg_session, chamber=chamber.classification, defaults={ **bills_per_session_data, **average_num_data, **bill_version_data, **no_sources_data, **bill_vote_data, **bill_subjects_data, }, )
def dq_overview_session(request, state, session): jid = abbr_to_jid(state) all_sessions = sessions_with_bills(jid) session = LegislativeSession.objects.get(identifier=session, jurisdiction_id=jid) dashboards = DataQualityReport.objects.filter(session=session) chambers = get_chambers_from_abbr(state) context = { "state": state, "chambers": chambers, "session": session, "all_sessions": all_sessions, "dashboards": dashboards, } return render(request, "dashboards/dqr_page.html", context)
def dq_overview(request, state): jid = abbr_to_jid(state) all_sessions = sessions_with_bills(jid) dashboards = [] session = "Dashboards Not Generated Yet" if all_sessions: session = all_sessions[0] dashboards = DataQualityReport.objects.filter(session=session) chambers = get_chambers_from_abbr(state) context = { "state": state, "chambers": chambers, "session": session, "all_sessions": all_sessions, "dashboards": dashboards, } return render(request, "dashboards/dqr_page.html", context)
def legislators(request, state): chambers = get_chambers_from_abbr(state) legislators = [ person_as_dict(p) for p in Person.objects.current_legislators_with_roles(chambers) ] chambers = {c.classification: c.name for c in chambers} return render( request, "public/views/legislators.html", { "state": state, "chambers": chambers, "legislators": legislators, "state_nav": "legislators", }, )
def committees(request, state): chambers = get_chambers_from_abbr(state) committees = [ org_as_dict(c) for c in Organization.objects.select_related("parent").filter( Q(parent__in=chambers), classification="committee").annotate( member_count=Count("memberships", filter=Q(memberships__end_date=""))) ] chambers = {c.classification: c.name for c in chambers} return render( request, "public/views/committees.html", { "state": state, "state_nav": "committees", "chambers": chambers, "committees": committees, }, )
def committees(request, state): chambers = get_chambers_from_abbr(state) # disabling committee data so all com. pages ask for help, let's see what happens committees = None # [ # org_as_dict(c) # for c in Organization.objects.select_related("parent") # .filter(Q(parent__in=chambers), classification="committee") # .annotate(member_count=Count("memberships", filter=Q(memberships__end_date=""))) # ] chambers = {c.classification: c.name for c in chambers} return render( request, "public/views/committees.html", { "state": state, "state_nav": "committees", "chambers": chambers, "committees": committees, }, )
def bill(request, state, session, bill_id): # canonicalize without space if " " in bill_id: return redirect("bill", state, session, bill_id.replace(" ", ""), permanent=True) jid = abbr_to_jid(state) identifier = fix_bill_id(bill_id) try: bill = Bill.objects.select_related( "legislative_session", "legislative_session__jurisdiction", "from_organization", ).get( legislative_session__jurisdiction_id=jid, legislative_session__identifier=session, identifier=identifier, ) except Bill.DoesNotExist: # try to find the asset in S3 request.path = request.path.replace(bill_id, identifier) return fallback(request) # sponsorships, attach people manually sponsorships = list(bill.sponsorships.all()) sponsor_people = { p.id: p for p in Person.objects.filter( id__in=[s.person_id for s in sponsorships if s.person_id]).prefetch_related( "memberships", "memberships__organization", "memberships__post") } for s in sponsorships: s.person = sponsor_people.get(s.person_id) related_entities = Prefetch( "related_entities", BillActionRelatedEntity.objects.all().select_related( "person", "organization"), ) actions = list(bill.actions.all().select_related( "organization").prefetch_related(related_entities).order_by("-date")) votes = list(bill.votes.all().select_related( "organization")) # .prefetch_related('counts') # stage calculation # get other chamber name chambers = { c.classification: c.name for c in get_chambers_from_abbr(state) } second_chamber = None if len(chambers ) > 1 and bill.from_organization.classification != "legislature": second_chamber = { "upper": chambers["lower"], "lower": chambers["upper"] }[bill.from_organization.classification] stages = compute_bill_stages(actions, bill.from_organization.name, second_chamber) versions = list(bill.versions.order_by("-date").prefetch_related("links")) documents = list( bill.documents.order_by("-date").prefetch_related("links")) try: sorted_links = sorted(versions[0].links.all(), key=_document_sort_key) read_link = sorted_links[0].url except IndexError: read_link = None return render( request, "public/views/bill.html", { "state": state, "state_nav": "bills", "unicameral": state in ("dc", "ne"), "bill": bill, "sponsorships": sponsorships, "actions": actions, "stages": stages, "votes": votes, "versions": versions, "documents": documents, "read_link": read_link, }, )
def test_get_chambers(): populate_db() chambers = get_chambers_from_abbr("ak") assert len(chambers) == 2 assert {"upper", "lower"} == {c.classification for c in chambers}