diff --git a/lametro/models.py b/lametro/models.py index e187f361..7533db2b 100644 --- a/lametro/models.py +++ b/lametro/models.py @@ -15,7 +15,7 @@ from django.utils.functional import cached_property from django.core.cache import cache -from django.db.models import Prefetch, Case, When, Value, Q, F +from django.db.models import Prefetch, Case, When, Value, Q, F, Subquery, OuterRef from django.db.models.functions import Now, Cast from django.templatetags.static import static from opencivicdata.legislative.models import ( @@ -24,6 +24,7 @@ EventRelatedEntity, RelatedBill, BillVersion, + BillAction, ) from proxy_overrides.related import ProxyForeignKey @@ -148,6 +149,17 @@ def get_queryset(self): return qs + def with_latest_actions(self): + latest_action = BillAction.objects.filter(bill=OuterRef("pk")).order_by( + "-order" + ) + + qs = self.annotate( + last_action_description=Subquery(latest_action.values("description")[:1]) + ) + + return qs + class LAMetroBill(Bill, SourcesMixin): objects = LAMetroBillManager() diff --git a/lametro/templates/event/_related_bills.html b/lametro/templates/event/_related_bills.html index f85da0b3..6a2f4cbe 100644 --- a/lametro/templates/event/_related_bills.html +++ b/lametro/templates/event/_related_bills.html @@ -14,11 +14,11 @@

Board Reports

{% with associated_bill=report.related_entities.all.0.bill %} {{ report.notes.0 | parse_agenda_item }} - {{associated_bill.identifier}} {{report.description | short_blurb}} {{ associated_bill.inferred_status | inferred_status_label | safe }} + {{associated_bill.identifier}} {{report.description | short_blurb}} {{ associated_bill.last_action_description | bill_status_from_last_action | inferred_status_label | safe }} View - Download + Download {% endwith %} {% endfor %} diff --git a/lametro/templatetags/lametro_extras.py b/lametro/templatetags/lametro_extras.py index 5566a5e1..060a1c80 100644 --- a/lametro/templatetags/lametro_extras.py +++ b/lametro/templatetags/lametro_extras.py @@ -7,7 +7,10 @@ from django import template from django.utils import timezone -from councilmatic.settings_jurisdiction import legislation_types +from councilmatic.settings_jurisdiction import ( + legislation_types, + BILL_STATUS_DESCRIPTIONS, +) from councilmatic.settings import PIC_BASE_URL from councilmatic_core.models import Person, Bill @@ -327,3 +330,10 @@ def get_events_with_manual_broadcasts(): broadcasts = EventBroadcast.objects.filter(is_manually_live=True) events = [b.event for b in broadcasts] return events + + +@register.filter +def bill_status_from_last_action(description): + if description and description.upper() in BILL_STATUS_DESCRIPTIONS.keys(): + return BILL_STATUS_DESCRIPTIONS[description.upper()]["search_term"] + return None diff --git a/lametro/views.py b/lametro/views.py index b21a670c..f5033e76 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -17,7 +17,16 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render from django.db.models.functions import Lower, Now, Cast -from django.db.models import Max, Prefetch, Case, When, Value, IntegerField, Q, F +from django.db.models import ( + Max, + Prefetch, + Case, + When, + Value, + IntegerField, + Q, + F, +) from django.urls import reverse from django.utils import timezone from django.views.generic import ( @@ -51,6 +60,7 @@ from councilmatic_core.models import Organization, Membership from opencivicdata.core.models import PersonLink +from opencivicdata.legislative.models import BillVersion from lametro.models import ( LAMetroBill, @@ -209,10 +219,29 @@ def get_context_data(self, **kwargs): except EventDocument.DoesNotExist: pass + related_bills = ( + LAMetroBill.objects.with_latest_actions() + .defer("extras") + .filter(eventrelatedentity__agenda_item__event=event) + .prefetch_related( + Prefetch( + "versions", + queryset=BillVersion.objects.filter( + note="Board Report" + ).prefetch_related("links"), + to_attr="br", + ), + "packet", + ) + ) + agenda_with_board_reports = ( event.agenda.filter(related_entities__bill__versions__isnull=False) .annotate(int_order=Cast("order", IntegerField())) .order_by("int_order") + .prefetch_related( + Prefetch("related_entities__bill", queryset=related_bills) + ) ) # Find agenda link.