Skip to content

Commit

Permalink
refactor: [AXM-506] Refactor user's enrolments API
Browse files Browse the repository at this point in the history
  • Loading branch information
KyryloKireiev committed Jul 3, 2024
1 parent eee5e7b commit 12a6866
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
38 changes: 20 additions & 18 deletions common/djangoapps/student/models/course_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,52 @@ def active(self):
"""
return self.filter(is_active=True)

def without_certificates(self, user_username):
def without_certificates(self, username):
"""
Returns a queryset of CourseEnrollment objects for courses that do not have a certificate.
"""
from lms.djangoapps.certificates.models import GeneratedCertificate # pylint: disable=import-outside-toplevel
course_ids_with_certificates = GeneratedCertificate.objects.filter(
user__username=user_username
).values_list('course_id', flat=True)
return self.exclude(course_id__in=course_ids_with_certificates)
return self.exclude(course_id__in=self.get_user_course_ids_with_certificates(username))

def with_certificates(self, user_username):
def with_certificates(self, username):
"""
Returns a queryset of CourseEnrollment objects for courses that have a certificate.
"""
from lms.djangoapps.certificates.models import GeneratedCertificate # pylint: disable=import-outside-toplevel
course_ids_with_certificates = GeneratedCertificate.objects.filter(
user__username=user_username
).values_list('course_id', flat=True)
return self.filter(course_id__in=course_ids_with_certificates)
return self.filter(course_id__in=self.get_user_course_ids_with_certificates(username))

def in_progress(self, user_username, time_zone=UTC):
def in_progress(self, username, time_zone=UTC):
"""
Returns a queryset of CourseEnrollment objects for courses that are currently in progress.
"""
now = datetime.now(time_zone)
return self.active().without_certificates(user_username).filter(
return self.active().without_certificates(username).filter(
Q(course__start__lte=now, course__end__gte=now)
| Q(course__start__isnull=True, course__end__isnull=True)
| Q(course__start__isnull=True, course__end__gte=now)
| Q(course__start__lte=now, course__end__isnull=True),
)

def completed(self, user_username):
def completed(self, username):
"""
Returns a queryset of CourseEnrollment objects for courses that have been completed.
"""
return self.active().with_certificates(user_username)
return self.active().with_certificates(username)

def expired(self, user_username, time_zone=UTC):
def expired(self, username, time_zone=UTC):
"""
Returns a queryset of CourseEnrollment objects for courses that have expired.
"""
now = datetime.now(time_zone)
return self.active().without_certificates(user_username).filter(course__end__lt=now)
return self.active().without_certificates(username).filter(course__end__lt=now)

def get_user_course_ids_with_certificates(self, username):
"""
Gets user's course ids with certificates.
"""
from lms.djangoapps.certificates.models import GeneratedCertificate # pylint: disable=import-outside-toplevel
course_ids_with_certificates = GeneratedCertificate.objects.filter(
user__username=username
).values_list('course_id', flat=True)
return course_ids_with_certificates


class CourseEnrollmentManager(models.Manager):
Expand Down
3 changes: 2 additions & 1 deletion lms/djangoapps/courseware/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,8 @@ def get_assignments_grades(user, course_id, cache_timeout):
is_staff = bool(has_access(user, 'staff', course_id))

try:
cache_key = f'course_block_structure_{str(course_id)}_{user.id}'
course = get_course_with_access(user, 'load', course_id)
cache_key = f'course_block_structure_{str(course_id)}_{str(course.course_version)}_{user.id}'
collected_block_structure = cache.get(cache_key)
if not collected_block_structure:
collected_block_structure = get_block_structure_manager(course_id).get_collected()
Expand Down
14 changes: 7 additions & 7 deletions lms/djangoapps/mobile_api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def get_serializer_class(self):
return CourseEnrollmentSerializer

@cached_property
def queryset(self):
def queryset_for_user(self):
"""
Find and return the list of course enrollments for the user.
Expand All @@ -380,11 +380,11 @@ def queryset(self):

if api_version == API_V4 and status in EnrollmentStatuses.values():
if status == EnrollmentStatuses.IN_PROGRESS.value:
queryset = queryset.in_progress(user_username=username, time_zone=self.user_timezone)
queryset = queryset.in_progress(username=username, time_zone=self.user_timezone)
elif status == EnrollmentStatuses.COMPLETED.value:
queryset = queryset.completed(user_username=username)
queryset = queryset.completed(username=username)
elif status == EnrollmentStatuses.EXPIRED.value:
queryset = queryset.expired(user_username=username, time_zone=self.user_timezone)
queryset = queryset.expired(username=username, time_zone=self.user_timezone)

return queryset

Expand Down Expand Up @@ -417,7 +417,7 @@ def get_same_org_mobile_available_enrollments(self) -> list[CourseEnrollment]:
org = self.request.query_params.get('org', None)

same_org = (
enrollment for enrollment in self.queryset
enrollment for enrollment in self.queryset_for_user
if enrollment.course_overview and self.is_org(org, enrollment.course_overview.org)
)
mobile_available = (
Expand Down Expand Up @@ -473,7 +473,7 @@ def get_primary_enrollment_by_latest_enrollment_or_progress(self) -> Optional[Co

mobile_available_course_ids = [enrollment.course_id for enrollment in mobile_available]

latest_enrollment = self.queryset.filter(
latest_enrollment = self.queryset_for_user.filter(
course__id__in=mobile_available_course_ids
).order_by('-created').first()

Expand All @@ -488,7 +488,7 @@ def get_primary_enrollment_by_latest_enrollment_or_progress(self) -> Optional[Co
if not latest_progress:
return latest_enrollment

enrollment_with_latest_progress = self.queryset.filter(
enrollment_with_latest_progress = self.queryset_for_user.filter(
course_id=latest_progress.course_id,
user__username=self.kwargs['username'],
).first()
Expand Down

0 comments on commit 12a6866

Please sign in to comment.