source: trunk/polls/templatetags/poll_extras.py @ 114

Revision 97, 1.5 KB checked in by aarkerio, 10 years ago (diff)

Update Polls

Line 
1from coatl.polls.models import Poll
2from django.contrib.auth.models import User
3from django import template
4import datetime
5
6register = template.Library()
7
8def bloggers():
9    bloggers = User.objects.filter(is_active=True).order_by("username")
10    return {'bloggers':bloggers}
11   
12register.inclusion_tag('blog/members.html')(bloggers)
13
14def view_poll():
15    poll = Poll.objects.filter(published=True).order_by("-id")[0]
16    return {'poll':poll}
17   
18register.inclusion_tag('polls/show.html')(view_poll)
19
20def view_results():
21    poll = Poll.objects.filter(published=True).order_by("-id")[0]
22    return {'poll':poll}
23register.inclusion_tag('polls/votes.html')(view_results)
24
25#other tag
26class CurrentTimeNode(template.Node):
27    def __init__(self, format_string):
28        self.format_string = format_string
29       
30    def render(self, context):
31        return datetime.datetime.now().strftime(self.format_string.encode('utf-8'))
32
33def do_current_time(parser, token):
34    try:
35        # split_contents() knows not to split quoted strings.
36        tag_name, format_string = token.split_contents()
37    except ValueError:
38        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
39    if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
40        raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
41    return CurrentTimeNode(format_string[1:-1])
42
43register.tag('current_time', do_current_time)
44
Note: See TracBrowser for help on using the repository browser.