Skip to content

Commit

Permalink
Merge pull request #1812 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
[fix] kkem
  • Loading branch information
adnankattekaden authored Dec 13, 2023
2 parents 336a332 + 9d5aa1a commit 29de893
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 63 deletions.
140 changes: 101 additions & 39 deletions api/common/common_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,41 @@ def get(self, request):
date = request.query_params.get("date")
if date:
learning_circle_count = LearningCircle.objects.filter(created_at__gt=date).count()
total_no_enrollment = UserCircleLink.objects.filter(accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
created_at__gt=date).count()
# total_no_enrollment = UserCircleLink.objects.filter(accepted=True,
# user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
# created_at__gt=date).count()
total_no_enrollment = (UserCircleLink.objects.filter(accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value,
created_at__gt=date).values(
first_name=F("user__first_name"),
last_name=F("user__last_name"),
email=F("user__email"),
muid=F("user__muid"),
circle_name=F("circle__name"),
district=F("user__user_organization_link_user__org__district__name"),
circle_ig=F("circle__ig__name"),
organisation=F("user__user_organization_link_user__org__title"),

)
.annotate(
karma_earned=Sum(
"user__karma_activity_log_user__task__karma",
filter=Q(
user__karma_activity_log_user__task__ig=F("circle__ig")
),
),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F(
"user__integration_authorization_user__additional_field"
),
),
default=Value(None, output_field=CharField()),
output_field=CharField(),
),
)
).count()
user_circle_link_count = UserCircleLink.objects.filter(created_at__gt=date, circle=OuterRef('pk')).values(
'circle_id').annotate(
total_users=Count('id')).values('total_users')
Expand All @@ -51,9 +83,39 @@ def get(self, request):
)
else:
learning_circle_count = LearningCircle.objects.all().count()
total_no_enrollment = UserCircleLink.objects.filter(accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).count()
# total_no_enrollment = UserCircleLink.objects.filter(accepted=True,
# user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).count()
total_no_enrollment = (UserCircleLink.objects.filter(accepted=True,
user__user_organization_link_user__org__org_type=OrganizationType.COLLEGE.value).values(
first_name=F("user__first_name"),
last_name=F("user__last_name"),
email=F("user__email"),
muid=F("user__muid"),
circle_name=F("circle__name"),
district=F("user__user_organization_link_user__org__district__name"),
circle_ig=F("circle__ig__name"),
organisation=F("user__user_organization_link_user__org__title"),

)
.annotate(
karma_earned=Sum(
"user__karma_activity_log_user__task__karma",
filter=Q(
user__karma_activity_log_user__task__ig=F("circle__ig")
),
),
dwms_id=Case(
When(
user__integration_authorization_user__integration__name=IntegrationType.KKEM.value,
then=F(
"user__integration_authorization_user__additional_field"
),
),
default=Value(None, output_field=CharField()),
output_field=CharField(),
),
)
).count()
user_circle_link_count = UserCircleLink.objects.filter(circle=OuterRef('pk')).values('circle_id').annotate(
total_users=Count('id')).values('total_users')

Expand Down Expand Up @@ -213,40 +275,6 @@ def get(self, request):
return CommonUtils.generate_csv(student_info_data, "Learning Circle Report")


class CollegeWiseLcReportCSV(APIView):
def get(self, request):
learning_circle_count_subquery = (
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(learning_circle_count=Count("id"))
.filter(org_title=OuterRef("org_title"))
.values("learning_circle_count")
[:1]
)

learning_circles_info = (
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(
learning_circle_count=Subquery(learning_circle_count_subquery),
user_count=Count("user_circle_link_circle__user"),
)
.order_by("org_title")
)
paginated_queryset = CommonUtils.get_paginated_queryset(
learning_circles_info,
request,
search_fields=["org_title", "learning_circle_count", "user_count"],
sort_fields={"org_title": "org_title", "learning_circle_count": "learning_circle_count",
"user_count": "user_count"},
is_pagination=False
)

lc_report = CollegeInfoSerializer(paginated_queryset, many=True).data

return CommonUtils.generate_csv(lc_report, "Learning Circle Report")


class CollegeWiseLcReport(APIView):
def get(self, request):
date = request.query_params.get('date')
Expand Down Expand Up @@ -304,6 +332,40 @@ def get(self, request):
)


class CollegeWiseLcReportCSV(APIView):
def get(self, request):
learning_circle_count_subquery = (
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(learning_circle_count=Count("id"))
.filter(org_title=OuterRef("org_title"))
.values("learning_circle_count")
[:1]
)

learning_circles_info = (
LearningCircle.objects.filter(org__org_type=OrganizationType.COLLEGE.value)
.values(org_title=F("org__title"))
.annotate(
learning_circle_count=Subquery(learning_circle_count_subquery),
user_count=Count("user_circle_link_circle__user"),
)
.order_by("org_title")
)
paginated_queryset = CommonUtils.get_paginated_queryset(
learning_circles_info,
request,
search_fields=["org_title", "learning_circle_count", "user_count"],
sort_fields={"org_title": "org_title", "learning_circle_count": "learning_circle_count",
"user_count": "user_count"},
is_pagination=False
)

lc_report = CollegeInfoSerializer(paginated_queryset, many=True).data

return CommonUtils.generate_csv(lc_report, "Learning Circle Report")


class LearningCircleEnrollment(APIView):

def get(self, request):
Expand Down
42 changes: 19 additions & 23 deletions api/dashboard/profile/profile_serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import uuid

from decouple import config as decouple_config
from django.core.files.storage import FileSystemStorage
from django.db import transaction
from django.db.models import F, Sum, Q
from rest_framework import serializers
Expand Down Expand Up @@ -130,7 +129,7 @@ def get_rank(self, obj):
Wallet.objects.filter(karma__gte=user_karma)
.exclude(
Q(user__user_role_link_user__role__title__in=[
RoleType.ENABLER.value, RoleType.MENTOR.value])
RoleType.ENABLER.value, RoleType.MENTOR.value])
).order_by('-karma')
)

Expand All @@ -154,14 +153,14 @@ def get_interest_groups(self, obj):
if KarmaActivityLog.objects.filter(
task__ig=ig_link.ig, user=obj, appraiser_approved=True
)
.aggregate(Sum("karma"))
.get("karma__sum")
is None
.aggregate(Sum("karma"))
.get("karma__sum")
is None
else KarmaActivityLog.objects.filter(
task__ig=ig_link.ig, user=obj, appraiser_approved=True
)
.aggregate(Sum("karma"))
.get("karma__sum")
.aggregate(Sum("karma"))
.get("karma__sum")
)
interest_groups.append(
{"id": ig_link.ig.id, "name": ig_link.ig.name, "karma": total_ig_karma}
Expand Down Expand Up @@ -219,35 +218,32 @@ def get_role(self, obj):
return ["Learner"] if len(roles) == 0 else roles

def get_rank(self, obj):
roles = self.context.get("roles")
roles = self.get_roles(obj)
user_karma = obj.wallet_user.karma
if RoleType.MENTOR.value in roles:
ranks = Wallet.objects.filter(
user__user_role_link_user__verified=True,
user__user_role_link_user__role__title=RoleType.MENTOR.value,
karma__gte=user_karma,
).count()
).order_by('-karma', '-updated_at', 'created_at')
elif RoleType.ENABLER.value in roles:
ranks = Wallet.objects.filter(
user__user_role_link_user__verified=True,
user__user_role_link_user__role__title=RoleType.ENABLER.value,
karma__gte=user_karma,
).count()
).order_by('-karma', '-updated_at', 'created_at')
else:
ranks = (
Wallet.objects.filter(
karma__gte=user_karma, user__user_role_link_user__verified=True)
Wallet.objects.filter(karma__gte=user_karma)
.exclude(
Q(
user__user_role_link_user__role__title__in=[
RoleType.ENABLER.value,
RoleType.MENTOR.value,
]
)
)
.count()
Q(user__user_role_link_user__role__title__in=[
RoleType.ENABLER.value, RoleType.MENTOR.value])
).order_by('-karma')
)
return ranks if ranks > 0 else None

for count, _rank in enumerate(ranks, start=1):
if obj == _rank.user:
return count

def get_karma(self, obj):
return total_karma.karma if (total_karma := obj.wallet_user) else None
Expand Down Expand Up @@ -402,7 +398,7 @@ def create_karma_activity_log(task_title, karma_value):
dl = WebHookActions.SEPARATOR.value
discord_id = User.objects.get(id=user_id).discord_id
value = f"{task.hashtag}{dl}{karma_value}{dl}{discord_id}{dl}{karma_log.id}"

DiscordWebhooks.general_updates(
WebHookCategory.KARMA_INFO.value,
WebHookActions.UPDATE.value,
Expand All @@ -411,7 +407,7 @@ def create_karma_activity_log(task_title, karma_value):
else:
KarmaActivityLog.objects.filter(
task_id=task.id, user_id=user_id
).delete()
).first().delete()
Wallet.objects.filter(user_id=user_id).update(
karma=F("karma") + karma_value,
updated_by_id=user_id
Expand Down
2 changes: 1 addition & 1 deletion api/url_shortener/url_shortener_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def post(self, request):
[RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value]
)
def get(self, request):
url_shortener_objects = UrlShortener.objects.all()
url_shortener_objects = UrlShortener.objects.all().order_by('-created_at')

paginated_queryset = CommonUtils.get_paginated_queryset(
url_shortener_objects,
Expand Down

0 comments on commit 29de893

Please sign in to comment.