From 1cf5ba4f8025cb63643335e426e89d4b8294c40b Mon Sep 17 00:00:00 2001 From: Derek Eder Date: Fri, 5 Jan 2024 16:02:27 -0600 Subject: [PATCH] rewrote primary_sponsorships attribute for person, using consistantly --- chicago/feeds.py | 8 +++----- chicago/models.py | 10 ++++++++++ chicago/views.py | 9 +-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/chicago/feeds.py b/chicago/feeds.py index c666c73..7ed387a 100644 --- a/chicago/feeds.py +++ b/chicago/feeds.py @@ -7,7 +7,7 @@ from django.urls import reverse, reverse_lazy from django.conf import settings -from .models import Person, Bill, Organization, Event +from .models import ChicagoPerson, Bill, Organization, Event from .utils import to_datetime @@ -107,7 +107,7 @@ class PersonDetailFeed(Feed): NUM_RECENT_BILLS = 20 def get_object(self, request, slug): - o = Person.objects.get(slug=slug) + o = ChicagoPerson.objects.get(slug=slug) return o def title(self, obj): @@ -132,9 +132,7 @@ def description(self, obj): return "Recent sponsored bills from " + obj.name + "." def items(self, person): - sponsored_bills = [s.bill for s in person.primary_sponsorships][:10] - recent_sponsored_bills = sponsored_bills[: self.NUM_RECENT_BILLS] - return recent_sponsored_bills + return person.primary_sponsorships[: self.NUM_RECENT_BILLS] class CommitteeDetailEventsFeed(Feed): diff --git a/chicago/models.py b/chicago/models.py index b7773fd..e57343a 100644 --- a/chicago/models.py +++ b/chicago/models.py @@ -281,6 +281,16 @@ def years_in_office(self): else: return years + @property + def primary_sponsorships(self): + return ( + ChicagoBill.objects.filter( + sponsorships__person=self, sponsorships__primary=True + ) + .annotate(last_action=models.Max("actions__date")) + .order_by("-last_action") + ) + class ChicagoPersonStatistic(models.Model): person = models.OneToOneField( diff --git a/chicago/views.py b/chicago/views.py index 1738bad..7183929 100644 --- a/chicago/views.py +++ b/chicago/views.py @@ -422,14 +422,7 @@ def get_context_data(self, **kwargs): ] = person.latest_council_membership.end_date_dt.strftime("%B %d, %Y") context["chair_positions"] = person.chair_role_memberships - - context["sponsored_legislation"] = ( - ChicagoBill.objects.filter( - sponsorships__person=person, sponsorships__primary=True - ) - .annotate(last_action=Max("actions__date")) - .order_by("-last_action")[:10] - ) + context["sponsored_legislation"] = person.primary_sponsorships[:10] attendance = person.full_attendance context["committee_count"] = len(person.current_memberships) - 1