diff --git a/ckanext/switzerland/dcat-ap-switzerland_scheming.json b/ckanext/switzerland/dcat-ap-switzerland_scheming.json index 75794a537..c2dc4646c 100644 --- a/ckanext/switzerland/dcat-ap-switzerland_scheming.json +++ b/ckanext/switzerland/dcat-ap-switzerland_scheming.json @@ -130,7 +130,7 @@ "fr": "Mots clés", "it": "Parole chiave" }, - "preset": "fluent_tags", + "preset": "ogdch_fluent_tags", "display_snippet": "keywords.html" }, { diff --git a/ckanext/switzerland/plugin.py b/ckanext/switzerland/plugin.py index 2851918a8..6b95b12b0 100644 --- a/ckanext/switzerland/plugin.py +++ b/ckanext/switzerland/plugin.py @@ -42,6 +42,7 @@ def get_validators(self): "timestamp_to_datetime": v.timestamp_to_datetime, "ogdch_multiple_choice": v.ogdch_multiple_choice, "ogdch_unique_identifier": v.ogdch_unique_identifier, + "ogdch_fluent_tags": v.ogdch_fluent_tags, "temporals_to_datetime_output": v.temporals_to_datetime_output, "json_list_of_dicts_field": v.json_list_of_dicts_field, "swiss_date": v.swiss_date, diff --git a/ckanext/switzerland/presets.json b/ckanext/switzerland/presets.json index d47e89513..ceb1cd7c2 100644 --- a/ckanext/switzerland/presets.json +++ b/ckanext/switzerland/presets.json @@ -63,17 +63,6 @@ "form_snipper": "slug.html" } }, - { - "preset_name": "tag_string_autocomplete", - "values": { - "validators": "ignore_missing tag_string_convert", - "form_attrs": { - "data-module": "autocomplete", - "data-module-tags": "", - "data-module-source": "/api/2/util/tag/autocomplete?incomplete=?" - } - } - }, { "preset_name": "dataset_organization", "values": { @@ -144,6 +133,21 @@ "validators": "ignore_empty natural_number_validator", "display_snippet": "file_size.html" } + }, + { + "preset_name": "ogdch_fluent_tags", + "values": { + "form_snippet": "fluent_tags.html", + "display_snippet": "fluent_tags.html", + "error_snippet": "fluent_text.html", + "validators": "fluent_tags ogdch_fluent_tags", + "output_validators": "fluent_tags_output", + "form_attrs": { + "data-module": "autocomplete", + "data-module-tags": "", + "data-module-source": "/api/2/util/tag/autocomplete?incomplete=?" + } + } } ] } diff --git a/ckanext/switzerland/templates/scheming/display_snippets/fluent_tags.html b/ckanext/switzerland/templates/scheming/display_snippets/fluent_tags.html new file mode 100644 index 000000000..2392939aa --- /dev/null +++ b/ckanext/switzerland/templates/scheming/display_snippets/fluent_tags.html @@ -0,0 +1,19 @@ +
+ {% set tags = data[field.field_name] %} + {% if tags %} + {% block tag_list %} + + {% endblock %} + {% else %} + - + {% endif %} +
\ No newline at end of file diff --git a/ckanext/switzerland/validators.py b/ckanext/switzerland/validators.py index 171ae7ff8..3e83c4182 100644 --- a/ckanext/switzerland/validators.py +++ b/ckanext/switzerland/validators.py @@ -5,6 +5,7 @@ from collections import defaultdict import ckan.lib.navl.dictization_functions as df +from ckan.lib.munge import munge_tag from ckan.logic import NotFound, get_action from ckan.plugins.toolkit import _, missing @@ -338,3 +339,38 @@ def validator(key, data, errors, context): raise df.Invalid(_("Invalid URL")) return validator + + +@scheming_validator +def ogdch_fluent_tags(field, schema): + """ + To be called after ckanext-fluent fluent_tags() because of an error that + does not save any tag data for a language that has no tags entered, e.g. it + would save {"de": ["tag-de"]} if German were the only language with a tag + entered in the form. Not saving tag data for all the languages causes the + tags to later be interpreted as a string, so here the dataset would display + the tag '{u"de": [u"tag-de"]}' in every language. + + What we need to do in this case is save the tag field thus: + {"de": ["tag-de"], "fr": [], "en": [], "it": []} + + Tags are munged to contain only lowercase letters, numbers, and the + characters `-_.` + """ + + def validator(key, data, errors, context): + if errors[key]: + return + + value = json.loads(data[key]) + new_value = {} + for lang in schema["form_languages"]: + new_value[lang] = [] + if lang not in value.keys(): + continue + for keyword in value[lang]: + new_value[lang].append(munge_tag(keyword)) + + data[key] = json.dumps(new_value) + + return validator