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

Display publisher as multilang #339

Merged
merged 13 commits into from
Jan 6, 2025
1 change: 1 addition & 0 deletions ckanext/switzerland/dcat-ap-switzerland_scheming.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@
"validators": "ogdch_validate_formfield_publisher",
"display_snippet": "publisher.html",
"form_snippet": "publisher.html",
"preset": "multilingual_text_full",
"mark_required": true,
"section_title": {
"en": "Publisher Information",
Expand Down
32 changes: 26 additions & 6 deletions ckanext/switzerland/helpers/dataset_form_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
ADDITIONAL_FORM_ROW_LIMIT = 10
HIDE_ROW_CSS_CLASS = 'ogdch-hide-row'
SHOW_ROW_CSS_CLASS = 'ogdch-show-row'
PUBLISHER_EMPTY = {'name': '', 'url': ''}
ORGANIZATION_URI_BASE = 'https://opendata.swiss/organization/'

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,22 +48,43 @@ def ogdch_publisher_form_helper(data):
fills the publisher form snippet either from a previous form entry
or from the db
"""
publisher_form_name = data.get('publisher-name')

# check for form inputs first
publisher_form_name = {
'fr': data.get('publisher-name-fr', ''),
'en': data.get('publisher-name-en', ''),
'de': data.get('publisher-name-de', ''),
'it': data.get('publisher-name-it', ''),
}
publisher_form_url = data.get('publisher-url')
publisher_in_form = publisher_form_url or publisher_form_name
if publisher_in_form:

if publisher_form_url or any(publisher_form_name.values()):
return {'name': publisher_form_name,
'url': publisher_form_url}

# check for publisher from db
publisher_stored = data.get('publisher')
if publisher_stored:
return json.loads(publisher_stored)
# handle stored publisher data (both as dict or string)
if isinstance(publisher_stored, dict):
name = publisher_stored.get('name', {})
return {'name': name, 'url': publisher_stored.get('url', '')}
elif isinstance(publisher_stored, str):
try:
parsed = json.loads(publisher_stored)
name = parsed.get('name', '')
return {'name': {'de': name, 'en': '', 'fr': '', 'it': ''},
'url': parsed.get('url', '')}
except (ValueError, TypeError):
log.error('Failed to parse stored publisher JSON')
return {'name': {'de': '', 'en': '', 'fr': '', 'it': ''},
'url': ''}

publisher_deprecated = _convert_from_publisher_deprecated(data)
if publisher_deprecated:
return publisher_deprecated

return {'name': '', 'url': ''}
return {'name': {'de': '', 'en': '', 'fr': '', 'it': ''}, 'url': ''}


def _convert_from_publisher_deprecated(data):
Expand Down
29 changes: 22 additions & 7 deletions ckanext/switzerland/helpers/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
get_contact_points_from_form, get_relations_from_form,
get_qualified_relations_from_form, get_temporals_from_form)
from ckanext.switzerland.helpers.frontend_helpers import get_permalink
from ckanext.switzerland.helpers.localize_utils import parse_json
from ckanext.switzerland.helpers.localize_utils import parse_json, LANGUAGES

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -322,23 +322,38 @@ def ogdch_validate_formfield_publisher(field, schema):
"""This validator is only used for form validation
The data is extracted from the publisher form fields and transformed
into a form that is expected for database storage:
'{"name": "Publisher Name", "url": "Publisher URL"}'
'{"name": {"de": "German Name", "en": "English Name", "fr": "French Name",
"it": "Italian Name"}, "url": "Publisher URL"}'
"""
def validator(key, data, errors, context):
# the value is already a dict
if isinstance(data.get(key), dict):
data[key] = json.dumps(data.get(key))
return # exit early since this case is handled
# the key is missing
if not data.get(key):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this if statement and un-indent everything beneath it by one tab

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not do this, actually I would remove

if isinstance(data.get(key), dict):
            data[key] = json.dumps(data.get(key))
            return

And really validate the missing value here. If we will remove if not data.get(key)
It could lead to overwriting valid data, like str value (from foaf:Organization).

extras = data.get(FORM_EXTRAS)
output = {'url': '', 'name': ''}
output = {'url': '',
'name': {'de': '', 'en': '', 'fr': '', 'it': ''}}
if extras:
publisher = _get_publisher_from_form(extras)
if publisher:
output = publisher
output['name'] = {
'de': extras.get('publisher-name-de', ''),
'en': extras.get('publisher-name-en', ''),
'fr': extras.get('publisher-name-fr', ''),
'it': extras.get('publisher-name-it', ''),
}
if 'publisher-url' in extras:
del extras['publisher-url']
if 'publisher-name' in extras:
del extras['publisher-name']
if any(key.startswith('publisher-name-') for key in
extras.keys()):
for lang in LANGUAGES:
lang_key = 'publisher-name-{}'.format(lang)
if lang_key in extras:
del extras[lang_key]
kovalch marked this conversation as resolved.
Show resolved Hide resolved
data[key] = json.dumps(output)
elif isinstance(data.get(key), dict):
data[key] = json.dumps(data.get(key))
return validator


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{%- set publisher = h.render_publisher(data[field.field_name]) -%}
{% if publisher %}
<a href="{{ publisher.url }}" target="_blank" class="break-word">{{ publisher.name }}</a>
{%- set current_lang = h.lang() -%} {# Get the current CKAN language #}
{%- set fallback_lang = 'de' -%} {# Default fallback language #}
{% if publisher.name is string %}
<a href="{{ publisher.url }}" target="_blank" class="break-word">{{ publisher.name }}</a>
{% else %}
{%- set publisher_name = publisher.name.get(current_lang) or publisher.name.get(fallback_lang) -%}
<a href="{{ publisher.url }}" target="_blank" class="break-word">{{ publisher_name }}</a>
{% endif %}
{% else %}
-
<p>Publisher information not available.</p>
{% endif %}
28 changes: 18 additions & 10 deletions ckanext/switzerland/templates/scheming/form_snippets/publisher.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
{% import 'macros/form.html' as form %}
{% set publisher = h.ogdch_publisher_form_helper(data) %}
{% call form.input(
'publisher-name',
id='field-publisher-name',
label=h.scheming_language_text(field.label_name),
placeholder=h.scheming_language_text(field.form_placeholder_name),
value=publisher.name,
error=errors['publisher-name'],
classes=['control-full']
) %}
{% endcall %}

{# Input for publisher names in multiple languages #}
{% for lang in ['fr', 'de', 'it', 'en'] %}
{% set name_value = publisher.name.get(lang, '') if publisher.name is mapping else '' %}
{% call form.input(
'publisher-name-' ~ lang,
id='field-publisher-name-' ~ lang,
label=h.scheming_language_text(field.label_name) ~ ' (' ~ lang ~ ')',
placeholder=h.scheming_language_text(field.form_placeholder_name) ~ ' (' ~ lang ~ ')',
value=name_value,
error=errors['publisher-name-' ~ lang],
classes=['control-full']
) %}
{% endcall %}
{% endfor %}

{# Input for publisher URL #}
{% call form.input(
'publisher-url',
id='field-publisher-url',
Expand All @@ -20,4 +27,5 @@
classes=['control-full']
) %}
{% endcall %}

{%- snippet 'scheming/form_snippets/help_text.html', field=field -%}
Loading