Package: ckanext-esdstandards
ckanext-esdstandardsHelpers for working with LGA’sESD standardson CKAN.This extension contains:Action functions for autocomplete lookup for ESD Functions and ServicesForm widgets and validators for easily integrate ESD Functions and Services fields
into custom schemas (adding thela_functionandla_servicefields,
supported when harvesting datasets intohttp://data.gov.uk)Snippets for rendering the ESD Functions and Services on the dataset page.For more details, seeIntegrating it on your own extensionInstallationTo install ckanext-esdstandards:Activate your CKAN virtual environment, for example:source /usr/lib/ckan/default/bin/activateInstall the ckanext-esdstandards Python package into your virtual environment:pip install ckanext-esdstandardsAddesdto theckan.pluginssetting in your CKAN
config file (by default the config file is located at/etc/ckan/default/production.ini).Populate the DB tables, by running:paster --plugin=ckanext-esdstandards esd initdb -c /etc/ckan/default/production.ini
paster --plugin=ckanext-esdstandards esd load -c /etc/ckan/default/production.iniRestart CKAN. For example if you’ve deployed CKAN with Apache on Ubuntu:sudo service apache2 reloadIntegrating it on your own extensionThis extension will not add anything by default to your CKAN instance (apart
from some helper actions and functions). You need to modify the schema and
templates on your project extension to add the fields and widgets.Adding thela_functionandla_servicefields to your custom schemaYou will need to extend the default CKAN schema
using theIDatasetForminterface (as described in thetutorial).The extension provides a couple of validators to use. The fields generated
should be stored as extras an namedla_functionandla_service(as per Appendix B of thedata.gov.uk harvesting guide).This snippet should be easy enough to follow (note: this assumes CKAN>=2.3):import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit


class MyPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IDatasetForm)


    # IDatasetForm

    def _modify_package_schema(self, schema):
        schema.update({
            'la_function': [toolkit.get_validator('ignore_missing'),
                            toolkit.get_validator('esd_function_validator'),
                            toolkit.get_converter('convert_to_extras')],
            'la_service': [toolkit.get_validator('ignore_missing'),
                           toolkit.get_validator('esd_service_validator'),
                           toolkit.get_converter('convert_to_extras')],
        })
        return schema

    def create_package_schema(self):
        schema = super(MyPlugin, self).create_package_schema()
        schema = self._modify_package_schema(schema)
        return schema

    def update_package_schema(self):
        schema = super(MyPlugin, self).update_package_schema()
        schema = self._modify_package_schema(schema)
        return schema

    def show_package_schema(self):
        schema = super(MyPlugin, self).show_package_schema()
        default_validators = [toolkit.get_converter('convert_from_extras'),
                              toolkit.get_validator('ignore_missing')]

        schema.update({
            'la_function': default_validators,
            'la_service': default_validators,
        })
        return schemaOn CKAN versions lower than 2.3, the validators need to be explicitly imported:import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

from ckanext.esdstandards.validators import (esd_function_validator,
                                             esd_service_validator)

class MyPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IDatasetForm)


    # IDatasetForm

    def _modify_package_schema(self, schema):
        schema.update({
            'la_function': [toolkit.get_validator('ignore_missing'),
                            esd_function_validator,
                            toolkit.get_converter('convert_to_extras')],
            'la_service': [toolkit.get_validator('ignore_missing'),
                           esd_service_validator,
                           toolkit.get_converter('convert_to_extras')],
        })
        return schema

    def create_package_schema(self):
        schema = super(MyPlugin, self).create_package_schema()
        schema = self._modify_package_schema(schema)
        return schema

    def update_package_schema(self):
        schema = super(MyPlugin, self).update_package_schema()
        schema = self._modify_package_schema(schema)
        return schema

    def show_package_schema(self):
        schema = super(MyPlugin, self).show_package_schema()
        default_validators = [toolkit.get_converter('convert_from_extras'),
                              toolkit.get_validator('ignore_missing')]

        schema.update({
            'la_function': default_validators,
            'la_service': default_validators,
        })
        return schemaAdding the Functions and Services fields to the dataset formThe extension provides two covenient snippets that will add all the necessary
markup and scripts to the templates. You need to extend thepackage_basic_fields.htmltemplate on your own extension with the following:# ckanext-yourext/ckanext/yourext/templates/package/snippets/package_basic_fields.html

{% ckan_extends %}

{% block package_basic_fields_custom %}

  {% snippet 'snippets/esd_functions.html', data=data, errors=errors %}

  {% snippet 'snippets/esd_services.html', data=data, errors=errors %}

{% endblock %}You should see a couple of new fields added, similar to the one for defining tags:Adding the field values on the dataset pageJust extend  theadditional_info.htmltemplate on your own extension with the following:# ckanext-yourext/ckanext/yourext/templates/package/snippets/additional_info.html

{% ckan_extends %}

{% block extras %}

  {{ super() }}

  {% snippet 'snippets/esd_functions_additional_info.html', data=pkg_dict %}

  {% snippet 'snippets/esd_services_additional_info.html', data=pkg_dict %}

{% endblock %}You can passdisplay_row_if_missing=Falseto the snippet to completely hide the
row if no values are defined.The snippets will show the fields rendered like that:Development InstallationTo install ckanext-esdstandards for development, activate your CKAN virtualenv and
do:git clone https://github.com/okfn/ckanext-esdstandards.git
cd ckanext-esdstandards
python setup.py developRunning the TestsTo run the tests, do:nosetests --nologcapture --ckan --with-pylons=test.iniRegistering ckanext-esdstandards on PyPIckanext-esdstandards should be availabe on PyPI ashttps://pypi.python.org/pypi/ckanext-esdstandards. If that link doesn’t work, then
you can register the project on PyPI for the first time by following these
steps:Create a source distribution of the project:python setup.py sdistRegister the project:python setup.py registerUpload the source distribution to PyPI:python setup.py sdist uploadTag the first release of the project on GitHub with the version number from
thesetup.pyfile. For example if the version number insetup.pyis
0.0.1 then do:git tag 0.0.1
git push --tagsReleasing a New Version of ckanext-esdstandardsckanext-esdstandards is availabe on PyPI ashttps://pypi.python.org/pypi/ckanext-esdstandards.
To publish a new version to PyPI follow these steps:Update the version number in thesetup.pyfile.
SeePEP 440for how to choose version numbers.Create a source distribution of the new version:python setup.py sdistUpload the source distribution to PyPI:python setup.py sdist uploadTag the new release of the project on GitHub with the version number from
thesetup.pyfile. For example if the version number insetup.pyis
0.0.2 then do:git tag 0.0.2
git push --tags