From d45a3b19a1f1d2a74f1b54b71039080cfd237b5b Mon Sep 17 00:00:00 2001 From: KristinAoki Date: Wed, 30 Oct 2024 09:33:52 -0400 Subject: [PATCH] feat: update sequence metadata to allow draft branch --- .../core/djangoapps/courseware_api/views.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/openedx/core/djangoapps/courseware_api/views.py b/openedx/core/djangoapps/courseware_api/views.py index 7f56133b3459..c7ab594cc6da 100644 --- a/openedx/core/djangoapps/courseware_api/views.py +++ b/openedx/core/djangoapps/courseware_api/views.py @@ -19,6 +19,7 @@ from rest_framework.response import Response from rest_framework.views import APIView +from xmodule.modulestore import ModuleStoreEnum # lint-amnesty, pylint: disable=wrong-import-order from xmodule.modulestore.django import modulestore from xmodule.modulestore.exceptions import ItemNotFoundError, NoPathToItem from xmodule.modulestore.search import path_to_location @@ -594,30 +595,36 @@ def get(self, request, usage_key_string, *args, **kwargs): # lint-amnesty, pyli usage_key = UsageKey.from_string(usage_key_string) except InvalidKeyError as exc: raise NotFound(f"Invalid usage key: '{usage_key_string}'.") from exc + + staff_access = has_access(request.user, 'staff', usage_key.course_key) + is_preview = request.GET.get('preview', '0') == '1' _, request.user = setup_masquerade( request, usage_key.course_key, - staff_access=has_access(request.user, 'staff', usage_key.course_key), + staff_access=staff_access, reset_masquerade_data=True, ) - sequence, _ = get_block_by_usage_id( - self.request, - str(usage_key.course_key), - str(usage_key), - disable_staff_debug_info=True, - will_recheck_access=True) + branch_type = ModuleStoreEnum.Branch.draft_preferred if is_preview and has_access else ModuleStoreEnum.Branch.published_only + + with modulestore().branch_setting(branch_type, usage_key.course_key): + sequence, _ = get_block_by_usage_id( + self.request, + str(usage_key.course_key), + str(usage_key), + disable_staff_debug_info=True, + will_recheck_access=True) - if not hasattr(sequence, 'get_metadata'): - # Looks like we were asked for metadata on something that is not a sequence (or section). - return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY) + if not hasattr(sequence, 'get_metadata'): + # Looks like we were asked for metadata on something that is not a sequence (or section). + return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY) - view = STUDENT_VIEW - if request.user.is_anonymous: - view = PUBLIC_VIEW + view = STUDENT_VIEW + if request.user.is_anonymous: + view = PUBLIC_VIEW - context = {'specific_masquerade': is_masquerading_as_specific_student(request.user, usage_key.course_key)} - return Response(sequence.get_metadata(view=view, context=context)) + context = {'specific_masquerade': is_masquerading_as_specific_student(request.user, usage_key.course_key)} + return Response(sequence.get_metadata(view=view, context=context)) class Resume(DeveloperErrorViewMixin, APIView):