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_")