From 808f95105a4b9fb410714f6cfc1fa4c131095591 Mon Sep 17 00:00:00 2001 From: David Cain Date: Sun, 8 Sep 2024 08:50:28 -0600 Subject: [PATCH] Reduce local variable usage 18 local variables is too many and will trigger some lint rules. Let's try to tidy up this method. --- ws/views/participant.py | 56 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/ws/views/participant.py b/ws/views/participant.py index 5c7b938d..63538064 100644 --- a/ws/views/participant.py +++ b/ws/views/participant.py @@ -327,8 +327,6 @@ def get_trips(self) -> GroupedTrips: is_par = Q(signup__participant=participant) par_on_trip = is_par & Q(signup__on_trip=True) leading_trip = Q(leaders=participant) - in_future = Q(trip_date__gte=today) - in_past = Q(trip_date__lt=today) # Prefetch data to avoid n+1 queries enumerating trips prefetches = ["leaders__leaderrating_set"] @@ -339,30 +337,42 @@ def get_trips(self) -> GroupedTrips: waitlisted = trips.filter(is_par, signup__waitlistsignup__isnull=False) trips_led = participant.trips_led.prefetch_related(*prefetches) - trips_created = models.Trip.objects.filter( - creator=participant - ).prefetch_related(*prefetches) # Avoid doubly-listing trips where they participated or led the trip - created_but_not_on = trips_created.exclude(leading_trip | par_on_trip) - - upcoming_first = ("trip_date", "-time_created") - recent_first = ("-trip_date", "-time_created") - upcoming_trips: _TripDescriptor = { - "on_trip": accepted.filter(in_future).order_by(*upcoming_first), - "waitlisted": waitlisted.filter(in_future).order_by(*upcoming_first), - "leader": trips_led.filter(in_future).order_by(*upcoming_first), - "creator": created_but_not_on.filter(in_future).order_by(*upcoming_first), - "wimp": participant.wimp_trips.filter(in_future).order_by(*upcoming_first), - } - past_trips: _TripDescriptor = { - "on_trip": accepted.filter(in_past).order_by(*recent_first), - "waitlisted": None, - "leader": trips_led.filter(in_past).order_by(*recent_first), - "creator": created_but_not_on.filter(in_past).order_by(*recent_first), - "wimp": participant.wimp_trips.filter(in_past).order_by(*recent_first), + created_but_not_on = ( + models.Trip.objects.filter(creator=participant) + .prefetch_related(*prefetches) + .exclude(leading_trip | par_on_trip) + ) + + def _get_current_trips(query: QuerySet[models.Trip]) -> QuerySet[models.Trip]: + return query.filter(trip_date__gte=today).order_by( + "trip_date", # Upcoming trips first + "-time_created", + ) + + def _get_past_trips(query: QuerySet[models.Trip]) -> QuerySet[models.Trip]: + return query.filter(trip_date__lt=today).order_by( + "-trip_date", # Recent trips first + "-time_created", + ) + + return { + "current": { + "on_trip": _get_current_trips(accepted), + "waitlisted": _get_current_trips(waitlisted), + "leader": _get_current_trips(trips_led), + "creator": _get_current_trips(created_but_not_on), + "wimp": _get_current_trips(participant.wimp_trips), + }, + "past": { + "on_trip": _get_past_trips(accepted), + "waitlisted": None, + "leader": _get_past_trips(trips_led), + "creator": _get_past_trips(created_but_not_on), + "wimp": _get_past_trips(participant.wimp_trips), + }, } - return {"current": upcoming_trips, "past": past_trips} @staticmethod def get_stats(trips: GroupedTrips) -> list[str]: