From 1a431b6a26b8659ef267aa06660716e690286834 Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Thu, 29 Aug 2024 18:37:17 +0500 Subject: [PATCH] feat: get_students_who_may_enroll update api to DRF ( 8th ) (#35174) * feat: upgrading simple api to drf compatible. --- lms/djangoapps/instructor/views/api.py | 45 +++++++++++++-------- lms/djangoapps/instructor/views/api_urls.py | 2 +- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index 1aa40b5e3376..0255e26e2a65 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -37,6 +37,7 @@ from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import CourseKey, UsageKey from openedx.core.djangoapps.course_groups.cohorts import get_cohort_by_name +from rest_framework.exceptions import MethodNotAllowed from rest_framework import serializers, status # lint-amnesty, pylint: disable=wrong-import-order from rest_framework.permissions import IsAdminUser, IsAuthenticated # lint-amnesty, pylint: disable=wrong-import-order from rest_framework.response import Response # lint-amnesty, pylint: disable=wrong-import-order @@ -1510,28 +1511,38 @@ def get_students_features(request, course_id, csv=False): # pylint: disable=red return JsonResponse({"status": success_status}) -@transaction.non_atomic_requests -@require_POST -@ensure_csrf_cookie -@cache_control(no_cache=True, no_store=True, must_revalidate=True) -@require_course_permission(permissions.CAN_RESEARCH) -@common_exceptions_400 -def get_students_who_may_enroll(request, course_id): +@method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') +@method_decorator(transaction.non_atomic_requests, name='dispatch') +class GetStudentsWhoMayEnroll(DeveloperErrorViewMixin, APIView): """ Initiate generation of a CSV file containing information about - students who may enroll in a course. + """ + permission_classes = (IsAuthenticated, permissions.InstructorPermission) + permission_name = permissions.CAN_RESEARCH - Responds with JSON - {"status": "... status message ..."} + @method_decorator(ensure_csrf_cookie) + @method_decorator(transaction.non_atomic_requests) + def post(self, request, course_id): + """ + Initiate generation of a CSV file containing information about + students who may enroll in a course. - """ - course_key = CourseKey.from_string(course_id) - query_features = ['email'] - report_type = _('enrollment') - task_api.submit_calculate_may_enroll_csv(request, course_key, query_features) - success_status = SUCCESS_MESSAGE_TEMPLATE.format(report_type=report_type) + Responds with JSON + {"status": "... status message ..."} + """ + course_key = CourseKey.from_string(course_id) + query_features = ['email'] + report_type = _('enrollment') + try: + task_api.submit_calculate_may_enroll_csv(request, course_key, query_features) + success_status = SUCCESS_MESSAGE_TEMPLATE.format(report_type=report_type) + except Exception as e: + raise self.api_error(status.HTTP_400_BAD_REQUEST, str(e), 'Requested task is already running') - return JsonResponse({"status": success_status}) + return JsonResponse({"status": success_status}) + + def get(self, request, *args, **kwargs): + raise MethodNotAllowed('GET') def _cohorts_csv_validator(file_storage, file_to_validate): diff --git a/lms/djangoapps/instructor/views/api_urls.py b/lms/djangoapps/instructor/views/api_urls.py index 18da0b63b218..f25ea56c2ea7 100644 --- a/lms/djangoapps/instructor/views/api_urls.py +++ b/lms/djangoapps/instructor/views/api_urls.py @@ -30,7 +30,7 @@ path('get_grading_config', api.get_grading_config, name='get_grading_config'), re_path(r'^get_students_features(?P/csv)?$', api.get_students_features, name='get_students_features'), path('get_issued_certificates/', api.get_issued_certificates, name='get_issued_certificates'), - path('get_students_who_may_enroll', api.get_students_who_may_enroll, name='get_students_who_may_enroll'), + path('get_students_who_may_enroll', api.GetStudentsWhoMayEnroll.as_view(), name='get_students_who_may_enroll'), path('get_anon_ids', api.get_anon_ids, name='get_anon_ids'), path('get_student_enrollment_status', api.get_student_enrollment_status, name="get_student_enrollment_status"), path('get_student_progress_url', api.StudentProgressUrl.as_view(), name='get_student_progress_url'),