From 86e325f7aafa6b196bce114e57c1a966133bb045 Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Fri, 8 Nov 2024 16:42:57 +0000 Subject: [PATCH] feat(dev): group before and after validators; - Added before and after validators to README. - Added before and after validators to org and group schemas. - Added error block to group and org form templates. --- README.md | 20 +++++++++++++++++++ ckanext/scheming/plugins.py | 10 ++++++++++ .../templates/scheming/group/group_form.html | 8 ++++++++ .../scheming/organization/group_form.html | 8 ++++++++ 4 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 1b6ce2b8..bfa40063 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,15 @@ Fields you exclude will not be shown to the end user, and will not be accepted when editing or updating this type of dataset. +### `before_validators`, `after_validators` + +```yaml +before_validators: validator_name + +after_validators: validator_name +``` + +Runs validator functions before and after the `dataset_type` package is created/updated. ## Group / Organization Schema Keys @@ -231,6 +240,17 @@ fields: A single `fields` list replaces the `dataset_fields` and `resource_fields` schema properties doin dataset schemas. +### `before_validators`, `after_validators` + +```yaml +before_validators: validator_name + +after_validators: validator_name +``` + +Runs validator functions before and after the `organization_type`/`group_type` group is created/updated. + + ---------------- ## Field Keys diff --git a/ckanext/scheming/plugins.py b/ckanext/scheming/plugins.py index 0980a684..e947d602 100644 --- a/ckanext/scheming/plugins.py +++ b/ckanext/scheming/plugins.py @@ -177,6 +177,16 @@ def validate(self, context, data_dict, schema, action): scheming_schema = self._expanded_schemas[t] scheming_fields = scheming_schema['fields'] + before = scheming_schema.get('before_validators') + after = scheming_schema.get('after_validators') + + if before: + schema['__before'] = validation.validators_from_string( + before, None, scheming_schema) + if after: + schema['__after'] = validation.validators_from_string( + after, None, scheming_schema) + get_validators = ( _field_output_validators_group if action_type == 'show' else _field_validators diff --git a/ckanext/scheming/templates/scheming/group/group_form.html b/ckanext/scheming/templates/scheming/group/group_form.html index b3cf2c9b..60f5ec07 100644 --- a/ckanext/scheming/templates/scheming/group/group_form.html +++ b/ckanext/scheming/templates/scheming/group/group_form.html @@ -10,6 +10,14 @@ {%- endif -%}
+ {% block errors %} + {%- if errors -%} + {%- set schema = h.scheming_get_group_schema(group_type) -%} + {%- snippet 'scheming/snippets/errors.html', + errors=errors, fields=schema.fields, + entity_type='group', object_type=group_type -%} + {%- endif -%} + {% endblock %} {{ h.csrf_input() if 'csrf_input' in h }} {%- set schema = h.scheming_get_group_schema(group_type) -%} {%- for field in schema['fields'] -%} diff --git a/ckanext/scheming/templates/scheming/organization/group_form.html b/ckanext/scheming/templates/scheming/organization/group_form.html index ebf78de7..10a82830 100644 --- a/ckanext/scheming/templates/scheming/organization/group_form.html +++ b/ckanext/scheming/templates/scheming/organization/group_form.html @@ -12,6 +12,14 @@ {%- endif -%} + {% block errors %} + {%- if errors -%} + {%- set schema = h.scheming_get_organization_schema(group_type) -%} + {%- snippet 'scheming/snippets/errors.html', + errors=errors, fields=schema.fields, + entity_type='organization', object_type=group_type -%} + {%- endif -%} + {% endblock %} {{ h.csrf_input() if 'csrf_input' in h }} {%- set schema = h.scheming_get_organization_schema(group_type) -%} {%- for field in schema['fields'] -%}