Commits

Jannis Leidel  committed edea1d8

Added get_license_by_slug template tag, made template filters use templates and made it Python 2.3 compatible

  • Participants
  • Parent commits 39c47b9

Comments (0)

Files changed (4)

File licenses/models.py

     def __unicode__(self):
         return self.name
 
-    @property
+    #@property
     def url(self):
         return self.website
-
+    url = property(url)
 
 class License(Model):
     '''

File licenses/templates/licenses/link.html

+<a rel="license" href="{{ url }}">{{ name }}</a>

File licenses/templates/licenses/logo.html

+<a rel="license" href="{{ url }}"><img src="{{ img }}" title="{{ name }}" alt="{{ name }}" /></a>

File licenses/templatetags/licensing.py

 from django import template
+from django.template.loader import render_to_string
 from django.utils.safestring import mark_safe
 from django.utils.html import conditional_escape
 from licenses.models import License
     '''
 
     @classmethod
-    def handle_token(cls, parser, token):
-        tokens = token.contents.split()
+    def handle_token(cls, parser, token, field):
+        tokens = token.split_contents()
         if len(tokens) == 4:
             if tokens[2] != 'as':
                 raise template.TemplateSyntaxError("Second argument in %s must be 'as'" % tokens[0])
-            return cls(
-                abbr = tokens[1][1:-1], # TODO: not a safe parser for quotes
-                varname = tokens[3],
-            )
+            return cls(query=tokens[1], varname=tokens[3], field=field)
         else:
             raise template.TemplateSyntaxError("%r tag requires 3 arguments" % tokens[0])
 
-    def __init__(self, abbr = None, varname = None):
+    def __init__(self, query=None, varname=None, field=None):
         self.varname = varname
-        self.abbr = abbr
+        self.lookup = lookup
+        self.field = field
 
     def render(self, context):
         try:
-            license = License.objects.filter(abbreviation = self.abbr)[0]
+            lookup_dict = {self.field: self.query}
+            license = License.objects.filter(**lookup_dict)[0]
         except IndexError:
             license = None
         context[self.varname] = license
         return ''
 
 
-# TODO: add get_license_by_slug, etc
-@register.tag
+#@register.tag
 def get_license_by_abbr(parser, token):
     '''
         Usage: {% get_license_by_abbr "short_name" as varname %}
     '''
-    return GetLicenseNode.handle_token(parser, token)
+    return GetLicenseNode.handle_token(parser, token, field='abbreviation')
+get_license_by_abbr = register.tag(get_license_by_abbr)
 
+#@register.tag
+def get_license_by_slug(parser, token):
+    '''
+        Usage: {% get_license_by_slug "slug" as varname %}
+    '''
+    return GetLicenseNode.handle_token(parser, token, field='slug')
+get_license_by_slug = register.tag(get_license_by_slug)
 
-# TODO: ?? put html into template?
-@register.filter
+#@register.filter
 def license_link(license):
     '''
         Usage {{ license|license_link }}
     '''
-    data = {
+    return render_to_string('licenses/link.html', {
         '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(license_link)
 
-
-@register.filter
+#@register.filter
 def license_logo(license):
     '''
         Usage {{ license|license_logo }}
     '''
-    data = {
+    return render_to_string('licenses/logo.html', {
         '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)
+    })
+register.filter(license_logo)