Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to create an arbitrary schema for rendering elsewhere #403

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

mutantsan
Copy link
Contributor

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.

{%- endif -%}
{%- endfor -%}

{% snippet 'scheming/snippets/render_fields.html', fields=schema.dataset_fields, data=data, errors=errors, entity_type='dataset', object_type=dataset_type, set_fields_defaults=true %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like licenses isn't passed through here or in the resource_form

Copy link
Contributor Author

@mutantsan mutantsan Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@wardi
Copy link
Contributor

wardi commented Jan 26, 2024

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

@mutantsan
Copy link
Contributor Author

@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?

entity_type - entity type
object_type - object type
set_fields_defaults - flag to set the default field values
{#}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cute but doing comments like this could lead to confusion about where the comment starts and ends

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{#}

{% for field in fields if field.form_snippet is not none %}
{% if field.field_name not in data and set_fields_defaults %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other templates are indented with 2 spaces. Better to be consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks

setup.py Outdated
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '3.0.0'
version = '3.0.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll bump the version number when doing a release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants