ckanext-scheming icon indicating copy to clipboard operation
ckanext-scheming copied to clipboard

Add the ability to create an arbitrary schema for rendering elsewhere

Open mutantsan opened this issue 1 year ago • 2 comments

Right now I'm working on an extended admin panel and I need to create several configurational pages for different plugins. Since we are using ckanext-scheming for all of our CKAN projects, I assume it's a good idea to make it more flexible.

The plugin is oriented toward creating a schema for predefined entity types (dataset, group & organization), but it already has the power to make much more. I suggest allowing defining arbitrary schemas with a unique schema_id. This schema could be rendered as independent fields with a render_fields.html snippet.

It's defined as a separate extension inside ckanext-scheming, that must be enabled to work with (same as other parts of the scheming). The schema follows the same format as group & organization schemas, except for the schema_id field. That is a unique identifier of a schema. Example of a schema:

scheming_version: 2
schema_id: ckanext_notifier
about: An example of a config schema for a fictional extension

fields:
  - field_name: ckanext.ckanext_notifier.enable_notifications
    label: Enable notifications
    validators: default(true) boolean_validator
    preset: select
    required: true
    choices:
      - value: true
        label: Enable
      - value: false
        label: Disable

  - field_name: ckanext.ckanext_notifier.notify_to_email
    label: Notification email
    validators: unicode_safe email_validator
    required: true
    help_text: Specify the email address to which the notification will be sent

  - field_name: ckanext.ckanext_notifier.frequency
    label: Notification frequency in seconds
    validators: default(3600) int_validator
    required: true
    input_type: number

When the schema is created and registered via CKAN config, we can fetch it with a scheming_get_arbitrary_schema helper. Then in a template, we can render a form like this:

<form method="POST">
    {% snippet 'scheming/snippets/render_fields.html', fields=schema.fields, data=data, errors=errors %}

    <button type="submit" class="btn btn-primary">{{ _('Update') }}</button>
</form>

And get the next result: image I wrote tests but didn't update the documentation yet. I'm going to do it if we are fine with those changes.

mutantsan avatar Jan 26 '24 16:01 mutantsan

@mutantsan sounds like a good idea, check the test failures and add some docs for the expected usage

wardi avatar Jan 26 '24 23:01 wardi

@wardi I've fixed the tests and wrote some doc. I'm not sure about the Tests / CKAN 2.8 (pull_request) Failing after 59s. It seems, that something went wrong during the initialization of the env for tests. Do you know how to fix it?

mutantsan avatar Feb 12 '24 13:02 mutantsan