Commits

Anonymous committed ec06b08

Initial import: license and organization models, admin support, easy-to-use field, example data, templatetags/filters

  • Participants
  • Parent commits f78a815

Comments (0)

Files changed (8)

File licenses/__init__.py

Empty file added.

File licenses/admin.py

+from django.contrib import admin
+from django.utils.translation import ugettext_lazy as _
+from licenses.models import Organization, License
+
+class OrganizationAdmin(admin.ModelAdmin):
+	prepopulated_fields = {'slug': ('name', )}
+	list_display = ('name', 'abbreviation', 'website')
+	search_fields = ('name', 'slug', 'abbreviation','website', 'description')
+
+class LicenseAdmin(admin.ModelAdmin):
+	prepopulated_fields = {'slug': ('name', )}
+	list_display = ('name', 'abbreviation', 'organization', 'is_active')
+	list_filter = ('is_active', )
+	search_fields = ('name', 'slug', 'abbreviation', 'url', 'description')
+	radio_fields = {'organization': admin.HORIZONTAL}
+	fieldsets = (
+		(None, {
+			'fields': ('name', 'slug', 'url', 'abbreviation', 'organization')
+		}),
+		(_('Advanced options'), {
+			'classes': ('collapse',),
+			'fields': ('logo', 'description', 'is_active')
+		}),
+	)
+
+admin.site.register(Organization, OrganizationAdmin)
+admin.site.register(License, LicenseAdmin)

File licenses/fields.py

+from django.db.models import *
+from django.utils.translation import ugettext_lazy as _
+from licenses.models import License
+
+class LicenseField(ForeignKey):
+	def __init__(self, **kwargs):
+		kwargs.setdefault('verbose_name', _('license'))
+		if kwargs.pop('required', None) == False:
+			kwargs['blank'] = True
+			kwargs['null'] = True
+		ForeignKey.__init__(self, License, **kwargs)

File licenses/fixtures/common_licenses.json

+[
+	{
+		"pk": 1,
+		"model": "licenses.license",
+		"fields": {
+			"name": "Public domain",
+			"slug": "public-domain ",
+			"organization": null,
+			"abbreviation": "PD",
+			"url": "http://en.wikipedia.org/wiki/Public_domain",
+			"logo": "",
+			"description": "Public domain is not realy a license. It is deactivated therefor.",
+			"is_active": false
+		}
+	},
+	{
+		"pk": 2,
+		"model": "licenses.license",
+		"fields": {
+			"name": "GNU General Public License version 3",
+			"slug": "gnu-general-public-license-v3",
+			"organization": 1,
+			"abbreviation": "GPLv3",
+			"url": "http://www.gnu.org/licenses/gpl.html",
+			"logo": "http://www.gnu.org/graphics/gplv3-88x31.png",
+			"description": "See also http://en.wikipedia.org/wiki/GNU_General_Public_License",
+			"is_active": true
+		}
+	},
+	{
+		"pk": 3,
+		"model": "licenses.license",
+		"fields": {
+			"name": "GNU Lesser General Public License version 3",
+			"slug": "gnu-lesser-general-public-license-v3",
+			"organization": 1,
+			"abbreviation": "LGPLv3",
+			"url": "http://www.gnu.org/licenses/lgpl.html",
+			"logo": "http://www.gnu.org/graphics/lgplv3-88x31.png",
+			"description": "",
+			"is_active": true
+		}
+	},
+	{
+		"pk": 4,
+		"model": "licenses.license",
+		"fields": {
+			"name": "GNU Affero General Public License version 3",
+			"slug": "gnu-affero-general-public-license-v3",
+			"organization": 1,
+			"abbreviation": "AGPL",
+			"url": "http://www.gnu.org/licenses/agpl.html",
+			"logo": "http://www.gnu.org/graphics/agplv3-88x31.png",
+			"description": "There is only one version of the AGPL, so the abbreviation doesn't include the version number.",
+			"is_active": true
+		}
+	},
+	{
+		"pk": 5,
+		"model": "licenses.license",
+		"fields": {
+			"name": "GNU Free Documentation License version 1.2",
+			"slug": "gnu-free-documentation-license",
+			"organization": 1,
+			"abbreviation": "FDLv1.2",
+			"url": "http://www.gnu.org/licenses/fdl.html",
+			"logo": "",
+			"description": "",
+			"is_active": true
+		}
+	},
+	{
+		"pk": 6,
+		"model": "licenses.license",
+		"fields": {
+			"name": "Creative Commons Attribution 3.0 Unported License",
+			"slug": "creative-commons-attribution-3",
+			"organization": 2,
+			"abbreviation": "CC-BY",
+			"url": "http://creativecommons.org/licenses/by/3.0/",
+			"logo": "http://i.creativecommons.org/l/by/3.0/88x31.png",
+			"description": "",
+			"is_active": true
+		}
+	},
+	{
+		"pk": 7,
+		"model": "licenses.license",
+		"fields": {
+			"name": "Creative Commons Attribution-Share Alike 3.0 Unported License",
+			"slug": "creative-commons-attribution-share-alike-3",
+			"organization": 2,
+			"abbreviation": "CC-BY-SA",
+			"url": "http://creativecommons.org/licenses/by-sa/3.0/",
+			"logo": "http://i.creativecommons.org/l/by-sa/3.0/88x31.png",
+			"description": "",
+			"is_active": true
+		}
+	}
+]

File licenses/fixtures/common_organizations.json

+[
+	{
+		"pk": 1,
+		"model": "licenses.organization",
+		"fields": {
+			"name": "Free Software Foundation",
+			"slug": "free-software-foundation",
+			"abbreviation": "FSF",
+			"website": "http://www.fsf.org/",
+			"description": "The Free Software Foundation (FSF) is a non-profit corporation founded by Richard Stallman on 4 October 1985 to support the free software movement, a copyleft-based movement which aims to promote the universal freedom to distribute and modify computer software without restriction.\n-- http://en.wikipedia.org/wiki/Free_Software_Foundation"
+		}
+	},
+	{
+		"pk": 2,
+		"model": "licenses.organization",
+		"fields": {
+			"name": "Creative Commons",
+			"slug": "creative-commons",
+			"abbreviation": "CC",
+			"website": "http://creativecommons.org/",
+			"description": "Creative Commons (CC) is a non-profit organization devoted to expanding the range of creative works available for others to build upon legally and to share.[1] The organization has released several copyright licenses known as Creative Commons licenses. These licenses allow creators to easily communicate which rights they reserve, and which rights they waive for the benefit of other creators.\n-- http://en.wikipedia.org/wiki/Creative_Commons"
+		}
+	}
+]

File licenses/models.py

+from django.db.models import Model, CharField, SlugField, URLField, TextField, ForeignKey, BooleanField
+from django.utils.translation import ugettext_lazy as _
+
+class Organization(Model):
+	'''
+		Organization/Authors of licenses.
+	'''
+	name = CharField(_('name'), max_length = 100, unique = True,
+		help_text = _('The complete name of a organization which is resposible for a license.'))
+	slug = SlugField(_('slug'), unique = True)
+	abbreviation = CharField(_('abbreviation'), max_length = 50, blank = True,
+		help_text = _('The short name of the organization.'))
+	website = URLField(_('website'), verify_exists = True,
+		help_text = _('The website of the organization.'))
+	description = TextField(_('description'), blank = True,
+		help_text = _('Additional information about the organization, that is not covered by the above fields.'))
+
+	class Meta:
+		db_table = 'licenses_organization'
+		ordering  = ('name', )
+		verbose_name = _('organization')
+		verbose_name_plural = _('organizations')
+
+	def __unicode__(self):
+		return self.name
+
+	@property
+	def url(self):
+		return self.website
+
+class License(Model):
+	'''
+		A license.
+	'''
+	name = CharField(_('name'), max_length = 200, unique = True,
+		help_text = _('The full name of the license.'))
+	slug = SlugField(_('slug'), unique = True)
+	organization = ForeignKey(Organization, verbose_name = _('organization'), related_name = 'licenses', blank = True, null = True)
+	abbreviation = CharField(_('abbreviation'), max_length = 50, blank = True,
+		help_text = _('The short name of the license.'))
+	url = URLField(_('url'), verify_exists = True,
+		help_text = _('The URL to the legal text/online representation of the license.'))
+	logo = URLField(_('logo'), verify_exists = True, blank = True,
+		help_text = _('A logo/icon/badge that is clearly associated with the license.'))
+	description = TextField(_('description'), blank = True)
+	is_active = BooleanField(_('active'), default=True,
+		help_text = _('Disable, if license shouldn\'t be available to users any more.'))
+
+	def __unicode__(self):
+		return self.name
+
+	class Meta:
+		db_table = 'licenses_license'
+		ordering  = ('name', )
+		verbose_name = _('license')
+		verbose_name_plural = _('licenses')

File licenses/templatetags/__init__.py

Empty file added.

File licenses/templatetags/licensing.py

+from django import template
+from django.utils.safestring import mark_safe
+from django.utils.html import conditional_escape
+from licenses.models import License
+
+register = template.Library()
+
+class GetLicenseNode(template.Node):
+	@classmethod
+	def handle_token(cls, parser, token):
+		tokens = token.contents.split()
+		if len(tokens) == 4:
+			if tokens[2] != 'as':
+				raise template.TemplateSyntaxError("Second argument in %r must be 'as'" % tokens[0])
+			return cls(
+				abbr = tokens[1][1:-1], # TODO: not a safe parser for quotes
+				varname = tokens[3],
+			)
+		else:
+			raise template.TemplateSyntaxError("%r tag requires 3 arguments" % tokens[0])
+
+	def __init__(self, abbr = None, varname = None):
+		self.varname = varname
+		self.abbr = abbr
+
+	def render(self, context):
+		try:
+			license = License.objects.filter(abbreviation = self.abbr)[0]
+		except IndexError:
+			license = None
+		context[self.varname] = license
+		return ''
+
+# TODO: add get_license_by_slug, etc
+@register.tag
+def get_license_by_abbr(parser, token):
+	'''
+		Usage: {% get_license_by_abbr "short_name" as varname %}
+	'''
+	return GetLicenseNode.handle_token(parser, token)
+
+@register.filter
+def license_link(license):
+	'''
+		Usage {{ license|license_link }}
+	'''
+	data = {
+		'name': conditional_escape(license.name),
+		'url': conditional_escape(license.url),
+	}
+	return mark_safe('<a rel="license" href="%(url)s">%(name)s</a>' % data)
+
+@register.filter
+def license_logo(license):
+	'''
+		Usage {{ license|license_logo }}
+	'''
+	data = {
+		'name': conditional_escape(license.name),
+		'url': conditional_escape(license.url),
+		'img': conditional_escape(license.logo),
+	}
+	return mark_safe('<a rel="license" href="%(url)s"><img src="%(img)s" title="%(name)s" alt="%(name)s" /></a>' % data)