From 75005a572944fccfa579fce192c0ad5e74c203a4 Mon Sep 17 00:00:00 2001 From: anonymous <anonymous> Date: Wed, 9 Jan 2019 06:26:59 +0100 Subject: [PATCH] Move parts of extra-field handling into macro, compute hash for index --- ckanext/odsh/helpers.py | 7 +++++- ckanext/odsh/plugin.py | 4 ++-- ckanext/odsh/templates/macros/form.html | 7 +++++- .../snippets/package_basic_fields.html | 24 ++++++++----------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py index e3006a11..af351d91 100644 --- a/ckanext/odsh/helpers.py +++ b/ckanext/odsh/helpers.py @@ -10,6 +10,7 @@ import datetime from dateutil import parser from ckan.common import config import urllib +import hashlib get_action = logic.get_action log = logging.getLogger(__name__) @@ -109,6 +110,10 @@ def odsh_upload_known_formats(): def odsh_encodeurl(url): return urllib.quote(url, safe='') +def odsh_create_checksum(in_string): + hashstring = hashlib.md5(in_string.encode('utf-8')).hexdigest() + return int(hashstring, base=16) + def odsh_extract_error(key, errors): if not errors or not ('extras' in errors): return None @@ -124,4 +129,4 @@ def odsh_extract_value_from_extras(extras, key): return None for item in extras: if 'key' in item and item['key'].lower() == key: - return item['value'] \ No newline at end of file + return item['value'] diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py index b2b925d0..4ae1d875 100644 --- a/ckanext/odsh/plugin.py +++ b/ckanext/odsh/plugin.py @@ -74,7 +74,6 @@ def odsh_group_id_selected(selected, group_id): return False - def known_spatial_uri(key, data, errors, context): mapping_file = config.get('ckanext.odsh.spatial.mapping') try: @@ -204,7 +203,8 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm 'odsh_upload_known_formats': odsh_helpers.odsh_upload_known_formats, 'odsh_encodeurl': odsh_helpers.odsh_encodeurl, 'odsh_extract_error': odsh_helpers.odsh_extract_error, - 'odsh_extract_value_from_extras': odsh_helpers.odsh_extract_value_from_extras + 'odsh_extract_value_from_extras': odsh_helpers.odsh_extract_value_from_extras, + 'odsh_create_checksum': odsh_helpers.odsh_create_checksum } def before_map(self, map): diff --git a/ckanext/odsh/templates/macros/form.html b/ckanext/odsh/templates/macros/form.html index 8fc3221b..7e2305ed 100644 --- a/ckanext/odsh/templates/macros/form.html +++ b/ckanext/odsh/templates/macros/form.html @@ -53,6 +53,11 @@ is_required=false) %} {% endcall %} {% endmacro %} +{% macro input_extra(field, value='', index='') %} +<input id="field-{{field}}-key" type="hidden" name="extras__{{index}}__key" value="{{field}}" /> +<input id="field-{{field}}-value" type="text" name="extras__{{index}}__value" value="{{value}}" /> +{% endmacro %} + {# Builds a single checkbox input. @@ -497,4 +502,4 @@ options - A list/tuple of fields to be used as <options>. {% if is_upload_enabled %}</div>{% endif %} - {% endmacro %} \ No newline at end of file + {% endmacro %} diff --git a/ckanext/odsh/templates/package/snippets/package_basic_fields.html b/ckanext/odsh/templates/package/snippets/package_basic_fields.html index d4903e9d..36fb61c1 100644 --- a/ckanext/odsh/templates/package/snippets/package_basic_fields.html +++ b/ckanext/odsh/templates/package/snippets/package_basic_fields.html @@ -96,27 +96,23 @@ is_required=true,placeholder=_('Enter title')) }} <!-- field issued --> - {% set field='issued' %} - {% set value = h.odsh_extract_value_from_extras(data.extras,field)%} - {% set value_extras = value if value else h.odsh_now() %} - {% set index = 4 %} - + {% set field = 'issued' %} + {% set value = h.odsh_extract_value_from_extras(data.extras,field) %} <div class="control-group control-full"> - <label class="control-label" for="field-{{field}}">Veröffentlichungsdatum: <span title="Dieses Feld ist erforderlich" class="control-required">*</span> </label> - <div class="controls"> + <label class="control-label" for="field-{{field}}">Veröffentlichungsdatum: <span title="Dieses Feld ist erforderlich" class="control-required">*</span> </label> + <div class="controls"> <div class="row-fluid"> <div class="span6"> - <input id="field-{{field}}-key" type="hidden" name="extras__{{index}}__key" value="{{field}}" /> - <input id="field-{{field}}-value" type="text" name="extras__{{index}}__value" value="{{value_extras}}" /> + {{ form.input_extra(field, value=value if value else h.odsh_now(), index=h.odsh_create_checksum(field)) }} </div> <div class="span6 inline-error"> - {% if h.odsh_extract_error(field, errors) %} - {{_(h.odsh_extract_error(field, errors))}} - {% endif %} + {% if h.odsh_extract_error(field, errors) %} + {{_(h.odsh_extract_error(field, errors))}} + {% endif %} </div> </div> - </div> </div> + </div> <!-- field tags --> @@ -217,4 +213,4 @@ is_required=true,placeholder=_('Enter title')) }} </div> </div> </div> - {% endif %} \ No newline at end of file + {% endif %} -- GitLab