Permalink
Browse files

Added votes_by_user tag

git-svn-id: https://django-voting.googlecode.com/svn/trunk@24 662f01ad-f42a-0410-a340-718c64ddaef4
  • Loading branch information...
1 parent 71ea2cc commit 7b9cfc354670bf9b235a822385820d1a742eec0c @insin insin committed Mar 13, 2007
Showing with 33 additions and 0 deletions.
  1. +33 −0 templatetags/voting_tags.py
@@ -35,6 +35,21 @@ def render(self, context):
context[self.context_var] = Vote.objects.get_for_user(self.object, self.user)
return ''
+class VotesByUserNode(template.Node):
+ def __init__(self, user, objects, context_var):
+ self.user = user
+ self.objects = objects
+ self.context_var = context_var
+
+ def render(self, context):
+ try:
+ user = template.resolve_variable(self.user, context)
+ objects = template.resolve_variable(self.objects, context)
+ except template.VariableDoesNotExist:
+ return ''
+ context[self.context_var] = Vote.objects.get_for_user_in_bulk(objects, user)
+ return ''
+
def do_score_for_object(parser, token):
"""
Retrieves the total score for an object and the number of votes
@@ -72,8 +87,26 @@ def do_vote_by_user(parser, token):
raise template.TemplateSyntaxError("third argument to '%s' tag must be 'as'" % bits[0])
return VoteByUserNode(bits[1], bits[2], bits[4])
+def do_votes_by_user(parser, token):
+ """
+ Retrieves the votes cast by a user on a list of objects as a
+ dictionary keyed with object ids and stores it in a context
+ variable.
+
+ Example usage::
+
+ {% votes_by_user user object_list as vote_dict %}
+ """
+ bits = token.contents.split()
+ if len(bits) != 5:
+ raise template.TemplateSyntaxError("'%s' tag takes exactly four arguments" % bits[0])
+ if bits[3] != 'as':
+ raise template.TemplateSyntaxError("third argument to '%s' tag must be 'as'" % bits[0])
+ return VotesByUserNode(bits[1], bits[2], bits[4])
+
register.tag('score_for_object', do_score_for_object)
register.tag('vote_by_user', do_vote_by_user)
+register.tag('votes_by_user', do_votes_by_user)
# Simple Tags

0 comments on commit 7b9cfc3

Please sign in to comment.