ckan / ckanext-pages

A simple builtin CMS for CKAN sites
GNU General Public License v3.0
51 stars 99 forks source link

iPagesSchema for custom schemas #95

Closed pdelboca closed 4 years ago

pdelboca commented 4 years ago

This PR implements an interfaz similar to IConfigurer to allow other extensions to extend and customize the pages schema and also tidy up a little bit the files and code to follow some CKAN conventions.

Details

This extension already provides a feature to save extras when creating a page however the schema is hardcoded in the actions.py file so there is no way to override it to add custom fields that may be needed.

Example of use

In a new extension implement IPagesSchema and override update_pages_schema() to add new custom fields and validators:

import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckanext.pages.interfaces import IPagesSchema

class MyextPlugin(plugins.SingletonPlugin):
    plugins.implements(IPagesSchema)

    #IPagesSchema
    def update_pages_schema(self, schema):
        schema.update({
            'new_field': [
                toolkit.get_validator('not_empty'),
                toolkit.get_validator('boolean_validator')]
            })
        return schema

and also extends base_form.html and override the extra_pages_form block:

{% ckan_extends %}

{% set options = [{'value': True, 'text': _('Yes')}, {'value': False, 'text': _('No')}]%}
{% block extra_pages_form %}
    {{ form.select('new_field', id = 'new_field', label = 'New Field', options=options, selected=data.testing) }}
{% endblock extra_pages_form %}

Tasks:

amercader commented 4 years ago

This is great @pdelboca, thanks