regluit/core/cc.py

165 lines
10 KiB
Python
Raw Normal View History

# coding=utf-8
2014-05-08 14:21:50 +00:00
# mostly constants related to Creative Commons
# let's be DRY with these parameters
## need to add versioned CC entries
2014-05-08 14:21:50 +00:00
INFO_CC = (
2017-07-27 14:33:13 +00:00
('CC BY-NC-ND', 'by-nc-nd', 'Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported (CC BY-NC-ND 3.0)', 'https://creativecommons.org/licenses/by-nc-nd/3.0/', 'Creative Commons Attribution-NonCommercial-NoDerivs'),
('CC BY-NC-SA', 'by-nc-sa', 'Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)', 'https://creativecommons.org/licenses/by-nc-sa/3.0/', 'Creative Commons Attribution-NonCommercial-ShareAlike'),
('CC BY-NC', 'by-nc', 'Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0)', 'https://creativecommons.org/licenses/by-nc/3.0/', 'Creative Commons Attribution-NonCommercial'),
('CC BY-ND', 'by-nd', 'Creative Commons Attribution-NoDerivs 3.0 Unported (CC BY-ND 3.0)', 'https://creativecommons.org/licenses/by-nd/3.0/','Creative Commons Attribution-NoDerivs'),
('CC BY-SA', 'by-sa', 'Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)', 'https://creativecommons.org/licenses/by-sa/3.0/', 'Creative Commons Attribution-ShareAlike'),
('CC BY', 'by', 'Creative Commons Attribution 3.0 Unported (CC BY 3.0)', 'https://creativecommons.org/licenses/by/3.0/', 'Creative Commons Attribution'),
('CC0', 'cc0', 'No Rights Reserved (CC0)', 'https://creativecommons.org/about/cc0', 'No Rights Reserved (CC0)'),
2014-05-08 14:21:50 +00:00
)
INFO_FREE = INFO_CC + (
('GFDL', 'gdfl', 'GNU Free Documentation License', 'http://www.gnu.org/licenses/fdl-1.3-standalone.html', 'GNU Free Documentation License'),
('LAL', 'lal', 'Licence Art Libre', 'http://artlibre.org/licence/lal/', 'Licence Art Libre'),
('OSI', 'opensource', 'OSI Approved License', 'https://opensource.org/licenses', 'OSI Approved License'),
)
2014-05-08 14:21:50 +00:00
INFO_PD = (
2017-07-27 14:33:13 +00:00
('PD-US', 'pd-us', 'Public Domain, US', 'https://creativecommons.org/about/pdm', 'Public Domain, US'),
2014-05-08 14:21:50 +00:00
)
INFO_ALL = INFO_FREE + INFO_PD
2014-05-08 14:21:50 +00:00
# CCHOICES, CCGRANTS, and FORMATS are all used in places that expect tuples
# CONTENT_TYPES will be easiest to manipulate in ungluify_record as a dict
CCCHOICES = tuple([(item[0],item[2]) for item in INFO_CC])
FREECHOICES = tuple([(item[0],item[2]) for item in INFO_FREE])
2014-05-08 14:21:50 +00:00
2014-12-03 18:25:01 +00:00
CHOICES = tuple([(item[0],item[4]) for item in INFO_ALL])
2014-05-08 14:21:50 +00:00
CCGRANTS = tuple([(item[0],item[3]) for item in INFO_CC])
GRANTS = tuple([(item[0],item[3]) for item in INFO_ALL])
LICENSE_LIST = [item[0] for item in INFO_CC]
LICENSE_LIST_ALL = [item[0] for item in INFO_ALL]
LICENSE_NAMES_ALL = [item[2] for item in INFO_ALL]
LICENSE_URLS_ALL = [item[3] for item in INFO_ALL]
FACET_LIST = [item[1] for item in INFO_ALL]
2014-05-08 14:21:50 +00:00
RIGHTS_ALIAS = {
"Public domain in the USA.":"PD-US",
}
2014-05-08 14:21:50 +00:00
class CCLicense():
@staticmethod
def url(license):
license = RIGHTS_ALIAS.get(license, license)
2014-05-08 14:21:50 +00:00
if license in LICENSE_LIST_ALL:
2014-05-08 14:49:59 +00:00
return INFO_ALL[LICENSE_LIST_ALL.index(license)][3]
2014-05-08 14:21:50 +00:00
else:
return ''
@staticmethod
def badge(license):
if license == 'PD-US':
2014-11-10 18:14:59 +00:00
return '/static/images/pdmark.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC0':
2014-11-10 18:14:59 +00:00
return '/static/images/cc0.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY':
2014-11-10 18:14:59 +00:00
return '/static/images/ccby.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY-NC-ND':
2014-11-10 18:14:59 +00:00
return '/static/images/ccbyncnd.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY-NC-SA':
2014-11-10 18:14:59 +00:00
return '/static/images/ccbyncsa.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY-NC':
2014-11-10 18:14:59 +00:00
return '/static/images/ccbync.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY-SA':
2014-11-10 18:14:59 +00:00
return '/static/images/ccbysa.png'
2014-05-08 14:21:50 +00:00
elif license == 'CC BY-ND':
2014-11-10 18:14:59 +00:00
return '/static/images/ccbynd.png'
elif license == 'GFDL':
2014-12-01 21:14:38 +00:00
return '/static/images/gfdl.png'
elif license == 'LAL':
return '/static/images/lal.png'
elif license == 'OSI':
return '/static/images/opensource.png'
2014-05-08 14:21:50 +00:00
else:
return ''
def description(license):
if license == 'PD-US':
return 'Use of this material is not restricted by copyright in the US.'
elif license == 'CC0':
return 'The copyright owner has dedicated the material to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.'
elif license == 'CC BY':
return 'You are free to: copy and redistribute the material in any medium or format; remix, transform, and build upon the material; for any purpose, even commercially. Under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.'
elif license == 'CC BY-NC-ND':
return 'You are free to: copy and redistribute the material in any medium or format; under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.; you may not use the material for commercial purposes; if you remix, transform, or build upon the material, you may not distribute the modified material.'
elif license == 'CC BY-NC-SA':
return 'You are free to: copy and redistribute the material in any medium or format; remix, transform, and build upon the material; Under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. You may not use the material for commercial purposes. If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.'
elif license == 'CC BY-NC':
return 'You are free to: copy and redistribute the material in any medium or format; remix, transform, and build upon the material; under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. You may not use the material for commercial purposes.'
elif license == 'CC BY-SA':
return 'You are free to: copy and redistribute the material in any medium or format; remix, transform, and build upon the material; for any purpose, even commercially. Under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.'
elif license == 'CC BY-ND':
return 'You are free to: copy and redistribute the material in any medium or format; for any purpose, even commercially. Under the following terms: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. If you remix, transform, or build upon the material, you may not distribute the modified material.'
elif license == 'GFDL':
return 'The purpose of this License is to make a manual, textbook, or other functional and useful document "free" in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others.'
elif license == 'LAL':
return 'Avec la Licence Art Libre, l\'autorisation est donnée de copier, de diffuser et de transformer librement les œuvres dans le respect des droits de l\'auteur.'
elif license == 'OSI':
return 'Open source licenses are licenses that comply with the Open Source Definition — in brief, they allow software to be freely used, modified, and shared. To be approved by the Open Source Initiative (also known as the OSI), a license must go through the Open Source Initiative\'s license review process.'
else:
return ''
fix error: ====================================================================== ERROR: test_nix (regluit.api.tests.FeedTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/api/tests.py", line 173, in test_nix r = self.client.get('/api/onix/by/') File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/test/client.py", line 500, in get **extra) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/test/client.py", line 303, in get return self.generic('GET', path, secure=secure, **r) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/test/client.py", line 379, in generic return self.request(**r) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/test/client.py", line 466, in request six.reraise(*exc_info) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/views/generic/base.py", line 89, in dispatch return handler(request, *args, **kwargs) File "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/api/views.py", line 208, in get return HttpResponse(onix.onix_feed(facet_class, max), File "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/api/onix.py", line 25, in onix_feed editions = facet.facet_object.filter_model("Edition",editions).distinct() File "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/core/facets.py", line 44, in filter_model return model_filter( self._filter_model(model, query_set)) File "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/core/facets.py", line 147, in edition_license_filter return query_set.filter(ebooks__rights=cc.ccinfo(facet_name)) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/db/models/query.py", line 679, in filter return self._filter_or_exclude(False, *args, **kwargs) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1310, in add_q clause, require_inner = self._add_q(where_part, self.used_aliases) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1338, in _add_q allow_joins=allow_joins, split_subq=split_subq, File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1177, in build_filter if isinstance(value, Iterator): File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/abc.py", line 144, in __instancecheck__ return cls.__subclasscheck__(subtype) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/abc.py", line 180, in __subclasscheck__ if issubclass(subclass, scls): File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/abc.py", line 161, in __subclasscheck__ ok = cls.__subclasshook__(subclass) File "/Users/raymondyee/anaconda/envs/regluit613/lib/python2.7/site-packages/backports_abc.py", line 66, in __subclasshook__ mro = C.__mro__ AttributeError: class ccinfo has no attribute '__mro__'
2016-09-12 17:42:12 +00:00
class ccinfo(object):
def __init__(self, license):
2014-12-03 18:17:43 +00:00
value=license_value(license)
2014-12-01 21:07:41 +00:00
self.license=value if value else license
@property
def description(self):
return description(self.license)
@property
def badge(self):
return CCLicense.badge(self.license)
@property
def url(self):
return CCLicense.url(self.license)
@property
def full_title(self):
if self.license in LICENSE_LIST_ALL:
2014-12-01 21:07:41 +00:00
return INFO_ALL[LICENSE_LIST_ALL.index(self.license)][2]
else:
2014-12-01 21:07:41 +00:00
return self.license
@property
def title(self):
if self.license in LICENSE_LIST_ALL:
return INFO_ALL[LICENSE_LIST_ALL.index(self.license)][4]
else:
return self.license
@property
def is_cc(self):
return self.license in LICENSE_LIST
@property
def is_pd(self):
return self.license == 'PD-US'
2014-12-01 21:07:41 +00:00
def __str__(self):
return self.license
2014-11-18 18:33:17 +00:00
2014-12-03 18:17:43 +00:00
def license_value(facet):
2014-11-18 18:33:17 +00:00
if facet in FACET_LIST:
return LICENSE_LIST_ALL[FACET_LIST.index(facet)]
else:
return ''
def match_license(license_string):
if license_string in LICENSE_LIST_ALL:
return license_string
try:
l = LICENSE_NAMES_ALL.index(license_string)
return INFO_ALL[l][0]
except ValueError:
pass
try:
l = LICENSE_URLS_ALL.index(license_string)
return INFO_ALL[l][0]
except ValueError:
pass
return RIGHTS_ALIAS.get(license_string, None)