From b453e4861f6cc29dacd6f8c3738009b80cb10c08 Mon Sep 17 00:00:00 2001 From: Ken Clary Date: Fri, 4 Aug 2023 10:50:24 -0400 Subject: [PATCH] fix: fix mako runtime bug with studio_view. TNL-10912 --- openedx/core/djangoapps/xblock/runtime/runtime.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/xblock/runtime/runtime.py b/openedx/core/djangoapps/xblock/runtime/runtime.py index f0a81c6e5fa8..74771833fedf 100644 --- a/openedx/core/djangoapps/xblock/runtime/runtime.py +++ b/openedx/core/djangoapps/xblock/runtime/runtime.py @@ -91,6 +91,9 @@ class XBlockRuntime(RuntimeShim, Runtime): block_field_datas: dict[ScopeIds, FieldData | None] # dict of FieldDataCache objects for XBlock with database-based user state django_field_data_caches: dict[LearningContextKey, FieldDataCache] + # keep track of view name (student_view, studio_view, etc) + # currently only used to track if we're in the studio_view (see below under service()) + view_name: str | None def __init__(self, system: XBlockRuntimeSystem, user: UserType | None): super().__init__( @@ -114,6 +117,7 @@ def __init__(self, system: XBlockRuntimeSystem, user: UserType | None): self.user_id = self.user.id self.block_field_datas = {} self.django_field_data_caches = {} + self.view_name = None def handler_url(self, block, handler_name: str, suffix='', query='', thirdparty=False): """ @@ -265,7 +269,13 @@ def service(self, block: XBlock, service_name: str): deprecated_anonymous_user_id=deprecated_anonymous_student_id ) elif service_name == "mako": - if self.system.student_data_mode == StudentDataMode.Ephemeral: + # The mako service has a seperate "preview" engine used for views in CMS that mimic LMS views. + # Said engine is only configured for CMS. Therefore, when we are in CMS but not the studio_view, + # we want to use the preview engine. The mako service uses a 'lms.main' namespace to indicate + # the preview engine, and 'main' otherwise. + # For backwards compatibility, we check the student_data_mode (Ephemeral indicates CMS) and the + # view_name for 'studio_view.' self.view_name is set by render() below. + if self.system.student_data_mode == StudentDataMode.Ephemeral and self.view_name != 'studio_view': return MakoService(namespace_prefix='lms.') return MakoService() elif service_name == "i18n": @@ -348,6 +358,9 @@ def render(self, block: XBlock, view_name: str, context: dict | None = None): if (self.user is None or self.user.is_anonymous) and view_name != 'public_view': raise PermissionDenied + # track this so service() can know + self.view_name = view_name + # We also need to override this method because some XBlocks in the # edx-platform codebase use methods from builtin_assets.py, # which create relative URLs (/static/studio/bundles/webpack-foo.js).