Skip to content

Commit

Permalink
Merge pull request #1632 from gtech-mulearn/dev-server
Browse files Browse the repository at this point in the history
dev to prod
  • Loading branch information
adnankattekaden authored Nov 29, 2023
2 parents a078267 + a36078d commit ee7e664
Show file tree
Hide file tree
Showing 21 changed files with 662 additions and 385 deletions.
52 changes: 38 additions & 14 deletions api/dashboard/campus/campus_views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from django.db.models import Count, F
from rest_framework.views import APIView
from django.db.models import Q, Case, When, Value
from rest_framework.views import APIView

from db.user import User
from . import serializers
from db.organization import UserOrganizationLink
from db.task import Level, Wallet
from db.user import User
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 . import serializers
from .dash_campus_helper import get_user_college_link
from utils.permission import CustomizePermission, JWTUtils, role_required
from utils.exception import CustomException


class CampusDetailsAPI(APIView):
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])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.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 @@ -44,7 +44,7 @@ def get(self, request):
general_message="Campus lead has no college"
).get_failure_response()

# Serialize the user's organization link using the CampusDetailsSerializer
# # Serialize the user's organization link using the CampusDetailsSerializer
serializer = serializers.CampusDetailsSerializer(user_org_link, many=False)

# Return a success response with the serialized data
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])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value, RoleType.LEAD_ENABLER.value])
def get(self, request):
user_id = JWTUtils.fetch_user_id(request)

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

@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.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)

start_date, end_date = DateTimeUtils.get_start_and_end_of_previous_month()
is_alumni = request.query_params.get("is_alumni", "").lower() in ["1", "true"]

if user_org_link.org is None:
return CustomResponse(
Expand All @@ -96,6 +95,7 @@ def get(self, request):
Wallet.objects.filter(
user__user_organization_link_user__org=user_org_link.org,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user__user_organization_link_user__is_alumni=is_alumni,
)
.distinct()
.order_by("-karma", "-created_at")
Expand All @@ -111,13 +111,18 @@ def get(self, request):
User.objects.filter(
user_organization_link_user__org=user_org_link.org,
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_organization_link_user__is_alumni=is_alumni,
)
.distinct()
.annotate(
user_id=F("id"),
email_=F("email"),
mobile_=F("mobile"),
karma=F("wallet_user__karma"),
level=F("user_lvl_link_user__level__name"),
join_date=F("created_at"),
department=F('user_organization_link_user__department__title'),
graduation_year=F("user_organization_link_user__graduation_year")
))

paginated_queryset = CommonUtils.get_paginated_queryset(
Expand Down Expand Up @@ -150,7 +155,7 @@ def get(self, request):
class CampusStudentDetailsCSVAPI(APIView):
authentication_classes = [CustomizePermission]

@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.value])
@role_required([RoleType.CAMPUS_LEAD.value, RoleType.ENABLER.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 @@ -210,7 +215,7 @@ def get(self, request):
class WeeklyKarmaAPI(APIView):
authentication_classes = [CustomizePermission]

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

Expand All @@ -222,4 +227,23 @@ def get(self, request):
).get_failure_response()

serializer = serializers.WeeklyKarmaSerializer(user_org_link)
return CustomResponse(response=serializer.data).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()


class ChangeStudentTypeAPI(APIView):
authentication_classes = [CustomizePermission]

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

user_org_link = get_user_college_link(user_id)
user_org_link_obj = UserOrganizationLink.objects.filter(user__id=member_id,
org=user_org_link.org,
org__org_type=OrganizationType.COLLEGE.value).first()

serializer = serializers.ChangeStudentTypeSerializer(user_org_link_obj, data=request.data)
if serializer.is_valid():
serializer.save()
return CustomResponse(general_message='Student Type updated successfully').get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()
74 changes: 70 additions & 4 deletions api/dashboard/campus/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.db.models import Sum
from rest_framework import serializers

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


Expand All @@ -14,33 +16,66 @@ class CampusDetailsSerializer(serializers.ModelSerializer):
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()
active_members = serializers.SerializerMethodField()
rank = serializers.SerializerMethodField()

lead = serializers.SerializerMethodField()

class Meta:
model = UserOrganizationLink
fields = [
"college_name",
"campus_lead",
"campus_code",
"campus_zone",
"campus_level",
"total_karma",
"total_members",
"active_members",
"rank",
"lead",
]

def get_lead(self, obj):

campus_lead = 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.CAMPUS_LEAD.value,
).first()
if campus_lead:
campus_lead = campus_lead.fullname

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,
).first()
if enabler:
enabler = enabler.fullname

return {'campus_lead': campus_lead, 'enabler': enabler}

def get_campus_level(self, obj):
campus = College.objects.filter(org=obj.org).first()
if campus:
return campus.level

return None

def get_total_members(self, obj):
return obj.org.user_organization_link_org.count()

def get_active_members(self, obj):
last_month = DateTimeUtils.get_current_utc_time() - timedelta(days=30)

last_month = DateTimeUtils.get_current_utc_time() - timedelta(weeks=26) # 6months
return obj.org.user_organization_link_org.filter(
verified=True,
user__wallet_user__isnull=False,
user__wallet_user__created_at__gte=last_month,
user__wallet_user__karma_last_update_at__gte=last_month,
).count()

def get_total_karma(self, obj):
Expand Down Expand Up @@ -84,9 +119,13 @@ class CampusStudentDetailsSerializer(serializers.Serializer):
level = serializers.CharField()
# is_active = serializers.CharField()
join_date = serializers.CharField()
email = serializers.CharField()
mobile = serializers.CharField()
graduation_year = serializers.CharField()
department = serializers.CharField()

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

def get_rank(self, obj):
ranks = self.context.get("ranks")
Expand Down Expand Up @@ -121,3 +160,30 @@ def to_representation(self, instance):
response[str(date)] = karma_logs.get("karma", 0)

return response


class ChangeStudentTypeSerializer(serializers.Serializer):
is_alumni = serializers.BooleanField(default=False)

class Meta:
model = UserOrganizationLink
fields = ("is_alumni",)

def update(self, instance, validated_data):
instance.is_alumni = validated_data.get('is_alumni')
instance.save()

return instance


class ListAluminiSerializer(serializers.Serializer):
user_id = serializers.CharField()
fullname = serializers.SerializerMethodField()
muid = serializers.CharField()
karma = serializers.IntegerField()
rank = serializers.SerializerMethodField()
level = serializers.CharField()
join_date = serializers.CharField()

class Meta:
fields = ("user_id", "fullname", "karma", "muid", "rank", "level", "join_date")
2 changes: 2 additions & 0 deletions api/dashboard/campus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
path("student-details/", campus_views.CampusStudentDetailsAPI.as_view(), name='student-details'),
path("student-details/csv/", campus_views.CampusStudentDetailsCSVAPI.as_view(), name='student-details-csv'),
path("weekly-karma/", campus_views.WeeklyKarmaAPI.as_view(), name='weekly-karma-insights'),

path('change-student-type/<str:member_id>/', campus_views.ChangeStudentTypeAPI.as_view(), name='change-student-type')
]
73 changes: 7 additions & 66 deletions api/dashboard/college/college_view.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,30 @@
from rest_framework.views import APIView
from utils.types import OrganizationType, RoleType

from db.organization import College
from db.user import User
from utils.permission import JWTUtils
from utils.response import CustomResponse
from utils.utils import CommonUtils
from .serializer import (
CollegeCreateDeleteSerializer,
CollegeListSerializer,
CollegeEditSerializer,
)
from utils.utils import CommonUtils
from django.db.models import Case,When,CharField,F


class CollegeApi(APIView):
def get(self, request, college_code=None):
if college_code:
colleges = College.objects.filter(id=college_code)
else:
colleges = College.objects.all().select_related(
"created_by", "updated_by", "org"
)
colleges = College.objects.all().select_related("org")

leads = (
User.objects.filter(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
user_role_link_user__role__title=RoleType.CAMPUS_LEAD.value,
)
.distinct()
.annotate(
college=Case(
When(
user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
then=F("user_organization_link_user__org__id"),
),
default=None,
output_field=CharField(),
)
)
)
paginated_queryset = CommonUtils.get_paginated_queryset(
colleges,
request,
search_fields=["created_by__firstname"],
sort_fields={"created_by": "created_by__firstname"},
search_fields=[""],
sort_fields=None,
)
serializer = CollegeListSerializer(
paginated_queryset.get("queryset"), many=True,context={"leads":leads}
paginated_queryset.get("queryset"), many=True
)

return CustomResponse().paginated_response(
data=serializer.data, pagination=paginated_queryset.get("pagination")
)

def post(self, request):
request_data = request.data
request_data["created_by"] = request_data[
"updated_by"
] = JWTUtils.fetch_user_id(request)

serializer = CollegeCreateDeleteSerializer(data=request_data)
if serializer.is_valid():
serializer.save()
return CustomResponse(serializer.data).get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()


class CollegeUpdateDeleteApi(APIView):
def delete(self, request, college_id):
if college := College.objects.filter(id=college_id).first():
college.delete()
return CustomResponse(
general_message="College succesfully deleted"
).get_success_response()
return CustomResponse(general_message="Invalid college").get_failure_response()

def patch(self, request, college_id):
college = College.objects.filter(id=college_id).first()
request_data = request.data
request_data["updated_by"] = JWTUtils.fetch_user_id(request)
if college is None:
return CustomResponse(
general_message="Invalid college"
).get_failure_response()
serializer = CollegeEditSerializer(college, data=request_data, partial=True)
if serializer.is_valid():
serializer.save()
return CustomResponse(serializer.data).get_success_response()
return CustomResponse(message=serializer.errors).get_failure_response()
Loading

0 comments on commit ee7e664

Please sign in to comment.