From 4332c72d57e259335064e87e22f1319487596a42 Mon Sep 17 00:00:00 2001 From: David Read Date: Mon, 22 Jun 2015 17:53:44 +0100 Subject: [PATCH] [#2472] Offer existing license in form if it is missing from current license. (Extracted from 2478-licenses-specific) --- ckan/lib/helpers.py | 17 +++++++++++++++++ .../package/snippets/package_basic_fields.html | 5 +++-- ckan/tests/lib/test_helpers.py | 8 ++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 770210179f0..cf24e4212eb 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -2032,6 +2032,22 @@ def get_organization(org=None, include_datasets=False): except (NotFound, ValidationError, NotAuthorized): return {} + +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() + sorted_licenses = sorted(register.values(), key=lambda x: x.title) + license_ids = [license.id for license in sorted_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] + + # these are the functions that will end up in `h` template helpers __allowed_functions__ = [ # functions defined in ckan.lib.helpers @@ -2150,4 +2166,5 @@ def get_organization(org=None, include_datasets=False): 'urlencode', 'check_config_permission', 'view_resource_url', + 'license_options', ] diff --git a/ckan/templates/package/snippets/package_basic_fields.html b/ckan/templates/package/snippets/package_basic_fields.html index 23ede701fa4..8d948e09c59 100644 --- a/ckan/templates/package/snippets/package_basic_fields.html +++ b/ckan/templates/package/snippets/package_basic_fields.html @@ -30,8 +30,9 @@
{% if error %}{{ error }}{% endif %} diff --git a/ckan/tests/lib/test_helpers.py b/ckan/tests/lib/test_helpers.py index 8e016292f68..d7777940158 100644 --- a/ckan/tests/lib/test_helpers.py +++ b/ckan/tests/lib/test_helpers.py @@ -101,3 +101,11 @@ class StringLike(str): strType = ''.__class__ assert result.__class__ == strType,\ '"remove_linebreaks" casts into str()' + + +class TestLicenseOptions(object): + def test_includes_existing_license(self): + licenses = h.license_options('some-old-license') + eq_(dict(licenses)['some-old-license'], 'some-old-license') + # and it is first on the list + eq_(licenses[0][0], 'some-old-license')