Skip to content

Commit

Permalink
feat: add filter to hook render of XBlock (#34865)
Browse files Browse the repository at this point in the history
* Bumps openedx-filters to 1.9.0
* Adds filter to hook render of XBlock
* Disables a too-many-statements warning on render_xblock function
  • Loading branch information
nsprenkle authored Jun 25, 2024
1 parent a26e4b4 commit deb5b0f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
36 changes: 28 additions & 8 deletions lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit deb5b0f

Please sign in to comment.