From a66288f713f797b37c130be10c47170c76b75f69 Mon Sep 17 00:00:00 2001 From: KyryloKireiev Date: Fri, 21 Jun 2024 18:30:47 +0300 Subject: [PATCH] refactor: [AXM-549] Refactor UserEnrollmentsStatus API --- lms/djangoapps/mobile_api/users/tests.py | 2 +- lms/djangoapps/mobile_api/users/views.py | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/lms/djangoapps/mobile_api/users/tests.py b/lms/djangoapps/mobile_api/users/tests.py index 4ebda3ecd47f..df7c4382971e 100644 --- a/lms/djangoapps/mobile_api/users/tests.py +++ b/lms/djangoapps/mobile_api/users/tests.py @@ -773,7 +773,7 @@ def test_discussion_tab_url(self, discussion_tab_enabled): @ddt.ddt -class UserEnrollmentsStatus(MobileAPITestCase, MobileAuthUserTestMixin): +class TestUserEnrollmentsStatus(MobileAPITestCase, MobileAuthUserTestMixin): """ Tests for /api/mobile/{api_version}/users//enrollments_status/ """ diff --git a/lms/djangoapps/mobile_api/users/views.py b/lms/djangoapps/mobile_api/users/views.py index 0be90f255cb2..03149157811a 100644 --- a/lms/djangoapps/mobile_api/users/views.py +++ b/lms/djangoapps/mobile_api/users/views.py @@ -14,7 +14,7 @@ from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user from django.contrib.auth.signals import user_logged_in from django.db import transaction -from django.shortcuts import redirect +from django.shortcuts import get_object_or_404, redirect from django.utils import dateparse from django.utils.decorators import method_decorator from opaque_keys import InvalidKeyError @@ -416,14 +416,6 @@ def my_user_info(request, api_version): return redirect("user-detail", api_version=api_version, username=request.user.username) -class UserCourseEnrollmentsV4Pagination(DefaultPagination): - """ - Pagination for `UserCourseEnrollments` API v4. - """ - page_size = 5 - max_page_size = 50 - - @mobile_view(is_user=True) class UserEnrollmentsStatus(views.APIView): """ @@ -503,13 +495,11 @@ def _build_enrollments_status_dict( """ Builds list with dictionaries with user's enrolments statuses. """ - user_enrollments = CourseEnrollment.objects.filter( - user__username=username, - is_active=True, - ) + user = get_object_or_404(User, username=username) + user_enrollments = CourseEnrollment.enrollments_for_user(user).select_related('course') mobile_available = [ enrollment for enrollment in user_enrollments - if is_mobile_available_for_user(self.request.user, enrollment.course_overview) + if is_mobile_available_for_user(user, enrollment.course_overview) ] enrollments_status = [] for user_enrollment in mobile_available: