From 0771fd70f6940b6de70fdef10c65c97bc2265d35 Mon Sep 17 00:00:00 2001 From: Jonathan Berry Date: Wed, 1 Sep 2021 17:11:34 +0100 Subject: [PATCH 1/4] ADX-738 Autofill validator --- ckanext/scheming/unaids_validators.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ckanext/scheming/unaids_validators.py b/ckanext/scheming/unaids_validators.py index 3af8abd8..669e6f1b 100644 --- a/ckanext/scheming/unaids_validators.py +++ b/ckanext/scheming/unaids_validators.py @@ -109,6 +109,17 @@ def validator(key, data, errors, context): return validator +@scheming_validator +def autofill(field, schema): + field_value = field.get(u'field_value', field.get('default', '')) + + def validator(key, data, errors, context): + if not data.get(key): + data[key] = field_value + + return validator + + def __lower_formatter(input): return input.lower() From 0fa3118d07096d3efa17c24974321751fceb10cc Mon Sep 17 00:00:00 2001 From: Jonathan Berry Date: Wed, 1 Sep 2021 17:12:18 +0100 Subject: [PATCH 2/4] ADX-738 Add autofill validator to the hidden_value preset --- ckanext/scheming/presets.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ckanext/scheming/presets.json b/ckanext/scheming/presets.json index ccc0e7d0..4583dbb9 100644 --- a/ckanext/scheming/presets.json +++ b/ckanext/scheming/presets.json @@ -150,7 +150,8 @@ { "preset_name": "hidden_value", "values": { - "form_snippet": "hidden_fields.html" + "form_snippet": "hidden_fields.html", + "validators": "autofill" } }, { From 8f531ffa772d19d95fe42a7445c4410dc6a8aae7 Mon Sep 17 00:00:00 2001 From: Jonathan Berry Date: Wed, 1 Sep 2021 17:12:53 +0100 Subject: [PATCH 3/4] ADX-738 Register the autofill validator. --- ckanext/scheming/plugins.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ckanext/scheming/plugins.py b/ckanext/scheming/plugins.py index 775c1d36..9410b849 100644 --- a/ckanext/scheming/plugins.py +++ b/ckanext/scheming/plugins.py @@ -151,7 +151,8 @@ def get_validators(self): 'autogenerate': unaids_validators.autogenerate, 'unique_combination': unaids_validators.unique_combination, 'auto_create_valid_name': unaids_validators.auto_create_valid_name, - 'scheming_shapefile': unaids_validators.scheming_shapefile + 'scheming_shapefile': unaids_validators.scheming_shapefile, + 'autofill': unaids_validators.autofill } @run_once_for_caller('_scheming_add_template_directory', lambda: None) From e3fe97772fcc53f46b0cdddb7eeff6bc95038a02 Mon Sep 17 00:00:00 2001 From: Jonathan Berry Date: Wed, 1 Sep 2021 17:46:06 +0100 Subject: [PATCH 4/4] ADX-738 Test validator --- .../tests/schemas/autofill_validator.json | 44 +++++++++++++++++++ .../scheming/tests/test_unaids_validators.py | 23 ++++++++++ test.ini | 1 + 3 files changed, 68 insertions(+) create mode 100644 ckanext/scheming/tests/schemas/autofill_validator.json diff --git a/ckanext/scheming/tests/schemas/autofill_validator.json b/ckanext/scheming/tests/schemas/autofill_validator.json new file mode 100644 index 00000000..26b94108 --- /dev/null +++ b/ckanext/scheming/tests/schemas/autofill_validator.json @@ -0,0 +1,44 @@ +{ + "scheming_version": 1, + "dataset_type": "autofill-validator", + "name": "Auto create valid name", + "link_name": "Auto create valid name", + "about": "", + "about_url": "http://github.com/ckan/ckanext-scheming", + "add_data_on_create": false, + "dataset_fields": [{ + "field_name": "title", + "label": "Title:", + "form_snippet": "text.html", + "classes": ["hidden"] + }, + { + "field_name": "name", + "label": "URL", + "form_snippet": "text.html", + "validators": "auto_create_valid_name not_empty unicode name_validator package_name_validator", + "classes": ["hidden"] + }, + { + "label": "Location", + "field_name": "location" + }, + { + "label": "Year", + "field_name": "year", + "validators": "autofill" + }, + { + "label": "Schema", + "field_name": "schema", + "field_value": "art_3", + "validators": "autofill" + }, + { + "label": "Type", + "field_name": "dataset_type", + "default": "data-type", + "validators": "autofill" + } + ] +} diff --git a/ckanext/scheming/tests/test_unaids_validators.py b/ckanext/scheming/tests/test_unaids_validators.py index b4594a13..5a36a366 100644 --- a/ckanext/scheming/tests/test_unaids_validators.py +++ b/ckanext/scheming/tests/test_unaids_validators.py @@ -41,3 +41,26 @@ def test_handles_deleted_datasets(self): type="auto-create-valid-name", year="2020", location="north-pole" ) assert dataset3['name'] == u'north-pole-autogenerate-2020-1' + + +@pytest.mark.usefixtures(u'clean_db') +@pytest.mark.usefixtures(u'clean_index') +class TestAutofill(object): + def test_autofilling(self): + lc = LocalCKAN() + dataset = lc.action.package_create( + type="autofill-validator", title="AutoFill", name='autofill', location="north-pole" + ) + assert dataset['schema'] == u'art_3' + assert dataset['dataset_type'] == u'data-type' + assert dataset['year'] == u'' + + def test_not_overwriting(self): + lc = LocalCKAN() + dataset = lc.action.package_create( + type="autofill-validator", title="AutoFill", name='autofill', location="north-pole", + schema="art_4", dataset_type="different-data-type", year="1984" + ) + assert dataset['schema'] == u'art_4' + assert dataset['dataset_type'] == u'different-data-type' + assert dataset['year'] == u'1984' diff --git a/test.ini b/test.ini index 6f145d1d..e1a91105 100644 --- a/test.ini +++ b/test.ini @@ -17,6 +17,7 @@ scheming.dataset_schemas = ckanext.scheming:ckan_dataset.json ckanext.scheming.tests:test_schema.json ckanext.scheming.tests:test_datastore_choices.json ckanext.scheming.tests.schemas:auto_unique_validator.json + ckanext.scheming.tests.schemas:autofill_validator.json scheming.organization_schemas = ckanext.scheming:org_with_dept_id.json ckanext.scheming:custom_org_with_address.json scheming.group_schemas = ckanext.scheming:group_with_bookface.json