Skip to content

Commit

Permalink
Merge pull request #1161 from Metro-Records/feature/1138-events
Browse files Browse the repository at this point in the history
Optimize event detail page
  • Loading branch information
antidipyramid authored Oct 7, 2024
2 parents a63e335 + f232aa3 commit 70fca3d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lametro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -24,6 +24,7 @@
EventRelatedEntity,
RelatedBill,
BillVersion,
BillAction,
)
from proxy_overrides.related import ProxyForeignKey

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions lametro/templates/event/_related_bills.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ <h4>Board Reports</h4>
{% with associated_bill=report.related_entities.all.0.bill %}
<tr>
<td><strong>{{ report.notes.0 | parse_agenda_item }}</strong></td>
<td>{{associated_bill.identifier}} {{report.description | short_blurb}} {{ associated_bill.inferred_status | inferred_status_label | safe }}</td>
<td>{{associated_bill.identifier}} {{report.description | short_blurb}} {{ associated_bill.last_action_description | bill_status_from_last_action | inferred_status_label | safe }}</td>
<td>
<a href='/board-report/{{ associated_bill.slug }}/' target="_blank">View</a>
</td>
<td><a href={% if associated_bill.packet.is_ready %}"{{associated_bill.packet.url}}"{% else %}"{{associated_bill.board_report.url}}"{% endif %}>Download</a></td>
<td><a href={% if associated_bill.packet.is_ready %}"{{associated_bill.packet.url}}"{% else %}"{{associated_bill.br.0.links.all.0.url}}"{% endif %}>Download</a></td>
</tr>
{% endwith %}
{% endfor %}
Expand Down
12 changes: 11 additions & 1 deletion lametro/templatetags/lametro_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
31 changes: 30 additions & 1 deletion lametro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 70fca3d

Please sign in to comment.