Skip to content

Commit

Permalink
Merge branch '2110-license-keys'
Browse files Browse the repository at this point in the history
  • Loading branch information
joetsoi committed May 14, 2015
2 parents 269ddc0 + 45e8862 commit 20f4332
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 65 deletions.
95 changes: 69 additions & 26 deletions ckan/model/license.py
Expand Up @@ -3,14 +3,28 @@
import re

from pylons import config
from paste.deploy.converters import asbool

from ckan.common import _, json
import ckan.lib.maintain as maintain

log = __import__('logging').getLogger(__name__)


class License(object):
"""Domain object for a license."""

def __init__(self, data):
# convert old keys if necessary
if 'is_okd_compliant' in data:
data['od_conformance'] = 'approved' \
if asbool(data['is_okd_compliant']) else ''
del data['is_okd_compliant']
if 'is_osi_compliant' in data:
data['osd_conformance'] = 'approved' \
if asbool(data['is_osi_compliant']) else ''
del data['is_osi_compliant']

self._data = data
for (key, value) in self._data.items():
if key == 'date_created':
Expand All @@ -23,20 +37,50 @@ def __init__(self, data):
self._data[key] = value

def __getattr__(self, name):
if name == 'is_okd_compliant':
log.warn('license.is_okd_compliant is deprecated - use '
'od_conformance instead.')
return self._data['od_conformance'] == 'approved'
if name == 'is_osi_compliant':
log.warn('license.is_osi_compliant is deprecated - use '
'osd_conformance instead.')
return self._data['osd_conformance'] == 'approved'
return self._data[name]

@maintain.deprecated("License.__getitem__() is deprecated and will be "
"removed in a future version of CKAN. Instead, "
"please use attribute access.")
def __getitem__(self, key):
return self._data[key]
'''NB This method is deprecated and will be removed in a future version
of CKAN. Instead, please use attribute access.
'''
return self.__getattr__(key)

def isopen(self):
return self.is_okd_compliant or self.is_osi_compliant

if not hasattr(self, '_isopen'):
self._isopen = self.od_conformance == 'approved' or \
self.osd_conformance == 'approved'
return self._isopen

@maintain.deprecated("License.as_dict() is deprecated and will be "
"removed in a future version of CKAN. Instead, "
"please use attribute access.")
def as_dict(self):
'''NB This method is deprecated and will be removed in a future version
of CKAN. Instead, please use attribute access.
'''
data = self._data.copy()
if 'date_created' in data:
value = data['date_created']
value = value.isoformat()
data['date_created'] = value

# deprecated keys
if 'od_conformance' in data:
data['is_okd_compliant'] = data['od_conformance'] == 'approved'
if 'osd_conformance' in data:
data['is_osi_compliant'] = data['osd_conformance'] == 'approved'

return data


Expand Down Expand Up @@ -73,12 +117,12 @@ def load_licenses(self, license_url):
response_body = response.read()
except Exception, inst:
msg = "Couldn't connect to licenses service %r: %s" % (license_url, inst)
raise Exception, msg
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
raise Exception(inst)
self._create_license_list(license_data, license_url)

def _create_license_list(self, license_data, license_url=''):
Expand All @@ -97,7 +141,7 @@ def __getitem__(self, key, default=Exception):
if default != Exception:
return default
else:
raise KeyError, "License not found: %s" % key
raise KeyError("License not found: %s" % key)

def get(self, key, default=None):
return self.__getitem__(key, default=default)
Expand All @@ -118,7 +162,6 @@ def __len__(self):
return len(self.licenses)



class DefaultLicense(dict):
''' The license was a dict but this did not allow translation of the
title. This is a slightly changed dict that allows us to have the title
Expand All @@ -127,13 +170,13 @@ class DefaultLicense(dict):
domain_content = False
domain_data = False
domain_software = False
family = ""
family = ''
is_generic = False
is_okd_compliant = False
is_osi_compliant = False
maintainer = ""
status = "active"
url = ""
od_conformance = 'not reviewed'
osd_conformance = 'not reviewed'
maintainer = ''
status = 'active'
url = ''
title = ''
id = ''

Expand All @@ -143,8 +186,8 @@ class DefaultLicense(dict):
'domain_software',
'family',
'is_generic',
'is_okd_compliant',
'is_osi_compliant',
'od_conformance',
'osd_conformance',
'maintainer',
'status',
'url',
Expand Down Expand Up @@ -179,7 +222,7 @@ def title(self):
class LicenseOpenDataCommonsPDDL(DefaultLicense):
domain_data = True
id = "odc-pddl"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/odc-pddl"

@property
Expand All @@ -189,7 +232,7 @@ def title(self):
class LicenseOpenDataCommonsOpenDatabase(DefaultLicense):
domain_data = True
id = "odc-odbl"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/odc-odbl"

@property
Expand All @@ -199,7 +242,7 @@ def title(self):
class LicenseOpenDataAttribution(DefaultLicense):
domain_data = True
id = "odc-by"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/odc-by"

@property
Expand All @@ -210,7 +253,7 @@ class LicenseCreativeCommonsZero(DefaultLicense):
domain_content = True
domain_data = True
id = "cc-zero"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/cc-zero"

@property
Expand All @@ -219,7 +262,7 @@ def title(self):

class LicenseCreativeCommonsAttribution(DefaultLicense):
id = "cc-by"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/cc-by"

@property
Expand All @@ -229,7 +272,7 @@ def title(self):
class LicenseCreativeCommonsAttributionShareAlike(DefaultLicense):
domain_content = True
id = "cc-by-sa"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/cc-by-sa"

@property
Expand All @@ -239,7 +282,7 @@ def title(self):
class LicenseGNUFreeDocument(DefaultLicense):
domain_content = True
id = "gfdl"
is_okd_compliant = True
od_conformance = 'approved'
url = "http://www.opendefinition.org/licenses/gfdl"
@property
def title(self):
Expand All @@ -249,7 +292,7 @@ class LicenseOtherOpen(DefaultLicense):
domain_content = True
id = "other-open"
is_generic = True
is_okd_compliant = True
od_conformance = 'approved'

@property
def title(self):
Expand All @@ -259,7 +302,7 @@ class LicenseOtherPublicDomain(DefaultLicense):
domain_content = True
id = "other-pd"
is_generic = True
is_okd_compliant = True
od_conformance = 'approved'

@property
def title(self):
Expand All @@ -269,7 +312,7 @@ class LicenseOtherAttribution(DefaultLicense):
domain_content = True
id = "other-at"
is_generic = True
is_okd_compliant = True
od_conformance = 'approved'

@property
def title(self):
Expand All @@ -278,7 +321,7 @@ def title(self):
class LicenseOpenGovernment(DefaultLicense):
domain_content = True
id = "uk-ogl"
is_okd_compliant = True
od_conformance = 'approved'
# CS: bad_spelling ignore
url = "http://reference.data.gov.uk/id/open-government-licence"

Expand Down
39 changes: 0 additions & 39 deletions ckan/tests/legacy/models/test_license.py

This file was deleted.

17 changes: 17 additions & 0 deletions ckan/tests/model/licenses.v1
@@ -0,0 +1,17 @@
[{

"status": "active",
"maintainer": "",
"family": "",
"title": "Creative Commons Attribution",
"domain_data": "False",
"is_okd_compliant": "True",
"is_generic": "False",
"url": "http://www.opendefinition.org/licenses/cc-by",
"is_osi_compliant": "False",
"domain_content": "False",
"domain_software": "False",
"id": "cc-by"

}
]
17 changes: 17 additions & 0 deletions ckan/tests/model/licenses.v2
@@ -0,0 +1,17 @@
{
"CC-BY-4.0": {

"domain_content": true,
"domain_data": true,
"domain_software": false,
"family": "",
"id": "CC-BY-4.0",
"maintainer": "Creative Commons",
"od_conformance": "approved",
"osd_conformance": "not reviewed",
"status": "active",
"title": "Creative Commons Attribution 4.0",
"url": "https://creativecommons.org/licenses/by/4.0/"

}
}

0 comments on commit 20f4332

Please sign in to comment.