Assembla home | Assembla project page
 

root/trunk/polls/templatetags/poll_extras.py

Revision 97, 1.5 kB (checked in by aarkerio, 5 months ago)

Update Polls

Line 
1 from coatl.polls.models import Poll
2 from django.contrib.auth.models import User
3 from django import template
4 import datetime
5
6 register = template.Library()
7
8 def bloggers():
9     bloggers = User.objects.filter(is_active=True).order_by("username")
10     return {'bloggers':bloggers}
11    
12 register.inclusion_tag('blog/members.html')(bloggers)
13
14 def view_poll():
15     poll = Poll.objects.filter(published=True).order_by("-id")[0]
16     return {'poll':poll}
17    
18 register.inclusion_tag('polls/show.html')(view_poll)
19
20 def view_results():
21     poll = Poll.objects.filter(published=True).order_by("-id")[0]
22     return {'poll':poll}
23 register.inclusion_tag('polls/votes.html')(view_results)
24
25 #other tag
26 class 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
33 def 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
43 register.tag('current_time', do_current_time)
44
Note: See TracBrowser for help on using the browser.