From bd00f3b7fc30f89279311915d521089a6274aed0 Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Fri, 5 Jul 2024 17:39:18 +0530 Subject: [PATCH] refactor: Update launchpad leaderboard to display latest organization, district, and state information for users --- api/launchpad/launchpad_views.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index 9ea2d001..0de9a863 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -33,24 +33,30 @@ def get(self, request): task__hashtag='#lp24-introduction', ).values('user') + latest_org_link = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__title')[:1] + + latest_district = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__name')[:1] + + latest_state = UserOrganizationLink.objects.filter( + user=OuterRef('id'), + org__org_type__in=allowed_org_types + ).order_by('-created_at').values('org__district__zone__state__name')[:1] + users = User.objects.filter( karma_activity_log_user__task__event="launchpad", karma_activity_log_user__appraiser_approved=True, id__in=intro_task_completed_users - ).prefetch_related( - Prefetch( - "user_organization_link_user", - queryset=UserOrganizationLink.objects.filter(org__org_type__in=allowed_org_types), - ) - ).filter( - Q(user_organization_link_user__user_id__in=UserOrganizationLink.objects.filter( - org__org_type__in=["College", "School", "Company", "Community"] - ).values("user_id")) | Q(user_organization_link_user__id__isnull=True) ).annotate( karma=Subquery(total_karma_subquery, output_field=IntegerField()), - org=F("user_organization_link_user__org__title"), - district_name=F("user_organization_link_user__org__district__name"), - state=F("user_organization_link_user__org__district__zone__state__name"), + org=Subquery(latest_org_link), + district_name=Subquery(latest_district), + state=Subquery(latest_state), time_=Max("karma_activity_log_user__created_at"), ).order_by("-karma", "time_")