From 05a7f955a1d9380c200822a167ad67117734366f Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Fri, 5 Jul 2024 17:51:14 +0530 Subject: [PATCH] refactor: Update launchpad leaderboard serializer --- api/launchpad/serializers.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/api/launchpad/serializers.py b/api/launchpad/serializers.py index 3f55279f..741a7207 100644 --- a/api/launchpad/serializers.py +++ b/api/launchpad/serializers.py @@ -39,22 +39,31 @@ def get_rank(self, obj): appraiser_approved=True, 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__id__in=UserOrganizationLink.objects.filter( - org__org_type__in=allowed_org_types - ).values("id")) | Q(user_organization_link_user__id__isnull=True) ).annotate( karma=Subquery(total_karma_subquery, output_field=IntegerField()), + 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_")