Skip to content

Commit

Permalink
feat: Enable saving and editing relations via GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
bellisk committed Feb 12, 2025
1 parent 94c5f55 commit 091bca0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
55 changes: 37 additions & 18 deletions ckanext/switzerland/helpers/dataset_form_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ckanext.switzerland.helpers.frontend_helpers import (
get_frequency_name, get_dataset_by_identifier, get_dataset_by_permalink)
from ckanext.switzerland.helpers.localize_utils import (
localize_by_language_order)
LANGUAGES, localize_by_language_order)
from ckanext.switzerland.helpers.terms_of_use_utils import (
TERMS_OF_USE_BY_ASK, TERMS_OF_USE_OPEN, TERMS_OF_USE_BY, TERMS_OF_USE_ASK)
from dateutil.parser import parse
Expand Down Expand Up @@ -114,9 +114,10 @@ def _convert_from_publisher_deprecated(data):


def _build_rows_form_field(data_empty, data_list=None):
"""builds a rows form field
"""Builds a rows form field.
- gets a list of data to fill in the form
- the form is build with that data
- the form is built with that data
- rows that are empty are set to hidden
- when there is no data the first row is displayed
"""
Expand Down Expand Up @@ -177,46 +178,64 @@ def get_contact_points_from_form(data):


def ogdch_relations_form_helper(data):
"""
sets the form field for relations
"""Sets up the form field for relations.
"relations": [
{"label": "legal_basis", "url": "https://www.admin.ch/#a20"},
{"label": "legal_basis", "url": "https://www.admin.ch/#a21"}]
{"label": {"de: "text", "fr": "text", "it": "text", "en": "text"},
"url": "https://www.admin.ch/#a20"},
...
]
"""
relations = _get_relations_from_storage(data)
if not relations:
relations = get_relations_from_form(data)

data_empty = {'title': '', 'url': ''}
data_empty = {'label': {'de': '', 'en': '', 'fr': '', 'it': ''}, 'url': ''}
rows = _build_rows_form_field(
data_empty=data_empty,
data_list=relations)
return rows


def _get_relations_from_storage(data):
"""
data is expected to be stored as:
"""Relations data is expected to be stored as:
"relations": [
{"label": "legal_basis", "url": "https://www.admin.ch/#a20"},
{"label": "legal_basis", "url": "https://www.admin.ch/#a21"}]
{"label": {"de: "text", "fr": "text", "it": "text", "en": "text"},
"url": "https://www.admin.ch/#a20"},
...
]
"""
relations = data.get('relations')
result = []
if relations:
return [{"title": relation["label"], "url": relation["url"]}
for relation in relations]
return None
for relation in relations:
if isinstance(relation['label'], dict):
result.append(relation)
else:
result.append(
{
'label': {
'de': relation['label'],
'fr': relation['label'],
'it': relation['label'],
'en': relation['label']
},
'url': relation['url']
})
return result


def get_relations_from_form(data):
if isinstance(data, dict):
relations = []
for i in range(1, ADDITIONAL_FORM_ROW_LIMIT + 1):
title = data.get('relation-title-' + str(i), '')
label = {
lang: data.get('relation-label-{}-{}'.format(str(i), lang), '')
for lang in LANGUAGES
}
url = data.get('relation-url-' + str(i), '')
if title or url:
if any(label.values()) or url:
relations.append(
{'title': title,
{'label': label,
'url': url})
return relations
return None
Expand Down
12 changes: 6 additions & 6 deletions ckanext/switzerland/helpers/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,21 @@ def validator(key, data, errors, context):
@scheming_validator
def ogdch_validate_formfield_relations(field, schema):
"""This validator is only used for form validation
The data is extracted form the publisher form fields and transformed
The data is extracted from the publisher form fields and transformed
into a form that is expected for database storage:
"relations": [
{"label": "legal_basis", "url": "https://www.admin.ch/#a20"},
{"label": "legal_basis", "url": "https://www.admin.ch/#a21"}]
{"label": {"de: "text", "fr": "text", "it": "text", "en": "text"},
"url": "https://www.admin.ch/#a20"},
...
]
"""
def validator(key, data, errors, context):

extras = data.get(FORM_EXTRAS)
if extras:
relations = get_relations_from_form(extras)
if relations:
output = [{'label': relation['title'], 'url': relation['url']}
for relation in relations]
data[key] = json.dumps(output)
data[key] = json.dumps(relations)
elif not _jsondata_for_key_is_set(data, key):
data[key] = '{}'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{% import 'macros/form.html' as form %}

{% set relations_rows = h.ogdch_relations_form_helper(data) %}
{%- for index in range(relations_rows | length) -%}
{% set row = relations_rows[index] %}
<h4 class="relation-label {{row.css_class}}">{{h.scheming_language_text(field.label) ~ ' ' ~ index}}</h4>
{%- for row in relations_rows -%}
<h4 class="relation-label {{row.css_class}}">{{h.scheming_language_text(field.label) ~ ' ' ~ row.index}}</h4>

{% for lang in h.fluent_form_languages(field, entity_type, object_type, schema) %}
{% call form.input(
'relation-title-' ~ row.index ~ '-' ~ lang,
id='field-relation-title-' ~ row.index ~ '-' ~ lang,
'relation-label-' ~ row.index ~ '-' ~ lang,
id='field-relation-label-' ~ row.index ~ '-' ~ lang,
label=lang.upper() ~ ' ' ~ h.scheming_language_text(field.label_text, lang),
placeholder=h.scheming_language_text(field.form_placeholder_text, lang),
value=data[field.field_name ~ '-' ~ lang]
or data.get(field.field_name, {})[lang],
error=errors[field.field_name ~ '-' ~ lang],
value=row.data.label[lang],
error=errors['relations'],
classes=['control-medium', row.css_class]
) %}
{% endcall %}
Expand Down

0 comments on commit 091bca0

Please sign in to comment.