diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py index a5c860f3fc67..3f9a04851173 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py @@ -1,7 +1,7 @@ """ API Views for unit page """ import edx_api_doc_tools as apidocs -from django.http import Http404 +from django.http import Http404, HttpResponseBadRequest from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import UsageKey from rest_framework.request import Request @@ -9,9 +9,11 @@ from rest_framework.views import APIView from cms.djangoapps.contentstore.utils import get_container_handler_context +from cms.djangoapps.contentstore.views.component import _get_item_in_course from cms.djangoapps.contentstore.rest_api.v1.serializers import ContainerHandlerSerializer from openedx.core.lib.api.view_utils import view_auth_classes -from xmodule.modulestore.django import modulestore +from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order +from xmodule.modulestore.exceptions import ItemNotFoundError # lint-amnesty, pylint: disable=wrong-import-order @view_auth_classes(is_authenticated=True) @@ -127,6 +129,15 @@ def get(self, request: Request, usage_key_string: str): usage_key = self.get_object(usage_key_string) course_key = usage_key.course_key with modulestore().bulk_operations(course_key): - context = get_container_handler_context(request, usage_key) + try: + course, xblock, lms_link, preview_lms_link = _get_item_in_course(request, usage_key) + except ItemNotFoundError: + return HttpResponseBadRequest() + + context = get_container_handler_context(request, usage_key, course, xblock) + context.update({ + 'draft_preview_link': preview_lms_link, + 'published_preview_link': lms_link, + }) serializer = ContainerHandlerSerializer(context) return Response(serializer.data) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index f447912a5e83..bdf8b840852e 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -12,8 +12,6 @@ from django.conf import settings from django.core.exceptions import ValidationError -from django.http import HttpResponseBadRequest -from django.shortcuts import redirect from django.urls import reverse from django.utils import translation from django.utils.translation import gettext as _ @@ -1691,14 +1689,13 @@ def get_course_videos_context(course_block, pagination_conf, course_key=None): return course_video_context -def get_container_handler_context(request, usage_key): +def get_container_handler_context(request, usage_key, course, xblock): """ Utils is used to get context for container xblock requests. It is used for both DRF and django views. """ from cms.djangoapps.contentstore.views.component import ( - _get_item_in_course, get_component_templates, get_unit_tags, CONTAINER_TEMPLATES, @@ -1711,11 +1708,6 @@ def get_container_handler_context(request, usage_key): ) from openedx.core.djangoapps.content_staging import api as content_staging_api - try: - course, xblock, lms_link, preview_lms_link = _get_item_in_course(request, usage_key) - except ItemNotFoundError: - return HttpResponseBadRequest() - component_templates = get_component_templates(course) ancestor_xblocks = [] parent = get_parent_xblock(xblock) @@ -1724,9 +1716,6 @@ def get_container_handler_context(request, usage_key): is_unit_page = is_unit(xblock) unit = xblock if is_unit_page else None - if is_unit_page and use_new_unit_page(course.id): - return redirect(get_unit_url(course.id, unit.location)) - is_first = True block = xblock @@ -1814,8 +1803,6 @@ def get_container_handler_context(request, usage_key): 'ancestor_xblocks': ancestor_xblocks, 'component_templates': component_templates, 'xblock_info': xblock_info, - 'draft_preview_link': preview_lms_link, - 'published_preview_link': lms_link, 'templates': CONTAINER_TEMPLATES, 'show_unit_tags': show_unit_tags, # Status of the user's clipboard, exactly as would be returned from the "GET clipboard" REST API. diff --git a/cms/djangoapps/contentstore/views/component.py b/cms/djangoapps/contentstore/views/component.py index 193a62dcaa0e..ae0f62e3316e 100644 --- a/cms/djangoapps/contentstore/views/component.py +++ b/cms/djangoapps/contentstore/views/component.py @@ -9,6 +9,7 @@ from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied from django.http import Http404, HttpResponseBadRequest +from django.shortcuts import redirect from django.utils.translation import gettext as _ from django.views.decorators.http import require_GET from opaque_keys import InvalidKeyError @@ -23,12 +24,14 @@ from common.djangoapps.student.auth import has_course_author_access from common.djangoapps.xblock_django.api import authorable_xblocks, disabled_xblocks from common.djangoapps.xblock_django.models import XBlockStudioConfigurationFlag -from cms.djangoapps.contentstore.toggles import use_new_problem_editor +from cms.djangoapps.contentstore.helpers import is_unit +from cms.djangoapps.contentstore.toggles import use_new_problem_editor, use_new_unit_page +from cms.djangoapps.contentstore.xblock_storage_handlers.view_handlers import load_services_for_studio from openedx.core.lib.xblock_utils import get_aside_from_xblock, is_xblock_aside from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration from openedx.core.djangoapps.content_tagging.api import get_content_tags from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order -from cms.djangoapps.contentstore.xblock_storage_handlers.view_handlers import load_services_for_studio +from xmodule.modulestore.exceptions import ItemNotFoundError # lint-amnesty, pylint: disable=wrong-import-order __all__ = [ 'container_handler', @@ -108,7 +111,7 @@ def container_handler(request, usage_key_string): # pylint: disable=too-many-st json: not currently supported """ - from ..utils import get_container_handler_context + from ..utils import get_container_handler_context, get_unit_url if 'text/html' in request.META.get('HTTP_ACCEPT', 'text/html'): @@ -117,7 +120,22 @@ def container_handler(request, usage_key_string): # pylint: disable=too-many-st except InvalidKeyError: # Raise Http404 on invalid 'usage_key_string' raise Http404 # lint-amnesty, pylint: disable=raise-missing-from with modulestore().bulk_operations(usage_key.course_key): - container_handler_context = get_container_handler_context(request, usage_key) + try: + course, xblock, lms_link, preview_lms_link = _get_item_in_course(request, usage_key) + except ItemNotFoundError: + return HttpResponseBadRequest() + + is_unit_page = is_unit(xblock) + unit = xblock if is_unit_page else None + + if is_unit_page and use_new_unit_page(course.id): + return redirect(get_unit_url(course.id, unit.location)) + + container_handler_context = get_container_handler_context(request, usage_key, course, xblock) + container_handler_context.update({ + 'draft_preview_link': preview_lms_link, + 'published_preview_link': lms_link, + }) return render_to_response('container.html', container_handler_context) else: return HttpResponseBadRequest("Only supports HTML requests")