From 8ea2d1030ebc76f7990b5535490136b4c2439b7c Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Mon, 25 Nov 2024 16:04:59 +0500 Subject: [PATCH] feat!: upgrade get_issued_certificates to DRF ( 24 ) (#35534) * feat!: upgrading api to DRF. --- lms/djangoapps/instructor/views/api.py | 85 ++++++++++++--------- lms/djangoapps/instructor/views/api_urls.py | 2 +- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index f27ffc32dbdb..d409a88c8d06 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -1384,44 +1384,59 @@ def post(self, request, course_id): return JsonResponse(response_payload) -@transaction.non_atomic_requests -@ensure_csrf_cookie -@cache_control(no_cache=True, no_store=True, must_revalidate=True) -@require_course_permission(permissions.VIEW_ISSUED_CERTIFICATES) -def get_issued_certificates(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 GetIssuedCertificates(APIView): """ Responds with JSON if CSV is not required. contains a list of issued certificates. - Arguments: - course_id - Returns: - {"certificates": [{course_id: xyz, mode: 'honor'}, ...]} - """ - course_key = CourseKey.from_string(course_id) - csv_required = request.GET.get('csv', 'false') - - query_features = ['course_id', 'mode', 'total_issued_certificate', 'report_run_date'] - query_features_names = [ - ('course_id', _('CourseID')), - ('mode', _('Certificate Type')), - ('total_issued_certificate', _('Total Certificates Issued')), - ('report_run_date', _('Date Report Run')) - ] - certificates_data = instructor_analytics_basic.issued_certificates(course_key, query_features) - if csv_required.lower() == 'true': - __, data_rows = instructor_analytics_csvs.format_dictlist(certificates_data, query_features) - return instructor_analytics_csvs.create_csv_response( - 'issued_certificates.csv', - [col_header for __, col_header in query_features_names], - data_rows - ) - else: - response_payload = { - 'certificates': certificates_data, - 'queried_features': query_features, - 'feature_names': dict(query_features_names) - } - return JsonResponse(response_payload) + permission_classes = (IsAuthenticated, permissions.InstructorPermission) + permission_name = permissions.VIEW_ISSUED_CERTIFICATES + + @method_decorator(ensure_csrf_cookie) + @method_decorator(transaction.non_atomic_requests) + def post(self, request, course_id): + """ + Arguments: course_id + Returns: + {"certificates": [{course_id: xyz, mode: 'honor'}, ...]} + """ + return self.all_issued_certificates(request, course_id) + + @method_decorator(ensure_csrf_cookie) + @method_decorator(transaction.non_atomic_requests) + def get(self, request, course_id): + return self.all_issued_certificates(request, course_id) + + def all_issued_certificates(self, request, course_id): + """ + common method for both post and get. This method will return all issued certificates. + """ + course_key = CourseKey.from_string(course_id) + csv_required = request.GET.get('csv', 'false') + + query_features = ['course_id', 'mode', 'total_issued_certificate', 'report_run_date'] + query_features_names = [ + ('course_id', _('CourseID')), + ('mode', _('Certificate Type')), + ('total_issued_certificate', _('Total Certificates Issued')), + ('report_run_date', _('Date Report Run')) + ] + certificates_data = instructor_analytics_basic.issued_certificates(course_key, query_features) + if csv_required.lower() == 'true': + __, data_rows = instructor_analytics_csvs.format_dictlist(certificates_data, query_features) + return instructor_analytics_csvs.create_csv_response( + 'issued_certificates.csv', + [col_header for __, col_header in query_features_names], + data_rows + ) + else: + response_payload = { + 'certificates': certificates_data, + 'queried_features': query_features, + 'feature_names': dict(query_features_names) + } + return JsonResponse(response_payload) @method_decorator(cache_control(no_cache=True, no_store=True, must_revalidate=True), name='dispatch') diff --git a/lms/djangoapps/instructor/views/api_urls.py b/lms/djangoapps/instructor/views/api_urls.py index 491de58ce3c5..2c538ba559b1 100644 --- a/lms/djangoapps/instructor/views/api_urls.py +++ b/lms/djangoapps/instructor/views/api_urls.py @@ -27,9 +27,9 @@ path('modify_access', api.ModifyAccess.as_view(), name='modify_access'), path('bulk_beta_modify_access', api.bulk_beta_modify_access, name='bulk_beta_modify_access'), path('get_problem_responses', api.get_problem_responses, name='get_problem_responses'), + path('get_issued_certificates/', api.GetIssuedCertificates.as_view(), name='get_issued_certificates'), re_path(r'^get_students_features(?P/csv)?$', api.GetStudentsFeatures.as_view(), name='get_students_features'), path('get_grading_config', api.GetGradingConfig.as_view(), name='get_grading_config'), - path('get_issued_certificates/', api.get_issued_certificates, name='get_issued_certificates'), path('get_students_who_may_enroll', api.GetStudentsWhoMayEnroll.as_view(), name='get_students_who_may_enroll'), path('get_anon_ids', api.GetAnonIds.as_view(), name='get_anon_ids'), path('get_student_enrollment_status', api.GetStudentEnrollmentStatus.as_view(),