Skip to content

Commit

Permalink
feat: update sequence metadata to allow draft branch
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki committed Oct 30, 2024
1 parent 7f681a5 commit d45a3b1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions openedx/core/djangoapps/courseware_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit d45a3b1

Please sign in to comment.