forked from pinax/symposion
/
sponsors_pro_tags.py
47 lines (37 loc) · 1.43 KB
/
sponsors_pro_tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django import template
from symposion.sponsors_pro.models import Sponsor
register = template.Library()
class SponsorsNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
bits = token.split_contents()
section = "all"
if len(bits) != 3 and len(bits) != 4:
raise template.TemplateSyntaxError("%r takes two to three arguments "
"(second to last argument must be 'as')" % bits[0])
if bits[-2] != "as":
raise template.TemplateSyntaxError("Second to last argument to %r must be "
"'as'" % bits[0])
if len(bits) == 4:
section = bits[1]
print section
return cls(bits[-1], section)
def __init__(self, context_var, section):
self.section = section
self.context_var = context_var
def render(self, context):
queryset = Sponsor.objects.active()
if self.section == "header":
queryset = queryset.filter(level__in=[1])
if self.section == "scroll":
queryset = queryset.filter(level__in=[2, 3])
if self.section == "footer":
queryset = queryset.filter(level__in=[4, 10])
context[self.context_var] = queryset
return u""
@register.tag
def sponsors(parser, token):
"""
{% sponsors [header, scroll, footer, all] as sponsors %}
"""
return SponsorsNode.handle_token(parser, token)