예제 #1
0
    def render(self, block, view_name, context=None):
        """
        Render a block by invoking its view.

        Finds the view named `view_name` on `block`.  The default view will be
        used if a specific view hasn't be registered.  If there is no default
        view, an exception will be raised.

        The view is invoked, passing it `context`.  The value returned by the
        view is returned, with possible modifications by the runtime to
        integrate it into a larger whole.

        """
        # Set the active view so that :function:`render_child` can use it
        # as a default
        old_view_name = self._view_name
        self._view_name = view_name
        try:

            view_fn = getattr(block, view_name, None)
            if view_fn is None:
                view_fn = getattr(block, "fallback_view", None)
                if view_fn is None:
                    raise NoSuchViewError()
                view_fn = functools.partial(view_fn, view_name)

            frag = view_fn(context)

            # Explicitly save because render action may have changed state
            block.save()
            return self.wrap_child(block, view_name, frag, context)
        finally:
            # Reset the active view to what it was before entering this method
            self._view_name = old_view_name
예제 #2
0
    def render_sequense_child_context(self, block, view_name, context=None):
        """ 
            :param block: object
            :param view_name: string
            :param context: dict
            :return: dict
        """
        context = context or {}
        if view_name in PREVIEW_VIEWS:
            block = self._get_student_block(block)

        vertical_block = VerticalBlockContext(block)

        view_fn = getattr(vertical_block, view_name, None)
        if view_fn is None:
            raise NoSuchViewError(vertical_block, view_name)

        frag = view_fn(vertical_block._verticalBlock, context)

        return frag
예제 #3
0
    def render_component_context(self, component, view_name, context=None):
        """
                :param component: object
                :param view_name: string
                :param context: dict
                :return: dict
        """

        context = context or {}
        if view_name in PREVIEW_VIEWS:
            block = self._get_student_block(component)

        component_module = ComponentModuleConext(component)

        view_fn = getattr(component_module, view_name, None)
        if view_fn is None:
            raise NoSuchViewError(component_module, view_name)

        frag = view_fn(component_module._component._xmodule, context)

        return frag
예제 #4
0
    def render_sequense_context(self, block, view_name, context=None):
        """
        
        :param block: object
        :param view_name: string
        :param context: dict
        :return: dict
        """
        context = context or {}
        if view_name in PREVIEW_VIEWS:
            block = self._get_student_block(block)

        sequence_module = SequenceModuleContext(block)

        view_fn = getattr(sequence_module, view_name, None)
        if view_fn is None:
            raise NoSuchViewError(sequence_module, view_name)

        frag = view_fn(sequence_module._sequenceModule, context)

        return frag