From deb5b0f0382216539b2fe2a585254b40edcd586f Mon Sep 17 00:00:00 2001 From: Nathan Sprenkle Date: Tue, 25 Jun 2024 11:17:20 -0400 Subject: [PATCH] feat: add filter to hook render of XBlock (#34865) * Bumps openedx-filters to 1.9.0 * Adds filter to hook render of XBlock * Disables a too-many-statements warning on render_xblock function --- lms/djangoapps/courseware/views/views.py | 36 ++++++++++++++++++------ requirements/edx/base.txt | 2 +- requirements/edx/development.txt | 2 +- requirements/edx/doc.txt | 2 +- requirements/edx/testing.txt | 2 +- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lms/djangoapps/courseware/views/views.py b/lms/djangoapps/courseware/views/views.py index 7df0a1648ae4..95af0cf75f06 100644 --- a/lms/djangoapps/courseware/views/views.py +++ b/lms/djangoapps/courseware/views/views.py @@ -33,10 +33,11 @@ from django.views.generic import View from edx_django_utils.monitoring import set_custom_attribute, set_custom_attributes_for_course_key from ipware.ip import get_client_ip +from lms.djangoapps.static_template_view.views import render_500 from markupsafe import escape from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import CourseKey, UsageKey -from openedx_filters.learning.filters import CourseAboutRenderStarted +from openedx_filters.learning.filters import CourseAboutRenderStarted, RenderXBlockStarted from requests.exceptions import ConnectionError, Timeout # pylint: disable=redefined-builtin from pytz import UTC from rest_framework import status @@ -1532,7 +1533,7 @@ def _check_sequence_exam_access(request, location): @xframe_options_exempt @transaction.non_atomic_requests @ensure_csrf_cookie -def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_staff_debug_info=False): +def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_staff_debug_info=False): # pylint: disable=too-many-statements """ Returns an HttpResponse with HTML content for the xBlock with the given usage_key. The returned HTML is a chromeless rendering of the xBlock (excluding content of the containing courseware). @@ -1641,11 +1642,7 @@ def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_sta if not _check_sequence_exam_access(request, seq_block.location): return HttpResponseForbidden("Access to exam content is restricted") - fragment = block.render(requested_view, context=student_view_context) - optimization_flags = get_optimization_flags_for_content(block, fragment) - context = { - 'fragment': fragment, 'course': course, 'block': block, 'disable_accordion': True, @@ -1666,10 +1663,33 @@ def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_sta 'is_learning_mfe': is_learning_mfe, 'is_mobile_app': is_mobile_app, 'render_course_wide_assets': True, + } + + try: + # .. filter_implemented_name: RenderXBlockStarted + # .. filter_type: org.openedx.learning.xblock.render.started.v1 + context, student_view_context = RenderXBlockStarted.run_filter( + context=context, student_view_context=student_view_context + ) + except RenderXBlockStarted.PreventXBlockBlockRender as exc: + log.info("Halted rendering block %s. Reason: %s", usage_key_string, exc.message) + return render_500(request) + except RenderXBlockStarted.RenderCustomResponse as exc: + log.info("Rendering custom exception for block %s. Reason: %s", usage_key_string, exc.message) + context.update({ + 'fragment': Fragment(exc.response) + }) + return render_to_response('courseware/courseware-chromeless.html', context, request=request) + fragment = block.render(requested_view, context=student_view_context) + optimization_flags = get_optimization_flags_for_content(block, fragment) + + context.update({ + 'fragment': fragment, **optimization_flags, - } - return render_to_response('courseware/courseware-chromeless.html', context) + }) + + return render_to_response('courseware/courseware-chromeless.html', context, request=request) def get_optimization_flags_for_content(block, fragment): diff --git a/requirements/edx/base.txt b/requirements/edx/base.txt index 639980820ab9..8e7f27129e8b 100644 --- a/requirements/edx/base.txt +++ b/requirements/edx/base.txt @@ -746,7 +746,7 @@ openedx-events==9.10.0 # edx-event-bus-redis # event-tracking # ora2 -openedx-filters==1.8.1 +openedx-filters==1.9.0 # via # -r requirements/edx/kernel.in # lti-consumer-xblock diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index 0e822be3c865..51a3617eaa86 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -1284,7 +1284,7 @@ openedx-events==9.10.0 # edx-event-bus-redis # event-tracking # ora2 -openedx-filters==1.8.1 +openedx-filters==1.9.0 # via # -r requirements/edx/doc.txt # -r requirements/edx/testing.txt diff --git a/requirements/edx/doc.txt b/requirements/edx/doc.txt index 28b251780046..1b6dbbbad573 100644 --- a/requirements/edx/doc.txt +++ b/requirements/edx/doc.txt @@ -872,7 +872,7 @@ openedx-events==9.10.0 # edx-event-bus-redis # event-tracking # ora2 -openedx-filters==1.8.1 +openedx-filters==1.9.0 # via # -r requirements/edx/base.txt # lti-consumer-xblock diff --git a/requirements/edx/testing.txt b/requirements/edx/testing.txt index 1dedb30ae609..42aba3f50378 100644 --- a/requirements/edx/testing.txt +++ b/requirements/edx/testing.txt @@ -951,7 +951,7 @@ openedx-events==9.10.0 # edx-event-bus-redis # event-tracking # ora2 -openedx-filters==1.8.1 +openedx-filters==1.9.0 # via # -r requirements/edx/base.txt # lti-consumer-xblock