Skip to content

Commit

Permalink
kkem fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adnankattekaden committed Dec 13, 2023
1 parent 9a9e244 commit 299d061
Showing 1 changed file with 101 additions and 39 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

0 comments on commit 299d061

Please sign in to comment.