def student_view(self, context): _ = self.runtime.service(self, "i18n").ugettext context = context or {} self._capture_basic_metrics() banner_text = None prereq_met = True prereq_meta_info = {} if self._required_prereq(): if self.runtime.user_is_staff: banner_text = _('This subsection is unlocked for learners when they meet the prerequisite requirements.') # lint-amnesty, pylint: disable=line-too-long else: # check if prerequisite has been met prereq_met, prereq_meta_info = self._compute_is_prereq_met( True) if prereq_met: special_html_view = self._hidden_content_student_view( context) or self._special_exam_student_view() if special_html_view: masquerading_as_specific_student = context.get( 'specific_masquerade', False) banner_text, special_html = special_html_view if special_html and not masquerading_as_specific_student: fragment = Fragment(special_html) add_webpack_to_fragment(fragment, 'SequenceBlockPreview') shim_xmodule_js(fragment, 'Sequence') return fragment return self._student_or_public_view(context, prereq_met, prereq_meta_info, banner_text)
def student_view(self, context): """ Renders the normal student view of the block in the LMS. """ _ = self.runtime.service(self, "i18n").ugettext context = context or {} self._capture_basic_metrics() banner_text = None prereq_met = True prereq_meta_info = {} if self._required_prereq(): if self.runtime.service(self, 'user').get_current_user().opt_attrs.get(ATTR_KEY_USER_IS_STAFF): banner_text = _( 'This subsection is unlocked for learners when they meet the prerequisite requirements.' ) else: # check if prerequisite has been met prereq_met, prereq_meta_info = self._compute_is_prereq_met(True) if prereq_met: special_html_view = self._hidden_content_student_view(context) or self._special_exam_student_view() if special_html_view: masquerading_as_specific_student = context.get('specific_masquerade', False) banner_text, special_html = special_html_view if special_html and not masquerading_as_specific_student: fragment = Fragment(special_html) add_webpack_to_fragment(fragment, 'SequenceBlockPreview') shim_xmodule_js(fragment, 'Sequence') return fragment return self._student_or_public_view(context, prereq_met, prereq_meta_info, banner_text)
def _student_or_public_view(self, context, prereq_met, prereq_meta_info, banner_text=None, view=STUDENT_VIEW): """ Returns the rendered student view of the content of this sequential. If banner_text is given, it is added to the content. """ _ = self.runtime.service(self, "i18n").ugettext display_items = self.get_display_items() self._update_position(context, len(display_items)) fragment = Fragment() params = self._get_render_metadata(context, display_items, prereq_met, prereq_meta_info, banner_text, view, fragment) # lint-amnesty, pylint: disable=line-too-long fragment.add_content( self.system.render_template("seq_module.html", params)) self._capture_full_seq_item_metrics(display_items) self._capture_current_unit_metrics(display_items) add_webpack_to_fragment(fragment, 'SequenceBlockPreview') shim_xmodule_js(fragment, 'Sequence') return fragment
def _student_or_public_view(self, context, prereq_met, prereq_meta_info, banner_text=None, view=STUDENT_VIEW): """ Returns the rendered student view of the content of this sequential. If banner_text is given, it is added to the content. """ _ = self.runtime.service(self, "i18n").ugettext display_items = self.get_display_items() self._update_position(context, len(display_items)) fragment = Fragment() params = self._get_render_metadata(context, display_items, prereq_met, prereq_meta_info, banner_text, view, fragment) # lint-amnesty, pylint: disable=line-too-long if SHOW_PROGRESS_BAR.is_enabled() and getattr( settings, 'COMPLETION_AGGREGATOR_URL', ''): parent_block_id = self.get_parent().scope_ids.usage_id.block_id params['chapter_completion_aggregator_url'] = '/'.join([ settings.COMPLETION_AGGREGATOR_URL, str(self.course_id), parent_block_id ]) + '/' fragment.add_content( self.runtime.service(self, 'mako').render_template( "seq_module.html", params)) self._capture_full_seq_item_metrics(display_items) self._capture_current_unit_metrics(display_items) add_webpack_to_fragment(fragment, 'SequenceBlockPreview') shim_xmodule_js(fragment, 'Sequence') return fragment
def student_view(self, _context): """ Return the student view. """ fragment = Fragment(self.get_html()) add_webpack_to_fragment(fragment, 'VideoBlockPreview') shim_xmodule_js(fragment, 'Video') return fragment
def student_view(self, _context): """ Return a fragment that contains the html for the student view """ fragment = Fragment(self.get_html()) add_webpack_to_fragment(fragment, 'HtmlBlockPreview') shim_xmodule_js(fragment, 'HTMLModule') return fragment
def wrap_xblock(handler_prefix, block, view, frag, context, display_name_only=False): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param handler_prefix: A function that takes a block and returns the url prefix for the javascript handler_url. This prefix should be able to have {handler_name}/{suffix}?{query} appended to it to return a valid handler_url :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` """ # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} css_classes = ['xblock', 'xblock-' + view] if isinstance(block, (XModule, XModuleDescriptor)): if view == 'student_view': # The block is acting as an XModule css_classes.append('xmodule_display') elif view == 'studio_view': # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + class_name) data['type'] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-version'] = frag.js_init_version data['handler-prefix'] = handler_prefix(block) data['block-type'] = block.scope_ids.block_type template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'data_attributes': ' '.join('data-{}="{}"'.format(key, value) for key, value in data.items()), } return wrap_fragment( frag, render_to_string('xblock_wrapper.html', template_context))
def wrap_xblock(runtime_class, block, view, frag, context, usage_id_serializer, request_token, display_name_only=False, extra_data=None): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param usage_id_serializer: A function to serialize the block's usage_id for use by the front-end Javascript Runtime. :param request_token: An identifier that is unique per-request, so that only xblocks rendered as part of this request will have their javascript initialized. :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} data.update(extra_data) css_classes = ['xblock', 'xblock-{}'.format(view)] if isinstance(block, (XModule, XModuleDescriptor)): if view in PREVIEW_VIEWS: # The block is acting as an XModule css_classes.append('xmodule_display') elif view == STUDIO_VIEW: # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + class_name) data['type'] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-class'] = runtime_class data['runtime-version'] = frag.js_init_version data['block-type'] = block.scope_ids.block_type data['usage-id'] = usage_id_serializer(block.scope_ids.usage_id) data['request-token'] = request_token template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'display_name': block.display_name_with_default, 'data_attributes': u' '.join(u'data-{}="{}"'.format(key, value) for key, value in data.iteritems()), } return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def student_view(self, _context): """ Return the student view. """ fragment = Fragment() fragment.add_content(self.runtime.service(self, 'mako').render_template('lti.html', self.get_context())) add_webpack_to_fragment(fragment, 'LTIBlockPreview') shim_xmodule_js(fragment, 'LTI') return fragment
def student_view(self, _context): """ Renders the student view. """ fragment = Fragment() fragment.add_content(self.get_html()) add_webpack_to_fragment(fragment, 'ConditionalBlockPreview') shim_xmodule_js(fragment, 'Conditional') return fragment
def studio_view(self, _context): """ Return the studio view. """ fragment = Fragment( self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context()) ) add_webpack_to_fragment(fragment, 'WordCloudBlockStudio') shim_xmodule_js(fragment, self.studio_js_module_name) return fragment
def studio_view(self, _context): """ Return the studio view. """ fragment = Fragment( self.runtime.service(self, 'mako').render_template( self.mako_template, self.get_context())) add_webpack_to_fragment(fragment, 'HtmlBlockStudio') shim_xmodule_js(fragment, 'HTMLEditingDescriptor') return fragment
def studio_view(self, _context): """ Return the studio view. """ fragment = Fragment( self.system.render_template(self.mako_template, self.get_context())) add_webpack_to_fragment(fragment, 'LibraryContentBlockStudio') shim_xmodule_js(fragment, self.studio_js_module_name) return fragment
def studio_view(self, _context): """ Return the studio view. """ fragment = Fragment( self.system.render_template(self.mako_template, self.get_context()) ) add_webpack_to_fragment(fragment, 'ProblemBlockStudio') shim_xmodule_js(fragment, 'MarkdownEditingDescriptor') return fragment
def studio_view(self, _context): """ Return the studio view. """ fragment = Fragment( self.system.render_template(self.mako_template, self.get_context())) add_webpack_to_fragment(fragment, 'CustomTagBlockStudio') shim_xmodule_js(fragment, 'XMLEditingDescriptor') return fragment
def student_view(self, _context): """ Return a fragment that contains the html for the student view """ fragment = Fragment(self.get_html()) ## this line is a cutom change made during ironwood rebase fragment.add_javascript_url(settings.STATIC_URL + 'bundles/commons.js') add_webpack_to_fragment(fragment, 'HtmlBlockPreview') shim_xmodule_js(fragment, 'HTMLModule') return fragment
def student_view(self, context): """ Renders the output that a student will see. """ fragment = Fragment() fragment.add_content(self.get_html()) add_webpack_to_fragment(fragment, 'AnnotatableBlockPreview') shim_xmodule_js(fragment, 'Annotatable') return fragment
def student_view(self, _context): """ Return the student view. """ # self.score is initialized in self.lcp but in this method is accessed before self.lcp so just call it first. self.lcp fragment = Fragment(self.get_html()) add_webpack_to_fragment(fragment, 'ProblemBlockPreview') shim_xmodule_js(fragment, 'Problem') return fragment
def student_view(self, context): # lint-amnesty, pylint: disable=unused-argument """ Renders the output that a student will see. """ fragment = Fragment() fragment.add_content(self.get_html()) add_webpack_to_fragment(fragment, 'AnnotatableBlockPreview') shim_xmodule_js(fragment, 'Annotatable') return fragment
def wrap_xblock(runtime_class, block, view, frag, context, display_name_only=False, extra_data=None): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} data.update(extra_data) css_classes = ['xblock', 'xblock-' + view] if isinstance(block, (XModule, XModuleDescriptor)): if view == 'student_view': # The block is acting as an XModule css_classes.append('xmodule_display') elif view == 'mobi_student_view': # The block is acting as an XModule css_classes.append('xmodule_display') elif view == 'studio_view': # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + class_name) data['type'] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-class'] = runtime_class data['runtime-version'] = frag.js_init_version data['block-type'] = block.scope_ids.block_type data['usage-id'] = quote_slashes(unicode(block.scope_ids.usage_id).encode('utf-8')) template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'display_name': block.display_name_with_default, 'data_attributes': ' '.join(u'data-{}="{}"'.format(key, value) for key, value in data.items()), } return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def wrap_xblock(runtime_class, block, view, frag, context, usage_id_serializer, display_name_only=False, extra_data=None): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param usage_id_serializer: A function to serialize the block's usage_id for use by the front-end Javascript Runtime. :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} data.update(extra_data) css_classes = ['xblock', 'xblock-' + view] if isinstance(block, (XModule, XModuleDescriptor)): if view == 'student_view': # The block is acting as an XModule css_classes.append('xmodule_display') elif view == 'studio_view': # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + class_name) data['type'] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-class'] = runtime_class data['runtime-version'] = frag.js_init_version data['block-type'] = block.scope_ids.block_type data['usage-id'] = usage_id_serializer(block.scope_ids.usage_id) template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'display_name': block.display_name_with_default, 'data_attributes': u' '.join(u'data-{}="{}"'.format(key, value) for key, value in data.iteritems()), } return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def studio_view(self, _context): """ Return the studio view. """ context = MakoTemplateBlockBase.get_context(self) # Add our specific template information (the raw data body) context.update({'data': self.data}) fragment = Fragment( self.system.render_template(self.mako_template, context)) add_webpack_to_fragment(fragment, 'LTIBlockStudio') shim_xmodule_js(fragment, self.studio_js_module_name) return fragment
def studio_view(self, context): """ Return the studio view. """ fragment = Fragment( self.system.render_template(self.mako_template, self.get_context())) # Use the SequenceDescriptor js for the metadata edit view. # Both the webpack bundle to include and the js class are named "SequenceDescriptor". add_webpack_to_fragment(fragment, SequenceDescriptor.js_module_name) shim_xmodule_js(fragment, SequenceDescriptor.js_module_name) return fragment
def student_view(self, _context): """ Renders the student view. """ fragment = Fragment() params = { 'element_id': self.location.html_id(), 'element_class': self.location.block_type, 'ajax_url': self.ajax_url, 'configuration_json': self.dump_poll(), } fragment.add_content(self.system.render_template('poll.html', params)) add_webpack_to_fragment(fragment, 'PollBlockPreview') shim_xmodule_js(fragment, 'Poll') return fragment
def student_view(self, _context, show_detailed_errors=False): """ Return the student view. """ # self.score is initialized in self.lcp but in this method is accessed before self.lcp so just call it first. try: self.lcp except Exception as err: html = self.handle_fatal_lcp_error(err if show_detailed_errors else None) else: html = self.get_html() fragment = Fragment(html) add_webpack_to_fragment(fragment, 'ProblemBlockPreview') shim_xmodule_js(fragment, 'Problem') return fragment
def wrap_xblock(handler_prefix, block, view, frag, context, display_name_only=False): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param handler_prefix: A function that takes a block and returns the url prefix for the javascript handler_url. This prefix should be able to have {handler_name}/{suffix}?{query} appended to it to return a valid handler_url :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` """ # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} css_classes = ['xblock', 'xblock-' + view] if isinstance(block, (XModule, XModuleDescriptor)): if view == 'student_view': # The block is acting as an XModule css_classes.append('xmodule_display') elif view == 'studio_view': # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + class_name) data['type'] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-version'] = frag.js_init_version data['handler-prefix'] = handler_prefix(block) data['block-type'] = block.scope_ids.block_type template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'data_attributes': ' '.join('data-{}="{}"'.format(key, value) for key, value in data.items()), } return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def student_view(self, context): """ Renders the output that a student will see. """ fragment = Fragment() fragment.add_content(self.system.render_template('word_cloud.html', { 'ajax_url': self.ajax_url, 'display_name': self.display_name, 'instructions': self.instructions, 'element_class': self.location.block_type, 'element_id': self.location.html_id(), 'num_inputs': self.num_inputs, 'submitted': self.submitted, })) add_webpack_to_fragment(fragment, 'WordCloudBlockPreview') shim_xmodule_js(fragment, 'WordCloud') return fragment
def student_view(self, context): # lint-amnesty, pylint: disable=unused-argument """ Renders the output that a student will see. """ fragment = Fragment() fragment.add_content(self.runtime.service(self, 'mako').render_template('word_cloud.html', { 'ajax_url': self.ajax_url, 'display_name': self.display_name, 'instructions': self.instructions, 'element_class': self.location.block_type, 'element_id': self.location.html_id(), 'num_inputs': self.num_inputs, 'submitted': self.submitted, })) add_webpack_to_fragment(fragment, 'WordCloudBlockPreview') shim_xmodule_js(fragment, 'WordCloud') return fragment
def wrap_xblock( runtime_class, block, view, frag, context, usage_id_serializer, request_token, # pylint: disable=redefined-outer-name display_name_only=False, extra_data=None ): """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param usage_id_serializer: A function to serialize the block's usage_id for use by the front-end Javascript Runtime. :param request_token: An identifier that is unique per-request, so that only xblocks rendered as part of this request will have their javascript initialized. :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} data.update(extra_data) if context: data.update(context.get('wrap_xblock_data', {})) css_classes = [ 'xblock', 'xblock-{}'.format(markupsafe.escape(view)), 'xblock-{}-{}'.format( markupsafe.escape(view), markupsafe.escape(block.scope_ids.block_type), ) ] if view == STUDENT_VIEW and getattr(block, 'HIDDEN', False): css_classes.append('is-hidden') if isinstance(block, (XModule, XModuleDescriptor)) or getattr(block, 'uses_xmodule_styles_setup', False): if view in PREVIEW_VIEWS: # The block is acting as an XModule css_classes.append('xmodule_display') elif view == STUDIO_VIEW: # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') css_classes.append('xmodule_' + markupsafe.escape(class_name)) if isinstance(block, (XModule, XModuleDescriptor)): data['type'] = block.js_module_name shim_xmodule_js(frag, block.js_module_name) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-class'] = runtime_class data['runtime-version'] = frag.js_init_version data['block-type'] = block.scope_ids.block_type data['usage-id'] = usage_id_serializer(block.scope_ids.usage_id) data['request-token'] = request_token data['graded'] = getattr(block, 'graded', False) data['has-score'] = getattr(block, 'has_score', False) if block.name: data['name'] = block.name template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'display_name': block.display_name_with_default_escaped, # xss-lint: disable=python-deprecated-display-name 'data_attributes': ' '.join('data-{}="{}"'.format(markupsafe.escape(key), markupsafe.escape(value)) for key, value in data.items()), } if hasattr(frag, 'json_init_args') and frag.json_init_args is not None: template_context['js_init_parameters'] = frag.json_init_args else: template_context['js_init_parameters'] = "" if isinstance(block, (XModule, XModuleDescriptor)): # Add the webpackified asset tags add_webpack_to_fragment(frag, class_name) return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def wrap_xblock( runtime_class, block, view, frag, context, usage_id_serializer, request_token, # pylint: disable=redefined-outer-name display_name_only=False, extra_data=None ): """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param usage_id_serializer: A function to serialize the block's usage_id for use by the front-end Javascript Runtime. :param request_token: An identifier that is unique per-request, so that only xblocks rendered as part of this request will have their javascript initialized. :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, 'unmixed_class', block.__class__).__name__ data = {} data.update(extra_data) if context: data.update(context.get('wrap_xblock_data', {})) css_classes = [ 'xblock', 'xblock-{}'.format(markupsafe.escape(view)), 'xblock-{}-{}'.format( markupsafe.escape(view), markupsafe.escape(block.scope_ids.block_type), ) ] if isinstance(block, (XModule, XModuleDescriptor)): if view in PREVIEW_VIEWS: # The block is acting as an XModule css_classes.append('xmodule_display') elif view == STUDIO_VIEW: # The block is acting as an XModuleDescriptor css_classes.append('xmodule_edit') if getattr(block, 'HIDDEN', False): css_classes.append('is-hidden') css_classes.append('xmodule_' + markupsafe.escape(class_name)) data['type'] = block.js_module_name shim_xmodule_js(block, frag) if frag.js_init_fn: data['init'] = frag.js_init_fn data['runtime-class'] = runtime_class data['runtime-version'] = frag.js_init_version data['block-type'] = block.scope_ids.block_type data['usage-id'] = usage_id_serializer(block.scope_ids.usage_id) data['request-token'] = request_token data['graded'] = getattr(block, 'graded', False) data['has-score'] = getattr(block, 'has_score', False) if block.name: data['name'] = block.name template_context = { 'content': block.display_name if display_name_only else frag.content, 'classes': css_classes, 'display_name': block.display_name_with_default_escaped, # xss-lint: disable=python-deprecated-display-name 'data_attributes': u' '.join(u'data-{}="{}"'.format(markupsafe.escape(key), markupsafe.escape(value)) for key, value in data.iteritems()), } if hasattr(frag, 'json_init_args') and frag.json_init_args is not None: template_context['js_init_parameters'] = frag.json_init_args else: template_context['js_init_parameters'] = "" if isinstance(block, (XModule, XModuleDescriptor)): # Add the webpackified asset tags add_webpack_to_fragment(frag, class_name) return wrap_fragment(frag, render_to_string('xblock_wrapper.html', template_context))
def wrap_xblock( runtime_class, block, view, frag, context, usage_id_serializer, request_token, display_name_only=False, extra_data=None, ): # pylint: disable=unused-argument """ Wraps the results of rendering an XBlock view in a standard <section> with identifying data so that the appropriate javascript module can be loaded onto it. :param runtime_class: The name of the javascript runtime class to use to load this block :param block: An XBlock (that may be an XModule or XModuleDescriptor) :param view: The name of the view that rendered the fragment being wrapped :param frag: The :class:`Fragment` to be wrapped :param context: The context passed to the view being rendered :param usage_id_serializer: A function to serialize the block's usage_id for use by the front-end Javascript Runtime. :param request_token: An identifier that is unique per-request, so that only xblocks rendered as part of this request will have their javascript initialized. :param display_name_only: If true, don't render the fragment content at all. Instead, just render the `display_name` of `block` :param extra_data: A dictionary with extra data values to be set on the wrapper """ if extra_data is None: extra_data = {} # If any mixins have been applied, then use the unmixed class class_name = getattr(block, "unmixed_class", block.__class__).__name__ data = {} data.update(extra_data) css_classes = ["xblock", "xblock-{}".format(markupsafe.escape(view))] if isinstance(block, (XModule, XModuleDescriptor)): if view in PREVIEW_VIEWS: # The block is acting as an XModule css_classes.append("xmodule_display") elif view == STUDIO_VIEW: # The block is acting as an XModuleDescriptor css_classes.append("xmodule_edit") css_classes.append("xmodule_" + markupsafe.escape(class_name)) data["type"] = block.js_module_name shim_xmodule_js(frag) if frag.js_init_fn: data["init"] = frag.js_init_fn data["runtime-class"] = runtime_class data["runtime-version"] = frag.js_init_version data["block-type"] = block.scope_ids.block_type data["usage-id"] = usage_id_serializer(block.scope_ids.usage_id) data["request-token"] = request_token if block.name: data["name"] = block.name template_context = { "content": block.display_name if display_name_only else frag.content, "classes": css_classes, "display_name": block.display_name_with_default, "data_attributes": u" ".join( u'data-{}="{}"'.format(markupsafe.escape(key), markupsafe.escape(value)) for key, value in data.iteritems() ), } if hasattr(frag, "json_init_args") and frag.json_init_args is not None: template_context["js_init_parameters"] = json.dumps(frag.json_init_args) template_context["js_pass_parameters"] = True else: template_context["js_init_parameters"] = "" template_context["js_pass_parameters"] = False return wrap_fragment(frag, render_to_string("xblock_wrapper.html", template_context))