-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nelc/shadinaif/courses-apis
feat: course details and course statuses APIs
- Loading branch information
Showing
44 changed files
with
1,015 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""One-line description for README and other doc files.""" | ||
|
||
__version__ = '0.1.1' | ||
__version__ = '0.2.1' |
107 changes: 107 additions & 0 deletions
107
futurex_openedx_extensions/dashboard/details/courses.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""Courses details collectors""" | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from common.djangoapps.student.models import CourseAccessRole | ||
from django.db.models import Count, Exists, IntegerField, OuterRef, Q, Subquery, Sum | ||
from django.db.models.functions import Coalesce | ||
from django.db.models.query import QuerySet | ||
from eox_nelp.course_experience.models import FeedbackCourse | ||
from lms.djangoapps.certificates.models import GeneratedCertificate | ||
|
||
from futurex_openedx_extensions.helpers.querysets import get_base_queryset_courses | ||
from futurex_openedx_extensions.helpers.tenants import get_course_org_filter_list, get_tenant_site | ||
|
||
|
||
def get_courses_queryset( | ||
tenant_ids: List, search_text: str = None, only_visible: bool = True, only_active: bool = False | ||
) -> QuerySet: | ||
""" | ||
Get the courses queryset for the given tenant IDs and search text. | ||
:param tenant_ids: List of tenant IDs to get the courses for | ||
:type tenant_ids: List | ||
:param search_text: Search text to filter the courses by | ||
:type search_text: str | ||
:param only_visible: Whether to only include courses that are visible in the catalog | ||
:type only_visible: bool | ||
:param only_active: Whether to only include active courses | ||
:type only_active: bool | ||
:return: QuerySet of courses | ||
:rtype: QuerySet | ||
""" | ||
course_org_filter_list = get_course_org_filter_list(tenant_ids)['course_org_filter_list'] | ||
tenant_sites = [] | ||
for tenant_id in tenant_ids: | ||
if site := get_tenant_site(tenant_id): | ||
tenant_sites.append(site) | ||
|
||
queryset = get_base_queryset_courses(course_org_filter_list, only_visible=only_visible, only_active=only_active) | ||
|
||
search_text = (search_text or '').strip() | ||
if search_text: | ||
queryset = queryset.filter( | ||
Q(display_name__icontains=search_text) | | ||
Q(id__icontains=search_text), | ||
) | ||
queryset = queryset.annotate( | ||
rating_count=Coalesce(Subquery( | ||
FeedbackCourse.objects.filter( | ||
course_id=OuterRef('id'), | ||
rating_content__isnull=False, | ||
rating_content__gt=0, | ||
).values('course_id').annotate(count=Count('id')).values('count'), | ||
output_field=IntegerField(), | ||
), 0), | ||
).annotate( | ||
rating_total=Coalesce(Subquery( | ||
FeedbackCourse.objects.filter( | ||
course_id=OuterRef('id'), | ||
rating_content__isnull=False, | ||
rating_content__gt=0, | ||
).values('course_id').annotate(total=Sum('rating_content')).values('total'), | ||
), 0), | ||
).annotate( | ||
enrolled_count=Count( | ||
'courseenrollment', | ||
filter=( | ||
Q(courseenrollment__is_active=True) & | ||
Q(courseenrollment__user__is_active=True) & | ||
Q(courseenrollment__user__is_staff=False) & | ||
Q(courseenrollment__user__is_superuser=False) & | ||
~Exists( | ||
CourseAccessRole.objects.filter( | ||
user_id=OuterRef('courseenrollment__user_id'), | ||
org=OuterRef('org'), | ||
), | ||
) | ||
), | ||
) | ||
).annotate( | ||
active_count=Count( | ||
'courseenrollment', | ||
filter=( | ||
Q(courseenrollment__is_active=True) & | ||
Q(courseenrollment__user__is_active=True) & | ||
Q(courseenrollment__user__is_staff=False) & | ||
Q(courseenrollment__user__is_superuser=False) & | ||
~Exists( | ||
CourseAccessRole.objects.filter( | ||
user_id=OuterRef('courseenrollment__user_id'), | ||
org=OuterRef('org'), | ||
), | ||
) | ||
), | ||
) | ||
).annotate( | ||
certificates_count=Coalesce(Subquery( | ||
GeneratedCertificate.objects.filter( | ||
course_id=OuterRef('id'), | ||
status='downloadable' | ||
).values('course_id').annotate(count=Count('id')).values('count'), | ||
output_field=IntegerField(), | ||
), 0), | ||
) | ||
|
||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.