diff --git a/lametro/models.py b/lametro/models.py index 2e4fca2e..3f1a8006 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 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 @@ -358,7 +359,7 @@ class Meta: def slug_name(self): return slugify(self.name) - @property + @cached_property def latest_council_membership(self): filter_kwarg = { "organization__name": settings.OCD_CITY_COUNCIL_NAME, @@ -417,12 +418,16 @@ def committee_sponsorships(self): Organizations do not include the Board of Directors. """ + actions = BillAction.objects.select_related("organization").filter( + organization__classification="committee", + organization__memberships__in=Subquery( + self.current_memberships.values("pk") + ), + ) + qs = ( LAMetroBill.objects.defer("extras") - .filter( - actions__organization__classification="committee", - actions__organization__memberships__in=self.current_memberships, - ) + .filter(actions__in=Subquery(actions.values("pk"))) .order_by("-actions__date") .distinct()[:5] ) diff --git a/lametro/templates/person/person.html b/lametro/templates/person/person.html index 379f93cf..f4a49132 100644 --- a/lametro/templates/person/person.html +++ b/lametro/templates/person/person.html @@ -64,9 +64,9 @@
- Credit: {{person.headshot_source}} + Credit: {{ headshot_source }}
{% endif %} diff --git a/lametro/views.py b/lametro/views.py index a88a6030..a50b3e26 100644 --- a/lametro/views.py +++ b/lametro/views.py @@ -701,13 +701,12 @@ def get_context_data(self, **kwargs): except AttributeError: context["map_geojson"] = None - if person.committee_sponsorships: + if self.request.GET.get("view") == "board-reports": context["sponsored_legislation"] = person.committee_sponsorships - else: - context["sponsored_legislation"] = [] context["memberships_list"] = ( - person.current_memberships.exclude(organization__name="Board of Directors") + person.current_memberships.prefetch_related("organization") + .exclude(organization__name="Board of Directors") .annotate( index=Case( When(role="Chair", then=Value(0)), @@ -730,6 +729,8 @@ def get_context_data(self, **kwargs): except PersonLink.DoesNotExist: pass + context["headshot_source"] = person.headshot_source + return context