Skip to content

Commit

Permalink
Merge branch 'dev-server' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
shaheenhyderk committed Dec 1, 2023
2 parents 0e554f4 + 6985aec commit 3b1ec23
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 58 deletions.
18 changes: 10 additions & 8 deletions api/dashboard/campus/campus_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db.models import Count, F
from django.db.models import Q, Case, When, Value
from django.db.models import Q
from rest_framework.views import APIView

from db.organization import UserOrganizationLink
Expand All @@ -8,7 +8,7 @@
from utils.permission import CustomizePermission, JWTUtils, role_required
from utils.response import CustomResponse
from utils.types import OrganizationType, RoleType
from utils.utils import CommonUtils, DateTimeUtils
from utils.utils import CommonUtils
from . import serializers
from .dash_campus_helper import get_user_college_link

Expand All @@ -29,7 +29,7 @@ class CampusDetailsAPI(APIView):
authentication_classes = [CustomizePermission]

# Use the role_required decorator to specify the allowed roles for this view
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value, RoleType.LEAD_ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.LEAD_ENABLER.value])
def get(self, request):
# Fetch the user's ID from the request using JWTUtils
user_id = JWTUtils.fetch_user_id(request)
Expand All @@ -54,7 +54,7 @@ def get(self, request):
class CampusStudentInEachLevelAPI(APIView):
authentication_classes = [CustomizePermission]

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

Expand All @@ -80,7 +80,7 @@ def get(self, request):
class CampusStudentDetailsAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value, RoleType.LEAD_ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.LEAD_ENABLER.value])
def get(self, request):
user_id = JWTUtils.fetch_user_id(request)
user_org_link = get_user_college_link(user_id)
Expand Down Expand Up @@ -121,6 +121,7 @@ def get(self, request):
karma=F("wallet_user__karma"),
level=F("user_lvl_link_user__level__name"),
join_date=F("created_at"),
last_karma_gained=F("wallet_user__karma_last_update_at"),
department=F('user_organization_link_user__department__title'),
graduation_year=F("user_organization_link_user__graduation_year"),
is_alumni=F('user_organization_link_user__is_alumni'),
Expand Down Expand Up @@ -154,6 +155,7 @@ def get(self, request):
karma=F("wallet_user__karma"),
level=F("user_lvl_link_user__level__name"),
join_date=F("created_at"),
last_karma_gained=F("wallet_user__karma_last_update_at"),
department=F('user_organization_link_user__department__title'),
graduation_year=F("user_organization_link_user__graduation_year"),
is_alumni=F('user_organization_link_user__is_alumni'),
Expand Down Expand Up @@ -189,7 +191,7 @@ def get(self, request):
class CampusStudentDetailsCSVAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value, RoleType.LEAD_ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.LEAD_ENABLER.value])
def get(self, request):
user_id = JWTUtils.fetch_user_id(request)
user_org_link = get_user_college_link(user_id)
Expand Down Expand Up @@ -292,7 +294,7 @@ def get(self, request):
class WeeklyKarmaAPI(APIView):
authentication_classes = [CustomizePermission]

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

Expand All @@ -310,7 +312,7 @@ def get(self, request):
class ChangeStudentTypeAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value, RoleType.LEAD_ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.LEAD_ENABLER.value])
def patch(self, request, member_id):
user_id = JWTUtils.fetch_user_id(request)

Expand Down
12 changes: 6 additions & 6 deletions api/dashboard/campus/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class CampusDetailsSerializer(serializers.ModelSerializer):
college_name = serializers.ReadOnlyField(source="org.title")
campus_code = serializers.ReadOnlyField(source="org.code")
campus_zone = serializers.ReadOnlyField(source="org.district.zone.name")
campus_lead = serializers.ReadOnlyField(source="user.fullname")
campus_level = serializers.SerializerMethodField()
total_karma = serializers.SerializerMethodField()
total_members = serializers.SerializerMethodField()
Expand All @@ -28,7 +27,6 @@ class Meta:
model = UserOrganizationLink
fields = [
"college_name",
"campus_lead",
"campus_code",
"campus_zone",
"campus_level",
Expand All @@ -52,7 +50,7 @@ def get_lead(self, obj):
enabler = User.objects.filter(
user_organization_link_user__org=obj.org,
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_role_link_user__role__title=RoleType.ENABLER.value,
user_role_link_user__role__title=RoleType.LEAD_ENABLER.value,
).first()
if enabler:
enabler = enabler.fullname
Expand Down Expand Up @@ -90,10 +88,10 @@ def get_total_karma(self, obj):

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

rank_dict = {
data["org"]: data["total_karma"] if data["total_karma"] is not None else 0
Expand All @@ -119,14 +117,16 @@ class CampusStudentDetailsSerializer(serializers.Serializer):
level = serializers.CharField()
# is_active = serializers.CharField()
join_date = serializers.CharField()
last_karma_gained = serializers.CharField()
email = serializers.CharField()
mobile = serializers.CharField()
graduation_year = serializers.CharField()
department = serializers.CharField()
is_alumni = serializers.CharField()

class Meta:
fields = ("user_id", "email", "mobile", "fullname", "karma", "muid", "rank", "level", "join_date", "is_alumni")
fields = ("user_id", "email", "mobile", "fullname", "karma", "muid", "rank", "level", "join_date", "is_alumni",
"last_karma_update_at")

def get_rank(self, obj):
ranks = self.context.get("ranks")
Expand Down
4 changes: 2 additions & 2 deletions api/dashboard/college/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def get_no_of_alumni(self, obj):
def get_no_of_lc(self, obj):
learning_circle_count = LearningCircle.objects.filter(org=obj.org).count()
no_of_lc_increased = LearningCircle.objects.filter(org=obj.org,
created_at__lte=DateTimeUtils.get_current_utc_time() - timedelta(
created_at__gte=DateTimeUtils.get_current_utc_time() - timedelta(
days=30)).count()
return {'lc_count': learning_circle_count, 'no_of_lc_increased': no_of_lc_increased}

def get_number_of_members(self, obj):
member_count = obj.org.user_organization_link_org.all().count()

no_of_members_increased = obj.org.user_organization_link_org.filter(
created_at__lte=DateTimeUtils.get_current_utc_time() - timedelta(days=30)
created_at__gte=DateTimeUtils.get_current_utc_time() - timedelta(days=30)
).count()
return {'member_count': member_count, 'no_of_members_increased': no_of_members_increased}

Expand Down
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
1 change: 1 addition & 0 deletions api/dashboard/lc/dash_lc_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ def validate_attendees(self, attendees):
])
wallet = Wallet.objects.filter(user_id=user).first()
wallet.karma += Lc.KARMA.value
wallet.karma_last_update_at = DateTimeUtils.get_current_utc_time()
wallet.updated_at = DateTimeUtils.get_current_utc_time()
wallet.save()
return attendees
Expand Down
Loading

0 comments on commit 3b1ec23

Please sign in to comment.