Skip to content

Commit

Permalink
Merge branch '2472-offer-existing-license'
Browse files Browse the repository at this point in the history
Conflicts:
	ckan/tests/lib/test_helpers.py
  • Loading branch information
joetsoi committed Jul 1, 2015
2 parents c8fcc9d + 4332c72 commit 97b91f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
17 changes: 17 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -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
Expand Down Expand Up @@ -2150,4 +2166,5 @@ def get_organization(org=None, include_datasets=False):
'urlencode',
'check_config_permission',
'view_resource_url',
'license_options',
]
5 changes: 3 additions & 2 deletions ckan/templates/package/snippets/package_basic_fields.html
Expand Up @@ -30,8 +30,9 @@
<label class="control-label" for="field-license">{{ _("License") }}</label>
<div class="controls">
<select id="field-license" name="license_id" data-module="autocomplete">
{% for license_desc, license_id in licenses|sort if license_desc %}
<option value="{{ license_id }}" {% if data.get('license_id', 'notspecified') == license_id %}selected="selected"{% endif %}>{{ license_desc }}</option>
{% set existing_license_id = data.get('license_id') %}
{% for license_id, license_desc in h.license_options(existing_license_id) %}
<option value="{{ license_id }}" {% if existing_license_id == license_id %}selected="selected"{% endif %}>{{ license_desc }}</option>
{% endfor %}
</select>
{% if error %}<span class="error-block">{{ error }}</span>{% endif %}
Expand Down
8 changes: 8 additions & 0 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -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')

0 comments on commit 97b91f7

Please sign in to comment.