Merge remote-tracking branch 'gsoc2016/Subtitle-1'
[mediagoblin.git] / mediagoblin / tools / licenses.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from collections import namedtuple
18 # Give a License attribute names: uri, name, abbreviation
19 License = namedtuple("License", ["abbreviation", "name", "uri"])
20
21 SORTED_LICENSES = [
22 License("All rights reserved", "No license specified", ""),
23 License("CC BY 4.0", "Creative Commons Attribution 4.0 International",
24 "https://creativecommons.org/licenses/by/4.0/"),
25 License("CC BY-SA 4.0",
26 "Creative Commons Attribution-ShareAlike 4.0 International",
27 "https://creativecommons.org/licenses/by-sa/4.0/"),
28 License("CC BY-ND 4.0",
29 "Creative Commons Attribution-NoDerivs 4.0 International",
30 "https://creativecommons.org/licenses/by-nd/4.0/"),
31 License("CC BY-NC 4.0",
32 "Creative Commons Attribution-NonCommercial 4.0 International",
33 "https://creativecommons.org/licenses/by-nc/4.0/"),
34 License("CC BY-NC-SA 4.0",
35 "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International",
36 "https://creativecommons.org/licenses/by-nc-sa/4.0/"),
37 License("CC BY-NC-ND 4.0",
38 "Creative Commons Attribution-NonCommercial-NoDerivs 4.0 International",
39 "https://creativecommons.org/licenses/by-nc-nd/4.0/"),
40 License("CC BY 3.0", "Creative Commons Attribution Unported 3.0",
41 "https://creativecommons.org/licenses/by/3.0/"),
42 License("CC BY-SA 3.0",
43 "Creative Commons Attribution-ShareAlike 3.0 Unported",
44 "https://creativecommons.org/licenses/by-sa/3.0/"),
45 License("CC BY-ND 3.0",
46 "Creative Commons Attribution-NoDerivs 3.0 Unported",
47 "https://creativecommons.org/licenses/by-nd/3.0/"),
48 License("CC BY-NC 3.0",
49 "Creative Commons Attribution-NonCommercial 3.0 Unported",
50 "https://creativecommons.org/licenses/by-nc/3.0/"),
51 License("CC BY-NC-SA 3.0",
52 "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported",
53 "https://creativecommons.org/licenses/by-nc-sa/3.0/"),
54 License("CC BY-NC-ND 3.0",
55 "Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported",
56 "https://creativecommons.org/licenses/by-nc-nd/3.0/"),
57 License("CC0 1.0",
58 "Creative Commons CC0 1.0 Universal",
59 "https://creativecommons.org/publicdomain/zero/1.0/"),
60 License("Public Domain","Public Domain",
61 "https://creativecommons.org/publicdomain/mark/1.0/"),
62 ]
63
64 # dict {uri: License,...} to enable fast license lookup by uri. Ideally,
65 # we'd want to use an OrderedDict (python 2.7+) here to avoid having the
66 # same data in two structures
67 SUPPORTED_LICENSES = dict(((l.uri, l) for l in SORTED_LICENSES))
68
69
70 def get_license_by_url(url):
71 """Look up a license by its url and return the License object"""
72 try:
73 return SUPPORTED_LICENSES[url]
74 except KeyError:
75 # in case of an unknown License, just display the url given
76 # rather than exploding in the user's face.
77 return License(url, url, url)
78
79
80 def licenses_as_choices():
81 """List of (uri, abbreviation) tuples for HTML choice field population
82
83 The data seems to be consumed/deleted during usage, so hand over a
84 throwaway list, rather than just a generator.
85 """
86 return [(lic.uri, lic.abbreviation) for lic in SORTED_LICENSES]