From a05b9a2bafc96642eb7bf4048c4256d76e0e6245 Mon Sep 17 00:00:00 2001 From: anonymous <anonymous> Date: Wed, 20 Feb 2019 11:57:31 +0100 Subject: [PATCH] uses sorting in licenses.json instead of alphabetical order --- ckanext/odsh/helpers.py | 14 ++++++++++++++ ckanext/odsh/plugin.py | 3 ++- .../package/snippets/package_basic_fields.html | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py index 5f16c4a0..3cef4f2c 100644 --- a/ckanext/odsh/helpers.py +++ b/ckanext/odsh/helpers.py @@ -168,3 +168,17 @@ def odsh_extract_value_from_extras(extras, key): if 'value' in item: return item['value'] return None + +def license_options(existing_license_id=None): + '''Returns [(l.title, l.id), ...] for the licenses configured to be + offered. Always includes the existing_license_id, if supplied. + ''' + register = model.Package.get_license_register() + licenses = register.values() + license_ids = [license.id for license in licenses] + if existing_license_id and existing_license_id not in license_ids: + license_ids.insert(0, existing_license_id) + return [ + (license_id, + register[license_id].title if license_id in register else license_id) + for license_id in license_ids] diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py index 1d5701d4..95933161 100644 --- a/ckanext/odsh/plugin.py +++ b/ckanext/odsh/plugin.py @@ -239,7 +239,8 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm '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_create_checksum': odsh_helpers.odsh_create_checksum + 'odsh_create_checksum': odsh_helpers.odsh_create_checksum, + 'presorted_license_options': odsh_helpers.presorted_license_options } def before_map(self, map): diff --git a/ckanext/odsh/templates/package/snippets/package_basic_fields.html b/ckanext/odsh/templates/package/snippets/package_basic_fields.html index 8b67ef94..d803642d 100644 --- a/ckanext/odsh/templates/package/snippets/package_basic_fields.html +++ b/ckanext/odsh/templates/package/snippets/package_basic_fields.html @@ -43,7 +43,7 @@ is_required=true,placeholder=_('Enter title')) }} <div class="controls"> <select id="field-license" name="license_id"> {% set existing_license_id = data.get('license_id') %} - {% for license_id, license_desc in h.license_options(existing_license_id) %} + {% for license_id, license_desc in h.presorted_license_options(existing_license_id) %} <option value="{{ license_id }}" {% if existing_license_id==license_id %}selected="selected" {% endif %}>{{ license_desc }}</option> {% endfor %} -- GitLab