From eb4ec4909e8982f4c75de9d8deb273cab1005674 Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Mon, 15 Jul 2024 17:48:32 +0500 Subject: [PATCH 1/3] 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 6abfc81e75dd..5e217287d217 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -37,6 +37,8 @@ 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.permissions import SAFE_METHODS 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 @@ -1501,29 +1503,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), 'failed-validation') - 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 fc9d5a342773..0c7c0e6bc378 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'), From 027ecd66207889ef6d5e2e4606828e8ea4cd6213 Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Fri, 16 Aug 2024 12:35:19 +0500 Subject: [PATCH 2/3] feat: upgrading simple api to drf compatible. --- lms/djangoapps/instructor/views/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index 5e217287d217..a1c81de4566b 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -38,7 +38,6 @@ 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.permissions import SAFE_METHODS 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 @@ -1529,7 +1528,7 @@ def post(self, request, course_id): 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), 'failed-validation') + raise self.api_error(status.HTTP_400_BAD_REQUEST, str(e), 'Requested task is already running') return JsonResponse({"status": success_status}) From 4a753f7cc03683669d50068556fe0a0e294a723a Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Fri, 16 Aug 2024 15:52:59 +0500 Subject: [PATCH 3/3] feat: upgrading simple api to drf compatible. --- lms/djangoapps/instructor/views/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index a1c81de4566b..0b3121abc94b 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -1535,6 +1535,7 @@ def post(self, request, course_id): def get(self, request, *args, **kwargs): raise MethodNotAllowed('GET') + def _cohorts_csv_validator(file_storage, file_to_validate): """ Verifies that the expected columns are present in the CSV used to add users to cohorts.