Beispiel #1
0
 def render(self, block, view_name, context=None):
     """
     Render a specific view of an XBlock.
     """
     # We only need to override this method because some XBlocks in the
     # edx-platform codebase use methods like add_webpack_to_fragment()
     # which create relative URLs (/static/studio/bundles/webpack-foo.js).
     # We want all resource URLs to be absolute, such as is done when
     # local_resource_url() is used.
     fragment = super(XBlockRuntime, self).render(block, view_name, context)
     needs_fix = False
     for resource in fragment.resources:
         if resource.kind == 'url' and resource.data.startswith('/'):
             needs_fix = True
             break
     if needs_fix:
         log.warning(
             "XBlock %s returned relative resource URLs, which are deprecated",
             block.scope_ids.usage_id)
         # The Fragment API is mostly immutable, so changing a resource requires this:
         frag_data = fragment.to_dict()
         for resource in frag_data['resources']:
             if resource['kind'] == 'url' and resource['data'].startswith(
                     '/'):
                 log.debug("-> Relative resource URL: %s", resource['data'])
                 resource['data'] = get_xblock_app_config(
                 ).get_site_root_url() + resource['data']
         fragment = Fragment.from_dict(frag_data)
     return fragment
Beispiel #2
0
    def render(self, block, view_name, context=None):
        """
        Render a specific view of an XBlock.
        """
        # Users who aren't logged in are not allowed to view any views other
        # than public_view. They may call any handlers though.
        if (self.user is None or self.user.is_anonymous) and view_name != 'public_view':
            raise PermissionDenied
        # We also need to override this method because some XBlocks in the
        # edx-platform codebase use methods like add_webpack_to_fragment()
        # which create relative URLs (/static/studio/bundles/webpack-foo.js).
        # We want all resource URLs to be absolute, such as is done when
        # local_resource_url() is used.
        fragment = super(XBlockRuntime, self).render(block, view_name, context)
        needs_fix = False
        for resource in fragment.resources:
            if resource.kind == 'url' and resource.data.startswith('/'):
                needs_fix = True
                break
        if needs_fix:
            log.warning("XBlock %s returned relative resource URLs, which are deprecated", block.scope_ids.usage_id)
            # The Fragment API is mostly immutable, so changing a resource requires this:
            frag_data = fragment.to_dict()
            for resource in frag_data['resources']:
                if resource['kind'] == 'url' and resource['data'].startswith('/'):
                    log.debug("-> Relative resource URL: %s", resource['data'])
                    resource['data'] = get_xblock_app_config().get_site_root_url() + resource['data']
            fragment = Fragment.from_dict(frag_data)

        # Apply any required transforms to the fragment.
        # We could move to doing this in wrap_xblock() and/or use an array of
        # wrapper methods like the ConfigurableFragmentWrapper mixin does.
        fragment = wrap_fragment(fragment, self.transform_static_paths_to_urls(block, fragment.content))

        return fragment
Beispiel #3
0
 def test_from_dict(self):
     """
     Tests the from_dict method.
     """
     test_dict = {
         'content': TEST_HTML,
         'resources': EXPECTED_RESOURCES,
         'js_init_fn': TEST_JS_INIT_FN,
         'js_init_version': EXPECTED_JS_INIT_VERSION,
         'json_init_args': TEST_JSON_INIT_ARGS,
     }
     fragment = Fragment.from_dict(test_dict)
     self.validate_fragment(fragment)