Merge pull request #33294 from raccoongang/rg/feat/FC0031/add_pagination_in_user_course_enrollments

feat: [FC-0031] Add DefaultPagination in UserCourseEnrollmentsList
This commit is contained in:
Feanil Patel
2023-11-27 11:23:18 -05:00
committed by GitHub
4 changed files with 46 additions and 4 deletions

View File

@@ -34,7 +34,7 @@ from lms.djangoapps.mobile_api.testutils import (
MobileAuthUserTestMixin,
MobileCourseAccessTestMixin
)
from lms.djangoapps.mobile_api.utils import API_V1, API_V05, API_V2
from lms.djangoapps.mobile_api.utils import API_V1, API_V05, API_V2, API_V3
from openedx.core.lib.courses import course_image_url
from openedx.features.course_duration_limits.models import CourseDurationLimitConfig
from openedx.features.course_experience.tests.views.helpers import add_course_mode
@@ -376,6 +376,29 @@ class TestUserEnrollmentApi(UrlResetMixin, MobileAPITestCase, MobileAuthUserTest
self.assertDictEqual(response.data['configs'], expected_result)
assert 'enrollments' in response.data
def test_pagination_enrollment(self):
"""
Test pagination for UserCourseEnrollmentsList view v3
for 3rd version of this view we use DefaultPagination
Test for /api/mobile/{api_version}/users/<user_name>/course_enrollments/
api_version = v3
"""
self.login()
# Create and enroll to 15 courses
courses = [CourseFactory.create(org="my_org", mobile_available=True) for _ in range(15)]
for course in courses:
self.enroll(course.id)
response = self.api_response(api_version=API_V3)
assert response.status_code == 200
assert response.data["enrollments"]["count"] == 15
assert response.data["enrollments"]["num_pages"] == 2
assert response.data["enrollments"]["current_page"] == 1
assert len(response.data["enrollments"]["results"]) == 10
assert "next" in response.data["enrollments"]
assert "previous" in response.data["enrollments"]
@override_settings(MKTG_URLS={'ROOT': 'dummy-root'})
class TestUserEnrollmentCertificates(UrlResetMixin, MobileAPITestCase, MilestonesTestCaseMixin):

View File

@@ -21,6 +21,7 @@ from rest_framework.permissions import SAFE_METHODS
from rest_framework.response import Response
from xblock.fields import Scope
from xblock.runtime import KeyValueStore
from edx_rest_framework_extensions.paginators import DefaultPagination
from common.djangoapps.student.models import CourseEnrollment, User # lint-amnesty, pylint: disable=reimported
from lms.djangoapps.courseware.access import is_mobile_available_for_user
@@ -30,7 +31,7 @@ from lms.djangoapps.courseware.model_data import FieldDataCache
from lms.djangoapps.courseware.block_render import get_block_for_descriptor
from lms.djangoapps.courseware.views.index import save_positions_recursively_up
from lms.djangoapps.mobile_api.models import MobileConfig
from lms.djangoapps.mobile_api.utils import API_V1, API_V05, API_V2
from lms.djangoapps.mobile_api.utils import API_V1, API_V05, API_V2, API_V3
from openedx.features.course_duration_limits.access import check_course_expired
from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.exceptions import ItemNotFoundError # lint-amnesty, pylint: disable=wrong-import-order
@@ -372,7 +373,7 @@ class UserCourseEnrollmentsList(generics.ListAPIView):
response = super().list(request, *args, **kwargs)
api_version = self.kwargs.get('api_version')
if api_version == API_V2:
if api_version in (API_V2, API_V3):
enrollment_data = {
'configs': MobileConfig.get_structured_configs(),
'enrollments': response.data
@@ -381,6 +382,23 @@ class UserCourseEnrollmentsList(generics.ListAPIView):
return response
# pylint: disable=attribute-defined-outside-init
@property
def paginator(self):
"""
Override API View paginator property to dynamically determine pagination class
Implements solutions from the discussion at
https://www.github.com/encode/django-rest-framework/issues/6397.
"""
super().paginator # pylint: disable=expression-not-assigned
api_version = self.kwargs.get('api_version')
if self._paginator is None and api_version == API_V3:
self._paginator = DefaultPagination()
return self._paginator
@api_view(["GET"])
@mobile_view()

View File

@@ -5,6 +5,7 @@ Common utility methods for Mobile APIs.
API_V05 = 'v0.5'
API_V1 = 'v1'
API_V2 = 'v2'
API_V3 = 'v3'
def parsed_version(version):

View File

@@ -218,7 +218,7 @@ urlpatterns = [
if settings.FEATURES.get('ENABLE_MOBILE_REST_API'):
urlpatterns += [
re_path(r'^api/mobile/(?P<api_version>v(2|1|0.5))/', include('lms.djangoapps.mobile_api.urls')),
re_path(r'^api/mobile/(?P<api_version>v(3|2|1|0.5))/', include('lms.djangoapps.mobile_api.urls')),
]
urlpatterns += [