Metadata Form Plugins

MetadataForm Plugins can change the metadata form before it is sent to the web browser.

IGetMetadataFieldGroups

class portal.generic.plugin_interfaces.IGetMetadataFieldGroups

This interface class takes a list of metadata groups (metadataforms) which can then be overridden by a plugin.

The list of metadata groups is used in many places in portal such as creating new media, search advanced, import from storage and most places where metadata is set.

The standard use case for using this a plugin here is to force a metadata group to always be used in a system (or indeed apply some business logic to select the correct one). In the standard Portal templates we have logic that says that if there is only one group then straight away choose that group and load the form.

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

  • __call__ method when the class is called.

Args:

  • request - the current request

  • metadata_groups - a list of groups

Return:
  • a list of groups, with a blank choice as the first list item. `['','first_group']`

A simple example which will send back just one group to the chooser:

from portal.pluginbase.core import *
from portal.generic.plugin_interfaces import IGetMetadataFieldGroups

class MetadataGroupOverridePlugIn(Plugin):
    """ Override the list of MetadataField Groups for
        display when choosing metadata groups in pages such
        as import and create new media.
    """

    implements(IGetMetadataFieldGroups)

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

    def __call__(self, request, metadata_groups, *args, **kwargs):
        return ['','MYGroup']

metadata_group_override_plugin = MetadataGroupOverridePlugIn()

IMetadataGroupApiProcessor

class portal.generic.plugin_interfaces.IMetadataGroupApiProcessor

Allows a plugin to pre-process a metadata group document that will be rendered as an empty metadata form - either for creating item/collection metadata, or showing a metadata group search form.

Note that search form does not allow for inserting default values for fields.

The REST-endpoint affected is GET /API/v2/metadata-schema/groups/{name}/, and the plugin is called only when metadata group data was found.

This allows a 3rd-party plugin to modify for example default values in a metadata form for example based on where in Portal the request is made, or which item metadata is created for.

Requires:
  • process(plugin, metadata_group_document, request)

Args:
  • metadata_group_document - A metadata group json document which is about to be returned from the API

  • request - The request done to the API

Below is an example plugin that a) hides Season field (portal_mf201890) in the Film metadata group from search forms, and b) forces Season to 42 on the Create Placeholder -page. Please note that b) is only needed for special per view/user cases, standard default values can be set in the Metadata Manager:

import logging
from urllib.parse import urlparse

from portal.generic.plugin_interfaces import IMetadataGroupApiProcessor
from portal.pluginbase.core import Plugin, implements

class MetadataGroupApiProcessorExample(Plugin):
    implements(IMetadataGroupApiProcessor)

    def process(plugin, metadata_group_document, request):
        referrer = request.META.get('HTTP_REFERER')
        group_name = metadata_group_document.get('name')

        log.debug("MetadataGroupApiProcessorExample, document: %s, referrer: %s",
                  metadata_group_document, referrer)

        # Only try to modify if request is made for Film group and referrer is known
        if referrer and group_name == 'Film':
            parsed_url = urlparse(referrer)
            if parsed_url.path.startswith('/search/'):
                # If search request, remove Season field
                metadata_group_document['fields'] = [field for field in metadata_group_document['fields']
                                                     if field['name'] != 'portal_mf201890']
            elif parsed_url.path.startswith('/vs/item/new/'):
                # If create placeholder request, set Season to 42 and readonly
                for field in metadata_group_document.get('fields'):
                    if field['name'] == 'portal_mf201890':
                        # Modify list in place, set default value and readonly
                        field['default'] = 42
                        field['readonly'] = True

        return metadata_group_document


MetadataGroupApiProcessorExample()

Deprecated interfaces

IMetadataFieldValues

This plugin is deprecated, and does not have an effect on the Cantemo UI.

class portal.generic.plugin_interfaces.IMetadataFieldValues

DEPRECATED

This interface class takes a field values to which you can override the values for choice fields, and the initial data sent back.

This means that can provide choices from external and other plugins from with portal, rendered into the browser and saved into the database.

It will receive a list of initial values which you can append to or override.

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

  • __call__ method when the class is called.

Args:

  • fieldname - the internal name of the field - metadata_field123456

  • field_label - the human readable label - “Country”

  • initial_data - List of values coming in. - [‘Uk’, ‘Sweden’, ‘America’]

  • initial_value - Default value when rendering the field - “America”

Return:
  • A list of values.

  • A initial_value

Example which returns a list of countries, but doesn’t set the initial_value:

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


class CountryField(Plugin):
    implements(IMetadataFieldValues)

    def __init__(self):
        pass

    def __call__(self, fieldname, field_label, initial_data, initial_value):
        from portal.countries.countries import COUNTRIES as MyCountries
        if field_label == "Country":
            try:
                new_list = []
                for c in MyCountries:
                    new_list.append(c[1].title())
                return new_list, initial_value
            except Exception:
                return initial_data, initial_value

        else:
            return initial_data, initial_value

countries_field = CountryField()

IItemEmptyMetadataForm

This plugin is deprecated, and does not have an effect on the Cantemo UI.

class portal.generic.plugin_interfaces.IItemEmptyMetadataForm

DEPRECATED

This interface class should be called when the user visits and item page that does not already have a metadatagroup/form associated to the item.

Using this plugin, site specific behaviors can be implemented for such items. For instance, if no metadataform is associated with a specific item, associate form X.

Note, for any interaction with the Vidispine system, the plugin should use the admin/superuser credentials.

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

  • __call__ method when the class is called.

Args:
  • item_id - the id to the item

Return:
  • The metadata form name upon success