Skip to content

Commit

Permalink
Merge pull request #2193 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
Dev server
  • Loading branch information
jelanmathewjames authored Jul 31, 2024
2 parents 5a74d3f + ceba6b2 commit 99f4c1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 73 deletions.
24 changes: 24 additions & 0 deletions api/launchpad/launchpad_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def get(self, request):
time_=Max("karma_activity_log_user__created_at"),
).order_by("-karma", "time_")

rank_list = list(users)
for index, user in enumerate(rank_list):
user.rank = index + 1

paginated_queryset = CommonUtils.get_paginated_queryset(
users,
request,
Expand All @@ -89,8 +93,25 @@ def get(self, request):
completed_tasks=Count('task', distinct=True)
).filter(completed_tasks=launchpad_tasks.count())

allowed_org_types = ["College", "School", "Company"]

completed_users = completed_tasks_counts.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]

wallet_subquery = Wallet.objects.filter(
user=OuterRef('id')
).values('karma')[:1]
Expand All @@ -101,6 +122,9 @@ def get(self, request):
id__in=completed_users
).annotate(
karma=Subquery(wallet_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_")

Expand Down
80 changes: 7 additions & 73 deletions api/launchpad/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,19 @@ def get_rank(self, obj):
).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')

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

Expand All @@ -77,7 +58,7 @@ def get_rank(self, obj):
return rank

class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
rank = serializers.SerializerMethodField()
rank = serializers.IntegerField()
karma = serializers.IntegerField()
actual_karma = serializers.IntegerField(source="wallet_user.karma", default=None)
org = serializers.CharField(allow_null=True, allow_blank=True)
Expand All @@ -88,68 +69,21 @@ class LaunchpadLeaderBoardSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("rank", "full_name", "actual_karma", "karma", "org", "district_name", "state","launchpad_id")

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')

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

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

return rank


class TaskCompletedLeaderBoardSerializer(serializers.ModelSerializer):
rank = serializers.IntegerField()
karma = serializers.IntegerField()
is_public = serializers.BooleanField(source='user_settings_user.is_public',default=False)
org = serializers.CharField(allow_null=True, allow_blank=True)
district_name = serializers.CharField(allow_null=True, allow_blank=True)
state = serializers.CharField(allow_null=True, allow_blank=True)

class Meta:
model = User
fields = ("muid", "is_public", "rank", "full_name", "karma")

fields = ("muid", "is_public", "rank", "full_name", "karma", "org", "district_name", "state", "profile_pic")

class LaunchpadParticipantsSerializer(serializers.ModelSerializer):
org = serializers.CharField(allow_null=True, allow_blank=True)
Expand Down

0 comments on commit 99f4c1f

Please sign in to comment.