def yield_state_sessions(state: str, session: Optional[str]): """ parse provided options to get a list of sessions to process """ if state == "all" or state == "all_sessions": scrape_state = state for state in states: sessions = sorted( s.identifier for s in sessions_with_bills(abbr_to_jid(state.abbr)) ) if len(sessions) > 0: state = state.abbr.lower() if scrape_state == "all_sessions": for session in sessions: yield state, session else: session = sessions[-1] yield state, session elif session: # state and session, yield once yield state, session else: # single state sessions = sorted(s.identifier for s in sessions_with_bills(abbr_to_jid(state))) for session in sessions: yield state, session
def dqr_listing(request): state_dqr_data = {} for state in states: try: session = sessions_with_bills(abbr_to_jid(state.abbr))[0] except KeyError: continue dashboards = list( DataQualityReport.objects.filter( session=session).order_by("chamber")) session_name = session.name # if there are two, lower is first (b/c of ordering above), otherwise figure it out if len(dashboards) == 2: lower_dashboard, upper_dashboard = dashboards elif len(dashboards) == 1: if dashboards[0].chamber == "lower": lower_dashboard = dashboards[0] upper_dashboard = None else: upper_dashboard = dashboards[0] lower_dashboard = None state_dqr_data[state.abbr.lower()] = { "state": state.name, "session_name": session_name, "lower_dashboard": lower_dashboard, "upper_dashboard": upper_dashboard, } return render(request, "dashboards/dqr_listing.html", {"state_dqr_data": state_dqr_data})
def dqr_listing(request): state_dqr_data = {} for state in states: session = sessions_with_bills(abbr_to_jid(state.abbr)) abbr = state.abbr.lower() lower_dashboard = [] upper_dashboard = [] session_name = "" if len(session) > 0: dashboards = DataQualityReport.objects.filter(session=session[0]) if dashboards.count() > 0: session_name = session[0].name # Nebraska only has one legislature if abbr == "ne" or abbr == "dc": lower_dashboard = dashboards.filter( session=session[0], chamber="legislature")[0] else: lower_dashboard = dashboards.filter(session=session[0], chamber="lower")[0] if dashboards.filter(session=session[0], chamber="upper").count() > 0: upper_dashboard = dashboards.filter(session=session[0], chamber="upper")[0] state_dqr_data[abbr] = { "state": state.name, "session_name": session_name, "lower_dashboard": lower_dashboard, "upper_dashboard": upper_dashboard, } return render(request, "dashboards/dqr_listing.html", { "state_dqr_data": state_dqr_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 people_matcher(request, state, session=None): jid = abbr_to_jid(state) all_sessions = sessions_with_bills(jid) if all_sessions: session = all_sessions[0] else: session = get_object_or_404(LegislativeSession, identifier=session, jurisdiction_id=jid) unmatched = UnmatchedName.objects.filter( session_id=session, status="U").order_by("-sponsorships_count") state_sponsors = Person.objects.filter(current_jurisdiction_id=jid) unmatched_total = unmatched.count() context = { "state": state, "session": session, "all_sessions": all_sessions, "unmatched": unmatched, "state_sponsors": state_sponsors, "unmatched_total": unmatched_total, } return render(request, "people_admin/people_matcher.html", context)
def handle(self, *args, **options): from django.conf import settings print("DEBUG", settings.DEBUG) state = options["state"] # 'all' grabs the first session from every state # 'all_sessions' grabs every session from every state if state == "all" or state == "all_sessions": scrape_state = state for state in states: sessions = sessions_with_bills(abbr_to_jid(state.abbr)) if len(sessions) > 0: state = state.abbr.lower() if scrape_state == "all_sessions": for session in sessions: session = session.identifier create_dqr(state, session) else: session = sessions[0].identifier create_dqr(state, session) else: sessions = get_available_sessions(state) for session in sessions: create_dqr(state, session)
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 test_sessions_with_bills(): jid = "ocd-jurisdiction/country:us/state:wi/government" d = Division.objects.create(id="ocd-division/country:us/state:wi", name="Wisconsin") j = Jurisdiction.objects.create(id=jid, name="Wisconsin", division=d) j.legislative_sessions.create(identifier="2016", name="2016") s17 = j.legislative_sessions.create(identifier="2017", name="2017") s18 = j.legislative_sessions.create(identifier="2018", name="2018") Bill.objects.create(identifier="HB 1", title="Test", legislative_session=s17) Bill.objects.create(identifier="HB 1", title="Test", legislative_session=s18) Bill.objects.create(identifier="HB 2", title="Test", legislative_session=s18) sessions = sessions_with_bills(jid) assert len(sessions) == 2 assert s17 in sessions assert s18 in sessions
def state(request, state): RECENTLY_INTRODUCED_BILLS_TO_SHOW = 4 RECENTLY_PASSED_BILLS_TO_SHOW = 4 jid = abbr_to_jid(state) # we need basically all of the orgs, so let's just do one big query for them legislature = None committee_counts = Counter() chambers = [] organizations = (Organization.objects.filter(jurisdiction_id=jid).annotate( seats=Sum("posts__maximum_memberships")).select_related("parent")) for org in organizations: if org.classification == "legislature": legislature = org elif org.classification in ("upper", "lower"): chambers.append(org) elif org.classification == "committee": committee_counts[org.parent.classification] += 1 # unicameral if not chambers: chambers = [legislature] # legislators legislators = PersonProxy.get_current_legislators_with_roles(chambers) for chamber in chambers: parties = [] titles = [] for legislator in legislators: if legislator.current_role["chamber"] == chamber.classification: parties.append(legislator.current_role["party"]) titles.append(legislator.current_role["role"]) chamber.parties = dict(Counter(parties)) chamber.title = titles[0] if chamber.seats - len(legislators) > 0: chamber.parties["Vacancies"] = chamber.seats - len(legislators) chamber.committee_count = committee_counts[chamber.classification] # bills bills = (Bill.objects.all().select_related( "legislative_session", "legislative_session__jurisdiction", "billstatus").filter(from_organization__in=chambers).prefetch_related( "sponsorships", "sponsorships__person")) recently_introduced_bills = list( bills.filter(billstatus__first_action_date__isnull=False).order_by( "-billstatus__first_action_date") [:RECENTLY_INTRODUCED_BILLS_TO_SHOW]) recently_passed_bills = list( bills.filter(billstatus__latest_passage_date__isnull=False).order_by( "-billstatus__latest_passage_date") [:RECENTLY_PASSED_BILLS_TO_SHOW]) _preprocess_sponsors(recently_introduced_bills) _preprocess_sponsors(recently_passed_bills) all_sessions = sessions_with_bills(jid) return render( request, "public/views/state.html", { "state": state, "state_nav": "overview", "legislature": legislature, "chambers": chambers, "chambers_json": {c.classification: c.name for c in chambers}, "recently_introduced_bills": recently_introduced_bills, "recently_passed_bills": recently_passed_bills, "all_sessions": all_sessions, }, )