From 48a7249c134bb6f898ad3a7fc8c1b8c14cc5798e Mon Sep 17 00:00:00 2001 From: jelanmathewjames Date: Sun, 7 Jul 2024 03:50:04 +0530 Subject: [PATCH] refactor: Update launchpad leaderboard to display latest organization, district, and state information for users --- api/launchpad/launchpad_views.py | 58 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/api/launchpad/launchpad_views.py b/api/launchpad/launchpad_views.py index 0de9a863..af988d3e 100644 --- a/api/launchpad/launchpad_views.py +++ b/api/launchpad/launchpad_views.py @@ -85,27 +85,34 @@ 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), - ), Prefetch( "user_role_link_user", queryset=UserRoleLink.objects.filter(verified=True, role__title__in=allowed_levels).select_related('role') ) - ).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( - 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), level=F("user_role_link_user__role__title"), time_=Max("karma_activity_log_user__created_at"), ).filter( @@ -147,28 +154,35 @@ 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), - ), Prefetch( "user_role_link_user", queryset=UserRoleLink.objects.filter(verified=True, role__title__in=allowed_levels).select_related('role') ) - ).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( - 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), level=F("user_role_link_user__role__title"), time_=Max("karma_activity_log_user__created_at"), ).distinct()