Skip to content

Commit

Permalink
Merge pull request #352 from vrk-kpa/REKDAT-229_populate-distribution…
Browse files Browse the repository at this point in the history
…-name-from-url

REKDAT-229: Get default translations for distribution name from url field
  • Loading branch information
bzar authored Dec 10, 2024
2 parents a3de8b5 + d9c1f56 commit 0f483fc
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 4 additions & 7 deletions robot/restricteddata.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions robot/tests/dataset.robot
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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
Page Should Contain Testiaineiston kuvaus
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

Expand Down
4 changes: 2 additions & 2 deletions robot/tests/distribution.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f483fc

Please sign in to comment.