View Plugins

The view plugins add and change information related to the current view being processed before it is returned the user

ISimpleContextProcessor

class portal.generic.plugin_interfaces.ISimpleContextProcessor

This simple interface class adds extra context (such as variables) to all pages being returned. It can take as many plugin instances as is available. A plugin should have the following functionality:

Requires:
  • __init__ method that defines a name for the plugin

Returns:
  • A dictionary that will be added to the current RequestContext for the view.

Example which returns a simple dictionary:

from portal.pluginbase.core import Plugin
from portal.generic.plugin_interfaces import ISimpleContextProcessor

class MyContextPlug(Plugin):
    implements(ISimpleContextProcessor)

    def __init__(self):
        self.name = "testContext3"

    def return_context(self):
        return {"contextname":"contextvalue"}


service = MyContextPlug()

IContextProcessor

class portal.generic.plugin_interfaces.IContextProcessor

This simple interface class adds extra context (such as variables) to all pages being returned which takes an additional View Object. The View Object could provide extra functionality based upon which user is currently accessing which view. It can take as many plugin instances as is available. A plugin should have the following functionality:

It should always have a __call__(self, context, class_object) method that returns the context to the views, and takes the current view object.

Requires:
  • __init__ method that defines a name for the plugin

  • __call__(self, context, class_object) method

Returns:
  • RequestContext object that will override the current RequestContext bject.

Example which injects context to all pages from the settings:

from django.conf import settings

from portal.pluginbase.core import Plugin
from portal.generic.plugin_interfaces import IContextProcessor

class MyContextPlug(Plugin):
    implements(IContextProcessor)

    def __init__(self):
        self.name = "testGetContext"

    def __call__(self,context, class_object):
        self.SITE_NAME = getattr(settings, 'SITE_NAME', None)
        self.SITE_URL = getattr(settings, 'SITE_URL', None)
        self.context = context
        return self.process_context()

    def process_context(self):
        self.context['SITE_NAME'] = self.SITE_NAME
        self.context['SITE_URL'] = self.SITE_URL
        return self.return_context()

    def return_context(self):
        return self.context

service = MyContextPlug()

Example which adds extra context to the item view page. Firstly upon calling the plugin, we check whether the current view object is the item view page, if not we return the context untouched. If it is process_context then gets the item ID, accesses the extra_context as provided by the view object to get the original shape. If there is no original shape we update the context “archive” with nothing:

from portal.pluginbase.core import Plugin
from portal.generic.plugin_interfaces import IContextProcessor

class ArchiveRestoreContextPlugin(Plugin):
    implements(IContextProcessor)
    def __init__(self):
        self.name = "archiveRestoreGetContext"

    def __call__(self,context, class_object):
        from portal.vidispine.vitem import ItemView
        if isinstance(class_object, ItemView) is False:
            return context
        self.context = context
        self.class_object = class_object
        return self.process_context()

    def process_context(self):
        item_id = self.class_object.slug
        extra_context = self.context.dicts[len(self.context.dicts)-1]
        orig_shape = extra_context['originalshape']
        if not orig_shape:
            original_locations = ['archive', '']

        extra_context['originallocations'] = original_locations
        return self.context

 service = ArchiveRestoreContextPlugin()

ITemplateChooser

class portal.generic.plugin_interfaces.ITemplateChooser

This receives the template that is going to be rendered with a User object for the user that has called the view. It should return a string which points to an existing template which will override the template being used to render the page. If not we just pass through the template.

It should always provide a __call__(self, template, username): method that returns a string pointing to a new template.

Requires
  • __init__ method that defines a name for the plugin

  • __call__(self, template, username) method

Returns:
  • A string of the template to be rendered.

An example:

from portal.pluginbase.core import Plugin
from portal.generic.plugin_interfaces import ITemplateChooser

class MyTemplatePlugIn(Plugin):
    """ PLUGIN EXAMPLE
        This plugin returns a simple method of changing the template
        for a given view.
    """

    implements(ITemplateChooser)

    def __init__(self):
        self.name = "templatename"

    def __call__(self, template, username):
        self.template = template
        self.username = username
        return self.return_template_name()

    def return_template_name(self):
        print(self.username)
        if self.template == "vidispine/search/search.html" and \
                            str(self.username) == 'vidispine':
            self.template = "website/test.html"
        return self.template

templateservice = MyTemplatePlugIn()

The template chooser is useful for overriding what template is served.