diff --git a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/plugin.py b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/plugin.py index 4506813c..a0f895d1 100644 --- a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/plugin.py +++ b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/plugin.py @@ -93,7 +93,8 @@ def get_validators(self): validators.convert_to_json_compatible_str_if_str, 'required_languages': validators.required_languages, 'highvalue_category': validators.highvalue_category, - 'highvalue': validators.highvalue + 'highvalue': validators.highvalue, + 'populate_required_languages_from_field_if_missing': validators.populate_required_languages_from_field_if_missing, } # IPackageController diff --git a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/schemas/dataset.json b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/schemas/dataset.json index d8e128d7..f472a1fd 100644 --- a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/schemas/dataset.json +++ b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/schemas/dataset.json @@ -302,13 +302,18 @@ "sv", "en" ], + "required_languages": [ + "fi", + "sv" + ], "form_placeholder": "e.g. Most popular Finnish first names 2019", "form_attrs": { "class": "form-control" }, "description": "Give a short and descriptive name for the distribution. If the data covers a specific time frame, mention that in the name.", "group_title": "Data resource title", - "group_description": "* Required field" + "group_description": "* Required field", + "validators": "fluent_text populate_required_languages_from_field_if_missing(url)" }, { "field_name": "url", diff --git a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/tests/test_plugin.py b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/tests/test_plugin.py index 91ee574d..3273b827 100644 --- a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/tests/test_plugin.py +++ b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/tests/test_plugin.py @@ -104,6 +104,7 @@ def test_minimal_dataset(): assert len(dataset['resources']) == 1 resource = dataset['resources'][0] assert resource['url'] == dataset_fields['resources'][0]['url'] + assert resource['name_translated']['fi'] == resource['url'] @pytest.mark.usefixtures("with_plugins", "clean_db") diff --git a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/validators.py b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/validators.py index ecfe8f08..57325bb2 100644 --- a/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/validators.py +++ b/ckan/ckanext/ckanext-restricteddata/ckanext/restricteddata/validators.py @@ -192,6 +192,40 @@ def validator(key, data, errors, context): return implementation +def populate_required_languages_from_field_if_missing(field_name): + @scheming_validator + def validator(field, schema): + def inner(key, data, errors, context): + languages = field.get('required_languages', []) + value = {} if data[key] is toolkit.missing else data[key] + # Assumption: subject field is a sibling to the object field + default_value = data[key[:-1] + (field_name,)] + + if isinstance(value, str): + try: + value = json.loads(value) + except ValueError: + errors[key].append(_('Failed to decode JSON string')) + return + except UnicodeDecodeError: + errors[key].append(_('Invalid encoding for JSON string')) + return + + if not isinstance(value, dict): + errors[key].append(_('expecting JSON object')) + return + + for lang in languages: + if value.get(lang, '') == '': + value[lang] = default_value + + data[key] = json.dumps(value) + + return inner + + return validator + + def create_fluent_tags(vocab): def callable(key, data, errors, context): value = data[key] diff --git a/robot/restricteddata.robot b/robot/restricteddata.robot index 95d9b32a..7f41caa5 100644 --- a/robot/restricteddata.robot +++ b/robot/restricteddata.robot @@ -312,17 +312,13 @@ Fill Dataset Form With Full Test Data Input Text id:field-maintainer_website ${maintenance website} Fill Resource Form With Minimal Test Data - [Arguments] ${name fi}=Testiresurssi - ... ${name sv}=Test resurs - ... ${description fi}=Testiresurssin kuvaus + [Arguments] ${description fi}=Testiresurssin kuvaus ... ${description sv}=Test resurs beskrivning ... ${url}=https://example.com ... ${format}=HTML ... ${size}=12345 ... ${rights fi}=Testiresurssin käyttöoikeuksien kuvaus ... ${rights sv}=Test resurs användningsrättigheter - Input Text id:field-name_translated-fi ${name fi} - Input Text id:field-name_translated-sv ${name sv} TRY Click Button id:resource-link-button EXCEPT @@ -355,10 +351,11 @@ Fill Resource Form With Full Test Data ... ${temporal coverage from}=01/02/2023 ... ${temporal coverage till}=03/04/2033 ... ${geographical accuracy}=42 - Fill Resource Form With Minimal Test Data name fi=${name fi} name sv=${name sv} - ... description fi=${description fi} description sv=${description sv} + Fill Resource Form With Minimal Test Data description fi=${description fi} description sv=${description sv} ... url=${url} format=${format} size=${size} ... rights fi=${rights fi} rights sv=${rights sv} + Input Text id:field-name_translated-fi ${name fi} + Input Text id:field-name_translated-sv ${name sv} Input Text id:field-name_translated-en Test resource Input Text Into CKEditor field-description_translated-fi ${description fi} Input Text Into CKEditor field-description_translated-sv ${description sv} diff --git a/robot/tests/dataset.robot b/robot/tests/dataset.robot index 47205997..a2db8df4 100644 --- a/robot/tests/dataset.robot +++ b/robot/tests/dataset.robot @@ -17,7 +17,7 @@ Create Minimal Dataset And Resource Submit Primary Form URL Path Should Be /dataset/testiaineisto/resource/new - Fill Resource Form With Minimal Test Data + Fill Resource Form With Minimal Test Data url=http://example.com/test-resource Submit Primary Form URL Path Should Be /dataset/testiaineisto @@ -25,7 +25,7 @@ Create Minimal Dataset And Resource Page Should Contain Teemu Testaaja Page Should Contain teemu.testaaja@example.com - Click Link link:Testiresurssi + Click Link http://example.com/test-resource Page Should Contain 12345 Page Should Contain Testiresurssin käyttöoikeuksien kuvaus diff --git a/robot/tests/distribution.robot b/robot/tests/distribution.robot index c669c1e5..05a57051 100644 --- a/robot/tests/distribution.robot +++ b/robot/tests/distribution.robot @@ -110,11 +110,11 @@ Remove Distribution Submit Primary Form URL Path Should Be /dataset/testiaineisto/resource/new - Fill Resource Form With Minimal Test Data + Fill Resource Form With Minimal Test Data url=http://example.com/test-resource Submit Primary Form URL Path Should Be /dataset/testiaineisto - Click Link Testiresurssi + Click Link http://example.com/test-resource Click Link Muokkaa Scroll To Form Actions