Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/keywords #34

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ckanext/switzerland/dcat-ap-switzerland_scheming.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"fr": "Mots clés",
"it": "Parole chiave"
},
"preset": "fluent_tags",
"preset": "ogdch_fluent_tags",
"display_snippet": "keywords.html"
},
{
Expand Down
1 change: 1 addition & 0 deletions ckanext/switzerland/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 15 additions & 11 deletions ckanext/switzerland/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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=?"
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<section class="tags">
{% set tags = data[field.field_name] %}
{% if tags %}
{% block tag_list %}
<ul class="tag-list">
{% for tag in data[field.field_name] %}
{% set tag_dict = {'keywords_' + h.lang(): tag} %}
<li>
<a class="{% block tag_list_item_class %}tag{% endblock %}" href="{{
h.url_for(controller='package', action='search', **tag_dict) }}">{{
h.truncate(tag, 22) }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
{% else %}
-
{% endif %}
</section>
36 changes: 36 additions & 0 deletions ckanext/switzerland/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Loading