From 55c8ff6f108a1b9706b01ea68d875042639bfa9e Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Mon, 13 Feb 2012 11:02:09 +0000 Subject: [PATCH] [#1359,licenses][s]: refactor to remove dependency on external licenses package. * Inline list of default licenses (those from http://licenses.opendefinition.org/licenses/groups/ckan.json) * Minor refactoring on initialization of LicenseRegister to support dicts or lists or licenses * Fix up tests (now that we do not have gpl in our default lists of licenses) --- ckan/lib/create_test_data.py | 2 +- ckan/model/license.py | 237 +++++++++++++++++++++++++- ckan/tests/functional/test_package.py | 4 +- ckan/tests/models/test_license.py | 1 - ckan/tests/models/test_package.py | 6 +- requires/lucid_missing.txt | 1 - 6 files changed, 237 insertions(+), 14 deletions(-) diff --git a/ckan/lib/create_test_data.py b/ckan/lib/create_test_data.py index 1a45b9ad86c..6554cf8371a 100644 --- a/ckan/lib/create_test_data.py +++ b/ckan/lib/create_test_data.py @@ -546,7 +546,7 @@ def get_all_data(cls): 'format':'DOC', 'description':'http://www.statistics.gov.uk/hub/id/119-34565'}], 'groups':'ukgov test1 test2 penguin', - 'license':'gpl-3.0', + 'license':'odc-by', 'notes':u'''From > The Government Information Locator Service (GILS) is an effort to identify, locate, and describe publicly available Federal diff --git a/ckan/model/license.py b/ckan/model/license.py index 7b73e6f9ecb..b9aa6872b20 100644 --- a/ckan/model/license.py +++ b/ckan/model/license.py @@ -1,5 +1,7 @@ from pylons import config import datetime +import urllib2 +from ckan.lib.helpers import json import re class License(object): @@ -41,13 +43,32 @@ class LicenseRegister(object): def __init__(self): group_url = config.get('licenses_group_url', None) if group_url: - from licenses.service import LicensesService2 - self.service = LicensesService2(group_url) - entity_list = self.service.get_licenses() + self.load_licenses(group_url) else: - from licenses import Licenses - entity_list = Licenses().get_group_licenses('ckan_original') - self.licenses = [License(entity) for entity in entity_list] + self._create_license_list(self.default_license_list) + + def load_licenses(self, license_url): + try: + response = urllib2.urlopen(license_url) + response_body = response.read() + except Exception, inst: + msg = "Couldn't connect to licenses service %r: %s" % (license_url, inst) + raise Exception, msg + try: + license_data = json.loads(response_body) + except Exception, inst: + msg = "Couldn't read response from licenses service %r: %s" % (response_body, inst) + raise Exception, inst + self._create_license_list(license_data) + + def _create_license_list(self, license_data): + if isinstance(license_data, dict): + self.licenses = [License(entity) for entity in license_data.values()] + elif isinstance(license_data, list): + self.licenses = [License(entity) for entity in license_data] + else: + msg = "Licenses at %s must be dictionary or list" % license_url + raise ValueError(msg) def __getitem__(self, key, default=Exception): for license in self.licenses: @@ -85,3 +106,207 @@ def get_by_title(self, title, default=None): else: return default + + default_license_list = [ + { + "domain_content": False, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "notspecified", + "is_generic": True, + "is_okd_compliant": False, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "License Not Specified", + "url": "" + }, + { + "domain_content": False, + "domain_data": True, + "domain_software": False, + "family": "", + "id": "odc-pddl", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Open Data Commons Public Domain Dedication and Licence (PDDL)", + "url": "http://www.opendefinition.org/licenses/odc-pddl" + }, + { + "domain_content": False, + "domain_data": True, + "domain_software": False, + "family": "", + "id": "odc-odbl", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Open Data Commons Open Database License (ODbL)", + "url": "http://www.opendefinition.org/licenses/odc-odbl" + }, + { + "domain_content": False, + "domain_data": True, + "domain_software": False, + "family": "", + "id": "odc-by", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Open Data Commons Attribution License", + "url": "http://www.opendefinition.org/licenses/odc-by" + }, + { + "domain_content": True, + "domain_data": True, + "domain_software": False, + "family": "", + "id": "cc-zero", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Creative Commons CCZero", + "url": "http://www.opendefinition.org/licenses/cc-zero" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "cc-by", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Creative Commons Attribution", + "url": "http://www.opendefinition.org/licenses/cc-by" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "cc-by-sa", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Creative Commons Attribution Share-Alike", + "url": "http://www.opendefinition.org/licenses/cc-by-sa" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "gfdl", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "GNU Free Documentation License", + "url": "http://www.opendefinition.org/licenses/gfdl" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "other-open", + "is_generic": True, + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Other (Open)", + "url": "" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "other-pd", + "is_generic": True, + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Other (Public Domain)", + "url": "" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "other-at", + "is_generic": True, + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Other (Attribution)", + "url": "" + }, + { + "domain_content": True, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "uk-ogl", + "is_okd_compliant": True, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "UK Open Government Licence (OGL)", + "url": "http://reference.data.gov.uk/id/open-government-licence" + }, + { + "domain_content": False, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "cc-nc", + "is_okd_compliant": False, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Creative Commons Non-Commercial (Any)", + "url": "http://creativecommons.org/licenses/by-nc/2.0/" + }, + { + "domain_content": False, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "other-nc", + "is_generic": True, + "is_okd_compliant": False, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Other (Non-Commercial)", + "url": "" + }, + { + "domain_content": False, + "domain_data": False, + "domain_software": False, + "family": "", + "id": "other-closed", + "is_generic": True, + "is_okd_compliant": False, + "is_osi_compliant": False, + "maintainer": "", + "status": "active", + "title": "Other (Not Open)", + "url": "" + } + ] diff --git a/ckan/tests/functional/test_package.py b/ckan/tests/functional/test_package.py index e789b594a5c..d6aceda2c08 100644 --- a/ckan/tests/functional/test_package.py +++ b/ckan/tests/functional/test_package.py @@ -788,7 +788,7 @@ def test_edit_all_fields(self): ) assert len(resources[0]) == 5 notes = u'Very important' - license_id = u'gpl-3.0' + license_id = u'odc-by' state = model.State.ACTIVE tags = (u'tag1', u'tag2', u'tag 3') tags_txt = u','.join(tags) @@ -1152,7 +1152,7 @@ def test_new_all_fields(self): url = u'http://something.com/somewhere.zip' download_url = u'http://something.com/somewhere-else.zip' notes = u'Very important' - license_id = u'gpl-3.0' + license_id = u'odc-by' tags = (u'tag1', u'tag2.', u'tag 3', u'SomeCaps') tags_txt = u','.join(tags) extras = {self.key1:self.value1, 'key2':'value2', 'key3':'value3'} diff --git a/ckan/tests/models/test_license.py b/ckan/tests/models/test_license.py index 142a4939f77..281b6d966d3 100644 --- a/ckan/tests/models/test_license.py +++ b/ckan/tests/models/test_license.py @@ -35,6 +35,5 @@ def test_getitem(self): license = self.licenses[license_id] self.assert_unicode(license.id) self.assert_unicode(license.title) - self.assert_datetime(license.date_created) self.assert_unicode(license.url) diff --git a/ckan/tests/models/test_package.py b/ckan/tests/models/test_package.py index 8371f226f7d..653ea56333b 100644 --- a/ckan/tests/models/test_package.py +++ b/ckan/tests/models/test_package.py @@ -19,7 +19,7 @@ def setup_class(self): self.pkg1 = model.Package(name=self.name) model.Session.add(self.pkg1) self.pkg1.notes = self.notes - self.pkg1.license_id = u'gpl-3.0' + self.pkg1.license_id = u'odc-by' model.Session.commit() model.Session.remove() @@ -71,8 +71,8 @@ def test_create_package(self): package = model.Package.by_name(self.name) assert package.name == self.name assert package.notes == self.notes - assert package.license.id == u'gpl-3.0' - assert package.license.title == u'OSI Approved::GNU General Public License version 3.0 (GPLv3)', package.license.title + assert package.license.id == u'odc-by' + assert package.license.title == u'Open Data Commons Attribution License', package.license.title def test_update_package(self): newnotes = u'Written by Beethoven' diff --git a/requires/lucid_missing.txt b/requires/lucid_missing.txt index a5ed111fdf0..cb49ca034ee 100644 --- a/requires/lucid_missing.txt +++ b/requires/lucid_missing.txt @@ -18,6 +18,5 @@ pairtree==0.7.1-T ofs==0.4.1 apachemiddleware==0.1.1 -licenses==0.6.1 # markupsafe is required by webhelpers==1.2 required by formalchemy with SQLAlchemy 0.6 markupsafe==0.9.2