|
| 1 | +"""HomeCoursesViewSet for getting courses available to the logged-in user (v4).""" |
| 2 | + |
| 3 | +from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema |
| 4 | +from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication |
| 5 | +from edx_rest_framework_extensions.auth.session.authentication import ( |
| 6 | + SessionAuthenticationAllowInactiveUser, |
| 7 | +) |
| 8 | +from edx_rest_framework_extensions.paginators import DefaultPagination |
| 9 | +from rest_framework import viewsets |
| 10 | +from rest_framework.permissions import IsAuthenticated |
| 11 | +from rest_framework.request import Request |
| 12 | +from rest_framework.response import Response |
| 13 | + |
| 14 | +from cms.djangoapps.contentstore.rest_api.v4.serializers.home import ( |
| 15 | + CourseHomeTabSerializerV4, |
| 16 | +) |
| 17 | +from cms.djangoapps.contentstore.utils import get_course_context_v2 |
| 18 | + |
| 19 | + |
| 20 | +class HomePageCoursesPaginator(DefaultPagination): |
| 21 | + """ |
| 22 | + ADR 0032 - standard pagination for the Studio home courses list (v4). |
| 23 | +
|
| 24 | + Extends ``DefaultPagination`` (edx-rest-framework-extensions) which |
| 25 | + provides the 7-field response envelope: |
| 26 | + ``count``, ``num_pages``, ``current_page``, ``start``, |
| 27 | + ``next``, ``previous``, ``results``. |
| 28 | +
|
| 29 | + Overrides ``paginate_queryset`` to handle ``filter`` objects returned |
| 30 | + by ``get_course_context_v2``. |
| 31 | + """ |
| 32 | + |
| 33 | + page_size_query_param = "page_size" |
| 34 | + |
| 35 | + def paginate_queryset(self, queryset, request, view=None): |
| 36 | + """ |
| 37 | + Paginate a queryset, converting ``filter`` objects to lists first. |
| 38 | +
|
| 39 | + ``get_course_context_v2`` may return a ``filter`` object; the base |
| 40 | + ``PageNumberPagination`` cannot measure its length without materialising |
| 41 | + it first, so we do that here. |
| 42 | + """ |
| 43 | + if isinstance(queryset, filter): |
| 44 | + queryset = list(queryset) |
| 45 | + return super().paginate_queryset(queryset, request, view) |
| 46 | + |
| 47 | + |
| 48 | +def _query_param( |
| 49 | + name: str, description: str, deprecated: bool = False |
| 50 | +) -> OpenApiParameter: |
| 51 | + """Build a string-typed, optional query parameter for OpenAPI docs.""" |
| 52 | + return OpenApiParameter( |
| 53 | + name=name, |
| 54 | + description=description, |
| 55 | + required=False, |
| 56 | + type=str, |
| 57 | + location=OpenApiParameter.QUERY, |
| 58 | + deprecated=deprecated, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +_HOME_COURSES_QUERY_PARAMETERS = [ |
| 63 | + _query_param("org", "Filter by course org"), |
| 64 | + _query_param("search", "Filter by course name, org, or number"), |
| 65 | + _query_param( |
| 66 | + "ordering", |
| 67 | + "Order by course field: display_name, org, number, or run (ADR 0033 standard parameter).", |
| 68 | + ), |
| 69 | + _query_param( |
| 70 | + "order", |
| 71 | + "Deprecated alias for 'ordering' (ADR 0033). Use 'ordering' instead.", |
| 72 | + deprecated=True, |
| 73 | + ), |
| 74 | + _query_param("active_only", "Filter to active courses only"), |
| 75 | + _query_param("archived_only", "Filter to archived courses only"), |
| 76 | + _query_param("page", "Page number for pagination"), |
| 77 | + _query_param("page_size", "Number of courses per page (default 10, max 100)"), |
| 78 | +] |
| 79 | + |
| 80 | +_UNAUTHENTICATED_RESPONSE = OpenApiResponse( |
| 81 | + description="The requester is not authenticated." |
| 82 | +) |
| 83 | + |
| 84 | +# ADR 0033: emitted as an HTTP ``Deprecation`` header when the legacy ``order`` |
| 85 | +# parameter is used instead of the DRF-standard ``ordering``. |
| 86 | +_LEGACY_ORDER_DEPRECATION_HEADER = ( |
| 87 | + "Parameter 'order' is deprecated. Use 'ordering' instead. " |
| 88 | + "Support will be removed in release '<release_name>'." |
| 89 | +) |
| 90 | + |
| 91 | + |
| 92 | +def _maybe_set_legacy_order_deprecation_header( |
| 93 | + request: Request, response: Response |
| 94 | +) -> Response: |
| 95 | + """Set the ADR 0033 Deprecation header when the legacy ``order`` parameter is used.""" |
| 96 | + if "order" in request.query_params: |
| 97 | + response["Deprecation"] = _LEGACY_ORDER_DEPRECATION_HEADER |
| 98 | + return response |
| 99 | + |
| 100 | + |
| 101 | +class HomeCoursesViewSet(viewsets.ViewSet): |
| 102 | + """ |
| 103 | + ViewSet for course listing (v4). Registered via DefaultRouter (basename ``home-courses``). |
| 104 | +
|
| 105 | + Router-generated URLs:: |
| 106 | +
|
| 107 | + GET /api/contentstore/v4/home/courses/ → list |
| 108 | +
|
| 109 | + Supersedes ``HomePageCoursesViewV2`` at ``/api/contentstore/v2/home/courses``. |
| 110 | +
|
| 111 | + ADR compliance: |
| 112 | + - 0025: ``serializer_class`` attribute for schema generation |
| 113 | + - 0026: explicit ``authentication_classes`` and ``permission_classes`` |
| 114 | + - 0027: ``drf_spectacular`` for OpenAPI documentation |
| 115 | + - 0028: ViewSet with DefaultRouter registration |
| 116 | + - 0032: 7-field pagination envelope via ``DefaultPagination`` |
| 117 | + - 0033: ``ordering`` parameter; ``order`` kept as deprecated alias |
| 118 | + """ |
| 119 | + |
| 120 | + authentication_classes = (JwtAuthentication, SessionAuthenticationAllowInactiveUser) |
| 121 | + permission_classes = (IsAuthenticated,) |
| 122 | + serializer_class = CourseHomeTabSerializerV4 |
| 123 | + |
| 124 | + def get_exception_handler(self): |
| 125 | + """Return the ADR 0029 standardized error handler for this viewset.""" |
| 126 | + from openedx.core.lib.api.exceptions import standardized_error_exception_handler |
| 127 | + return standardized_error_exception_handler |
| 128 | + |
| 129 | + def get_serializer(self, *args, **kwargs): |
| 130 | + """Instantiate and return the configured serializer class.""" |
| 131 | + return self.serializer_class(*args, **kwargs) |
| 132 | + |
| 133 | + @extend_schema( |
| 134 | + summary="List courses for the Studio home page (paginated)", |
| 135 | + description=( |
| 136 | + "Returns a paginated list of all courses available to the logged-in user, " |
| 137 | + "with optional filtering and ordering. " |
| 138 | + "Supersedes ``GET /api/contentstore/v2/home/courses``." |
| 139 | + ), |
| 140 | + parameters=_HOME_COURSES_QUERY_PARAMETERS, |
| 141 | + responses={ |
| 142 | + 200: OpenApiResponse( |
| 143 | + response=CourseHomeTabSerializerV4, |
| 144 | + description="Paginated course list retrieved successfully.", |
| 145 | + ), |
| 146 | + 401: _UNAUTHENTICATED_RESPONSE, |
| 147 | + }, |
| 148 | + ) |
| 149 | + def list(self, request: Request): |
| 150 | + """ |
| 151 | + Get a paginated list of all courses available to the logged-in user. |
| 152 | +
|
| 153 | + **Example Request** |
| 154 | +
|
| 155 | + GET /api/contentstore/v4/home/courses/ |
| 156 | + GET /api/contentstore/v4/home/courses/?org=edX |
| 157 | + GET /api/contentstore/v4/home/courses/?search=E2E |
| 158 | + GET /api/contentstore/v4/home/courses/?ordering=-org |
| 159 | + GET /api/contentstore/v4/home/courses/?order=-org |
| 160 | + GET /api/contentstore/v4/home/courses/?active_only=true |
| 161 | + GET /api/contentstore/v4/home/courses/?archived_only=true |
| 162 | + GET /api/contentstore/v4/home/courses/?page=2 |
| 163 | + GET /api/contentstore/v4/home/courses/?page_size=20 |
| 164 | +
|
| 165 | + **Pagination Parameters** |
| 166 | +
|
| 167 | + - ``page`` (int): Page number to retrieve. Default is 1. |
| 168 | + - ``page_size`` (int): Items per page. Default is 10, max is 100. |
| 169 | +
|
| 170 | + **Response Envelope (ADR 0032)** |
| 171 | +
|
| 172 | + - ``count`` (int): Total number of courses matching the filters. |
| 173 | + - ``num_pages`` (int): Total number of pages. |
| 174 | + - ``current_page`` (int): The current page number. |
| 175 | + - ``start`` (int): The 0-based index of the first course on this page. |
| 176 | + - ``next`` (str|null): URL for the next page, or null on the last page. |
| 177 | + - ``previous`` (str|null): URL for the previous page, or null on the first page. |
| 178 | + - ``results`` (dict): Course data for the current page. |
| 179 | +
|
| 180 | + **Example Response** |
| 181 | +
|
| 182 | + ```json |
| 183 | + { |
| 184 | + "count": 1, |
| 185 | + "num_pages": 1, |
| 186 | + "current_page": 1, |
| 187 | + "start": 0, |
| 188 | + "next": null, |
| 189 | + "previous": null, |
| 190 | + "results": { |
| 191 | + "courses": [ |
| 192 | + { |
| 193 | + "course_key": "course-v1:edX+E2E-101+course", |
| 194 | + "display_name": "E2E Test Course", |
| 195 | + "lms_link": "//localhost:18000/courses/course-v1:edX+E2E-101+course", |
| 196 | + "cms_link": "//localhost:18010/course/course-v1:edX+E2E-101+course", |
| 197 | + "number": "E2E-101", |
| 198 | + "org": "edX", |
| 199 | + "rerun_link": "/course_rerun/course-v1:edX+E2E-101+course", |
| 200 | + "run": "course", |
| 201 | + "url": "/course/course-v1:edX+E2E-101+course", |
| 202 | + "is_active": true |
| 203 | + } |
| 204 | + ], |
| 205 | + "in_process_course_actions": [] |
| 206 | + } |
| 207 | + } |
| 208 | + ``` |
| 209 | + """ |
| 210 | + courses, in_process_course_actions = get_course_context_v2(request) |
| 211 | + paginator = HomePageCoursesPaginator() |
| 212 | + courses_page = paginator.paginate_queryset(courses, request, view=self) |
| 213 | + serializer = self.get_serializer( |
| 214 | + { |
| 215 | + "courses": courses_page, |
| 216 | + "in_process_course_actions": in_process_course_actions, |
| 217 | + } |
| 218 | + ) |
| 219 | + response = paginator.get_paginated_response(serializer.data) |
| 220 | + return _maybe_set_legacy_order_deprecation_header(request, response) |
0 commit comments