|
| 1 | +""" |
| 2 | +API Views for course grading settings — v3. |
| 3 | +
|
| 4 | +This module is the v3 incarnation of the v0 ``AuthoringGradingView`` endpoint, |
| 5 | +restructured to apply the FC-0118 ADRs from the start: |
| 6 | +
|
| 7 | + * ADR 0025 – ``serializer_class`` on the viewset |
| 8 | + * ADR 0026 – explicit ``authentication_classes`` + ``permission_classes`` |
| 9 | + * ADR 0027 – ``drf_spectacular`` for OpenAPI schema generation |
| 10 | + * ADR 0028 – consolidated into a single DRF ``ViewSet`` registered via |
| 11 | + ``DefaultRouter`` (replaces ``AuthoringGradingView`` ``APIView``) |
| 12 | + * ADR 0029 – standardized error envelope via :class:`StandardizedErrorMixin` |
| 13 | + (v3-scoped — does not change the project-wide DRF ``EXCEPTION_HANDLER`` |
| 14 | + setting) |
| 15 | + * ADR 0033 / OEP-68 – the URL kwarg, action parameter, and OpenAPI parameter |
| 16 | + are named ``course_key`` (the OEP-68-standardized name) rather than the |
| 17 | + legacy ``course_id``. Since this is a brand-new versioned API, no |
| 18 | + deprecated alias is needed — clients on the v0 endpoint continue to use |
| 19 | + ``course_id`` there. |
| 20 | +
|
| 21 | +Permission model note: |
| 22 | + PR #38363 proposed a class-level ``HasStudioReadAccess`` permission. The |
| 23 | + current v0 view has since evolved to use the ``openedx_authz`` permission |
| 24 | + framework (``COURSES_EDIT_GRADING_SETTINGS``), which is more specific to |
| 25 | + grading and aligns with the platform-wide authz direction. |
| 26 | +
|
| 27 | + The v3 viewset preserves the openedx_authz model via an *inline* |
| 28 | + ``user_has_course_permission`` check inside the action body (rather than |
| 29 | + the ``@authz_permission_required`` decorator). The decorator raises |
| 30 | + ``DeveloperErrorResponseException`` — a plain ``Exception`` subclass that |
| 31 | + does not flow through DRF's exception handler, so it would bypass |
| 32 | + :class:`StandardizedErrorMixin` and surface as an unstructured 500. |
| 33 | + Raising ``rest_framework.exceptions.PermissionDenied`` directly keeps the |
| 34 | + ADR 0029 envelope intact. |
| 35 | +""" |
| 36 | + |
| 37 | +from drf_spectacular.utils import OpenApiParameter, OpenApiRequest, OpenApiResponse, extend_schema |
| 38 | +from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication |
| 39 | +from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser |
| 40 | +from openedx_authz.constants.permissions import COURSES_EDIT_GRADING_SETTINGS |
| 41 | +from rest_framework import viewsets |
| 42 | +from rest_framework.exceptions import PermissionDenied |
| 43 | +from rest_framework.permissions import IsAuthenticated |
| 44 | +from rest_framework.request import Request |
| 45 | +from rest_framework.response import Response |
| 46 | + |
| 47 | +from cms.djangoapps.contentstore.rest_api.v0.serializers import CourseGradingModelSerializer |
| 48 | +from cms.djangoapps.contentstore.rest_api.v3.utils import COMMON_ERROR_RESPONSES, resolve_course_key |
| 49 | +from cms.djangoapps.models.settings.course_grading import CourseGradingModel |
| 50 | +from openedx.core.djangoapps.authz.constants import LegacyAuthoringPermission |
| 51 | +from openedx.core.djangoapps.authz.decorators import user_has_course_permission |
| 52 | +from openedx.core.djangoapps.credit.tasks import update_credit_course_requirements |
| 53 | +from openedx.core.lib.api.authentication import BearerAuthenticationAllowInactiveUser |
| 54 | +from openedx.core.lib.api.mixins import StandardizedErrorMixin |
| 55 | + |
| 56 | +_COURSE_KEY_PARAMETER = OpenApiParameter( |
| 57 | + name="course_key", |
| 58 | + description="OEP-68 course key (e.g. course-v1:org+course+run).", |
| 59 | + required=True, |
| 60 | + type=str, |
| 61 | + location=OpenApiParameter.PATH, |
| 62 | +) |
| 63 | + |
| 64 | + |
| 65 | +class AuthoringGradingViewSet(StandardizedErrorMixin, viewsets.ViewSet): |
| 66 | + """ |
| 67 | + ViewSet for course grading settings (v3). Registered via DefaultRouter |
| 68 | + (basename ``authoring_grading``). |
| 69 | +
|
| 70 | + Router-generated URL:: |
| 71 | +
|
| 72 | + PATCH /api/contentstore/v3/authoring_grading/{course_key}/ → partial_update |
| 73 | +
|
| 74 | + Supersedes ``AuthoringGradingView`` at ``POST /api/contentstore/v0/grading/{course_id}``. |
| 75 | + """ |
| 76 | + |
| 77 | + authentication_classes = ( |
| 78 | + JwtAuthentication, |
| 79 | + BearerAuthenticationAllowInactiveUser, |
| 80 | + SessionAuthenticationAllowInactiveUser, |
| 81 | + ) |
| 82 | + permission_classes = (IsAuthenticated,) |
| 83 | + serializer_class = CourseGradingModelSerializer |
| 84 | + |
| 85 | + # DefaultRouter lookup: matches course-v1:org+course+run (+ or / separators). |
| 86 | + # OEP-68: the kwarg name is ``course_key`` (not the legacy ``course_id``). |
| 87 | + lookup_field = "course_key" |
| 88 | + lookup_value_regex = r"[^/+]+(?:/|\+)[^/+]+(?:/|\+)[^/?]+" |
| 89 | + |
| 90 | + def get_serializer(self, *args, **kwargs): |
| 91 | + """Instantiate and return the configured serializer class.""" |
| 92 | + return self.serializer_class(*args, **kwargs) |
| 93 | + |
| 94 | + @extend_schema( |
| 95 | + summary="Update a course's grading settings", |
| 96 | + description="Partially update the grading settings for the specified course.", |
| 97 | + request=OpenApiRequest(request=CourseGradingModelSerializer), |
| 98 | + parameters=[_COURSE_KEY_PARAMETER], |
| 99 | + responses={ |
| 100 | + 200: OpenApiResponse( |
| 101 | + response=CourseGradingModelSerializer, |
| 102 | + description="Grading settings updated successfully.", |
| 103 | + ), |
| 104 | + **COMMON_ERROR_RESPONSES, |
| 105 | + }, |
| 106 | + ) |
| 107 | + def partial_update(self, request: Request, course_key: str): |
| 108 | + """ |
| 109 | + Update a course's grading settings. |
| 110 | +
|
| 111 | + **Example Request** |
| 112 | +
|
| 113 | + PATCH /api/contentstore/v3/authoring_grading/{course_key}/ |
| 114 | +
|
| 115 | + **PATCH Parameters** |
| 116 | +
|
| 117 | + The request body should follow the ``CourseGradingModelSerializer`` |
| 118 | + schema. Example:: |
| 119 | +
|
| 120 | + { |
| 121 | + "graders": [ |
| 122 | + { |
| 123 | + "type": "Homework", |
| 124 | + "min_count": 1, |
| 125 | + "drop_count": 0, |
| 126 | + "short_label": "", |
| 127 | + "weight": 100, |
| 128 | + "id": 0 |
| 129 | + } |
| 130 | + ], |
| 131 | + "grade_cutoffs": {"A": 0.75, "B": 0.63, "C": 0.57, "D": 0.5}, |
| 132 | + "grace_period": {"hours": 12, "minutes": 0}, |
| 133 | + "minimum_grade_credit": 0.7, |
| 134 | + "is_credit_course": true |
| 135 | + } |
| 136 | +
|
| 137 | + **Response Values** |
| 138 | +
|
| 139 | + If the request is successful, an HTTP 200 "OK" response is returned |
| 140 | + with the updated grading data serialized via |
| 141 | + :class:`CourseGradingModelSerializer`. |
| 142 | + """ |
| 143 | + parsed_course_key = resolve_course_key(course_key) |
| 144 | + |
| 145 | + # Per-action authorization (ADR 0026): kept inline rather than |
| 146 | + # behind ``@authz_permission_required`` because that decorator |
| 147 | + # raises ``DeveloperErrorResponseException`` (not a DRF exception), |
| 148 | + # which bypasses :class:`StandardizedErrorMixin`. Raising |
| 149 | + # ``PermissionDenied`` directly flows through the ADR 0029 envelope. |
| 150 | + if not user_has_course_permission( |
| 151 | + request.user, |
| 152 | + COURSES_EDIT_GRADING_SETTINGS.identifier, |
| 153 | + parsed_course_key, |
| 154 | + LegacyAuthoringPermission.READ, |
| 155 | + ): |
| 156 | + raise PermissionDenied("You do not have permission to perform this action.") |
| 157 | + |
| 158 | + if "minimum_grade_credit" in request.data: |
| 159 | + update_credit_course_requirements.delay(str(parsed_course_key)) |
| 160 | + |
| 161 | + updated_data = CourseGradingModel.update_from_json(parsed_course_key, request.data, request.user) |
| 162 | + serializer = self.get_serializer(updated_data) |
| 163 | + return Response(serializer.data) |
0 commit comments