Skip to content

Commit

Permalink
Merge pull request #2052 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
Dev server
  • Loading branch information
jelanmathewjames authored Jun 11, 2024
2 parents 5954395 + 4a6339a commit 98950d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
10 changes: 2 additions & 8 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db.models import Sum, Max, Prefetch, F, OuterRef, Subquery, Window, IntegerField
from django.db.models.functions import RowNumber
from django.db import connection
from django.db.models import Sum, Max, Prefetch, F, OuterRef, Subquery, IntegerField

from rest_framework.views import APIView

from .serializers import LaunchpadLeaderBoardSerializer
Expand Down Expand Up @@ -48,11 +47,6 @@ def get(self, request):
district_name=F("user_organization_link_user__org__district__name"),
state=F("user_organization_link_user__org__district__zone__state__name"),
time_=Max("karma_activity_log_user__created_at"),
).annotate(
rank=Window(
expression=RowNumber(),
order_by=[F('karma').desc(), F('time_').asc()]
)
).order_by("-karma", "time_")

paginated_queryset = CommonUtils.get_paginated_queryset(
Expand Down
52 changes: 51 additions & 1 deletion api/launchpad/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from django.db.models import Sum, Max, Prefetch, F, OuterRef, Subquery, Window, IntegerField
from django.db.models.functions import Rank

from rest_framework import serializers

from db.user import User
from db.organization import UserOrganizationLink
from db.task import KarmaActivityLog
import json

class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
rank = serializers.IntegerField()
rank = serializers.SerializerMethodField()
karma = serializers.IntegerField()
org = serializers.CharField()
district_name = serializers.CharField()
Expand All @@ -12,3 +18,47 @@ class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("rank", "full_name", "karma", "org", "district_name", "state")

def get_rank(self, obj):
total_karma_subquery = KarmaActivityLog.objects.filter(
user=OuterRef('id'),
task__event='launchpad',
appraiser_approved=True,
).values('user').annotate(
total_karma=Sum('karma')
).values('total_karma')
allowed_org_types = ["College", "School", "Company"]

intro_task_completed_users = KarmaActivityLog.objects.filter(
task__event='launchpad',
appraiser_approved=True,
task__hashtag='#lp24-introduction',
).values('user')

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(
user_organization_link_user__id__in=UserOrganizationLink.objects.filter(
org__org_type__in=allowed_org_types
).values("id")
).annotate(
karma=Subquery(total_karma_subquery, output_field=IntegerField()),
time_=Max("karma_activity_log_user__created_at"),
).order_by("-karma", "time_")

# high complexity
rank = 0
for data in users:
rank += 1
if data.id == obj.id:
break

return rank

0 comments on commit 98950d8

Please sign in to comment.