Skip to content

Commit

Permalink
Merge pull request #1673 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
zonal and district dashboard minor fixes
  • Loading branch information
adnankattekaden authored Nov 30, 2023
2 parents d56e251 + 047a1bb commit 2be156c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
18 changes: 11 additions & 7 deletions api/dashboard/district/dash_district_serializer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from datetime import timedelta

from django.db.models import Sum, Count, Q
from django.db.models import Sum
from rest_framework import serializers
from django.db.models import Sum, Count, Q, Case, When, IntegerField


from db.organization import UserOrganizationLink, Organization
from db.task import KarmaActivityLog, Level
from db.user import User
from utils.types import OrganizationType, RoleType
from utils.utils import DateTimeUtils


Expand All @@ -34,10 +33,10 @@ class Meta:

def get_rank(self, obj):
org_karma_dict = (
UserOrganizationLink.objects.all()
UserOrganizationLink.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values("org__district")
.annotate(total_karma=Sum("user__wallet_user__karma"))
)
).order_by("-total_karma", "org__created_at")

rank_dict = {
data["org__district"]: data["total_karma"]
Expand All @@ -57,18 +56,21 @@ def get_rank(self, obj):

def get_district_lead(self, obj):
user_org_link = UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value,
org__district=obj.org.district,
user__user_role_link_user__role__title="District Campus Lead",
user__user_role_link_user__role__title=RoleType.DISTRICT_CAMPUS_LEAD.value,
).first()
return user_org_link.user.fullname if user_org_link else None

def get_karma(self, obj):
return UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value,
org__district=obj.org.district
).aggregate(total_karma=Sum("user__wallet_user__karma"))["total_karma"]

def get_total_members(self, obj):
return UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value,
org__district=obj.org.district
).count()

Expand All @@ -80,6 +82,7 @@ def get_active_members(self, obj):
) - timedelta(days=1)

return KarmaActivityLog.objects.filter(
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user__user_organization_link_user__org__district=obj.org.district,
created_at__range=(start_date, end_date),
).count()
Expand All @@ -98,7 +101,7 @@ def get_rank(self, obj):
keys_list = list(self.context.get("ranks").keys())
position = keys_list.index(obj.id)
return position + 1

def get_karma(self, obj):
return self.context.get("ranks")[obj.id]

Expand All @@ -114,6 +117,7 @@ def get_students_count(self, obj):
district = self.context.get("district")
return (
User.objects.filter(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_organization_link_user__org__district=district,
user_lvl_link_user__level=obj,
)
Expand Down
7 changes: 4 additions & 3 deletions api/dashboard/district/dash_district_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ def get(self, request):

org_karma_dict = (
UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value,
org__district=user_org_link.org.district,
verified=True,
)
.values("org")
.annotate(total_karma=Sum("user__wallet_user__karma"))
.annotate(total_karma=Sum("user__wallet_user__karma")).order_by('-total_karma', 'org__created_at')
)

org_ranks = {
Expand Down Expand Up @@ -102,7 +103,7 @@ def get(self, request):
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.distinct()
.order_by("-karma", "-created_at")
.order_by("-karma", '-update_at', "created_at")
.values(
"user_id",
"karma",
Expand Down Expand Up @@ -164,7 +165,7 @@ def get(self, request):
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.distinct()
.order_by("-karma", "-created_at")
.order_by("-karma", '-updated_at', "created_at")
.values(
"user_id",
"karma",
Expand Down
9 changes: 7 additions & 2 deletions api/dashboard/zonal/dash_zonal_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from db.organization import UserOrganizationLink, District
from db.task import KarmaActivityLog, Level
from db.user import User
from utils.types import OrganizationType
from utils.utils import DateTimeUtils


Expand All @@ -30,10 +31,10 @@ class Meta:

def get_rank(self, obj):
org_karma_dict = (
UserOrganizationLink.objects.all()
UserOrganizationLink.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values("org__district__zone")
.annotate(total_karma=Sum("user__wallet_user__karma"))
)
).order_by("-total_karma", "org__created_at")

rank_dict = {
data["org__district__zone"]: data["total_karma"]
Expand All @@ -53,11 +54,13 @@ def get_rank(self, obj):

def get_karma(self, obj):
return UserOrganizationLink.objects.filter(
org_org_type=OrganizationType.COLLEGE.value,
org__district__zone=obj.org.district.zone,
).aggregate(total_karma=Sum("user__wallet_user__karma"))["total_karma"]

def get_total_members(self, obj):
return UserOrganizationLink.objects.filter(
org_org_type=OrganizationType.COLLEGE.value,
org__district__zone=obj.org.district.zone,
).count()

Expand All @@ -69,6 +72,7 @@ def get_active_members(self, obj):
) - timedelta(days=1)

return KarmaActivityLog.objects.filter(
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user__user_organization_link_user__org__district__zone=obj.org.district.zone,
created_at__range=(start_date, end_date),
).count()
Expand Down Expand Up @@ -103,6 +107,7 @@ def get_students_count(self, obj):
zone = self.context.get("zone")
return (
User.objects.filter(
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_organization_link_user__org__district__zone=zone,
user_lvl_link_user__level=obj,
)
Expand Down
11 changes: 5 additions & 6 deletions api/dashboard/zonal/dash_zonal_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from utils.response import CustomResponse
from utils.types import OrganizationType, RoleType
from utils.utils import CommonUtils

from . import dash_zonal_helper, dash_zonal_serializer


Expand All @@ -32,14 +31,15 @@ def get(self, request):
class ZonalTopThreeDistrictAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.ZONAL_CAMPUS_LEAD.value,])
@role_required([RoleType.ZONAL_CAMPUS_LEAD.value, ])
def get(self, request):
user_id = JWTUtils.fetch_user_id(request)

user_org_link = dash_zonal_helper.get_user_college_link(user_id)

district_karma_dict = (
UserOrganizationLink.objects.filter(
org__org_type=OrganizationType.COLLEGE.value,
org__district__zone=user_org_link.org.district.zone,
verified=True,
)
Expand Down Expand Up @@ -72,9 +72,8 @@ def get(self, request):
class ZonalStudentLevelStatusAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.ZONAL_CAMPUS_LEAD.value,])
@role_required([RoleType.ZONAL_CAMPUS_LEAD.value, ])
def get(self, request):

user_id = JWTUtils.fetch_user_id(request)

user_org_link = dash_zonal_helper.get_user_college_link(user_id)
Expand Down Expand Up @@ -103,7 +102,7 @@ def get(self, request):
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.distinct()
.order_by("-karma", "-created_at")
.order_by("-karma", "-updated_at", "created_at")
.values(
"user_id",
"karma",
Expand Down Expand Up @@ -165,7 +164,7 @@ def get(self, request):
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
)
.distinct()
.order_by("-karma", "-created_at")
.order_by("-karma", "-updated_at", "created_at")
.values(
"user_id",
"karma",
Expand Down

0 comments on commit 2be156c

Please sign in to comment.