From aa173b10b4eec2aab87def6251674c6c34a57ee1 Mon Sep 17 00:00:00 2001 From: alexjmpb Date: Tue, 26 Dec 2023 17:37:11 -0500 Subject: [PATCH] feat: adding CCX support to gradebook API --- lms/djangoapps/ccx/views.py | 9 ++++++++- lms/djangoapps/grades/rest_api/v1/gradebook_views.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/ccx/views.py b/lms/djangoapps/ccx/views.py index 7c6a75aaf6d4..c5fbb6be9284 100644 --- a/lms/djangoapps/ccx/views.py +++ b/lms/djangoapps/ccx/views.py @@ -47,11 +47,12 @@ parse_date ) from lms.djangoapps.courseware.field_overrides import disable_overrides -from lms.djangoapps.grades.api import CourseGradeFactory +from lms.djangoapps.grades.api import CourseGradeFactory, is_writable_gradebook_enabled from lms.djangoapps.instructor.enrollment import enroll_email, get_email_params from lms.djangoapps.instructor.views.gradebook_api import get_grade_book_page from openedx.core.djangoapps.django_comment_common.models import FORUM_ROLE_ADMINISTRATOR, assign_role from openedx.core.djangoapps.django_comment_common.utils import seed_permissions_roles +from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.lib.courses import get_course_by_id from xmodule.modulestore.django import SignalHandler # lint-amnesty, pylint: disable=wrong-import-order @@ -143,6 +144,12 @@ def dashboard(request, course, ccx=None): context['ccx_members'] = CourseEnrollment.objects.filter(course_id=ccx_locator, is_active=True) context['gradebook_url'] = reverse( 'ccx_gradebook', kwargs={'course_id': ccx_locator}) + writable_gradebook_url = configuration_helpers.get_value( + 'WRITABLE_GRADEBOOK_URL', + getattr(settings, 'WRITABLE_GRADEBOOK_URL', None), + ) + if is_writable_gradebook_enabled(ccx_locator) and writable_gradebook_url: + context['gradebook_url'] = f'{writable_gradebook_url}/{str(ccx_locator)}' context['grades_csv_url'] = reverse( 'ccx_grades_csv', kwargs={'course_id': ccx_locator}) context['grading_policy'] = json.dumps(grading_policy, indent=4) diff --git a/lms/djangoapps/grades/rest_api/v1/gradebook_views.py b/lms/djangoapps/grades/rest_api/v1/gradebook_views.py index c295563da565..0eab22b17429 100644 --- a/lms/djangoapps/grades/rest_api/v1/gradebook_views.py +++ b/lms/djangoapps/grades/rest_api/v1/gradebook_views.py @@ -20,9 +20,9 @@ from rest_framework.response import Response from rest_framework.views import APIView -from common.djangoapps.student.auth import has_course_author_access +from common.djangoapps.student.auth import has_course_author_access, is_ccx_course from common.djangoapps.student.models import CourseAccessRole, CourseEnrollment, CourseMode -from common.djangoapps.student.roles import BulkRoleCache +from common.djangoapps.student.roles import BulkRoleCache, CourseInstructorRole, CourseStaffRole from common.djangoapps.track.event_transaction_utils import ( create_new_event_transaction_id, get_event_transaction_id, @@ -227,6 +227,13 @@ def _wrapper_view(self, request, course_id, *args, **kwargs): """ course_key = CourseKey.from_string(course_id) if not has_course_author_access(request.user, course_key): + user_has_gradebook_access = any([ + CourseStaffRole(course_key).has_user(request.user), + CourseInstructorRole(course_key).has_user(request.user), + ]) + if is_ccx_course(course_key) and user_has_gradebook_access: + return view(self, request, course_key, *args, **kwargs) + raise DeveloperErrorViewMixin.api_error( status_code=status.HTTP_403_FORBIDDEN, developer_message='The requesting user does not have course author permissions.',