diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index f604da3ab7d..b0db46aaf31 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 feded9d1a59..a1175dddc20 100644 --- a/ckan/tests/lib/test_helpers.py +++ b/ckan/tests/lib/test_helpers.py @@ -101,3 +101,11 @@ class UnicodeLike(unicode): strType = u''.__class__ assert result.__class__ == strType,\ '"remove_linebreaks" casts into unicode()' + + +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')