Skip to content
This repository has been archived by the owner on Jul 17, 2018. It is now read-only.

Commit

Permalink
#2 (actually committing this time) rating_by_user and rating_by_reque…
Browse files Browse the repository at this point in the history
…st template tags.
  • Loading branch information
David Cramer authored and David Cramer committed Feb 11, 2010
1 parent 9e32d33 commit d17aa52
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
21 changes: 20 additions & 1 deletion README.rst
Expand Up @@ -138,12 +138,31 @@ Template Tags

Right now django-ratings has limited support for template tags, and only for Django.

-----------------
rating_by_request
-----------------

Retrieves the ``Vote`` cast by a user on a particular object and
stores it in a context variable. If the user has not voted, the
context variable will be 0::

{% rating_by_request request on instance.field as vote %}

If you are using Coffin, a better approach might be::

{% with instance.field_name.get_rating_for_user(request.user, request.META['REMOTE_ADDR']) as vote %}
Do some magic with {{ vote }}
{% endwith %}

--------------
rating_by_user
--------------

It is recommended that you use rating_by_request as you will gain full support
for anonymous users if they are enabled

Retrieves the ``Vote`` cast by a user on a particular object and
stores it in a context variable. If the user has not voted, the
context variable will be 0::

{% rating_by_user user on widget as vote %}
{% rating_by_user user on instance.field as vote %}
6 changes: 4 additions & 2 deletions djangoratings/fields.py
Expand Up @@ -81,8 +81,8 @@ def get_real_rating(self):
return 0
return float(self.score)/self.votes

def get_rating_for_user(self, user, ip_address):
"""get_rating_for_user(user, ip_address)
def get_rating_for_user(self, user, ip_address=None):
"""get_rating_for_user(user, ip_address=None)
Returns the rating for a user or anonymous IP."""
kwargs = dict(
Expand All @@ -92,6 +92,8 @@ def get_rating_for_user(self, user, ip_address):
)

if not (user and user.is_authenticated()):
if not ip_address:
raise ValueError('``user`` or ``ip_address`` must be present.')
kwargs['user__isnull'] = True
kwargs['ip_address'] = ip_address
else:
Expand Down
Empty file.
89 changes: 89 additions & 0 deletions djangoratings/templatetags/ratings.py
@@ -0,0 +1,89 @@
"""
Template tags for Django
"""
# TODO: add in Jinja tags if Coffin is available

from django import template
from django.contrib.contenttypes.models import ContentType
from django.db.models import ObjectDoesNotExist

from djangoratings.models import Vote

register = template.Library()

class RatingByRequestNode(template.Node):
def __init__(self, request, obj, context_var):
self.request = request
self.obj, self.field_name = obj.split('.')
self.context_var = context_var

def render(self, context):
try:
request = template.resolve_variable(self.request, context)
obj = template.resolve_variable(self.obj, context)
field = getattr(obj, self.field_name)
except (template.VariableDoesNotExist, AttributeError):
return ''
try:
vote = field.get_rating_for_user(request.user, request.META['REMOTE_ADDR'])
context[self.context_var] = vote
except ObjectDoesNotExist:
context[self.context_var] = 0
return ''

def do_rating_by_request(parser, token):
"""
Retrieves the ``Vote`` cast by a user on a particular object and
stores it in a context variable. If the user has not voted, the
context variable will be 0.
Example usage::
{% rating_by_request request on instance as vote %}
"""

bits = token.contents.split()
if len(bits) != 6:
raise template.TemplateSyntaxError("'%s' tag takes exactly five arguments" % bits[0])
if bits[2] != 'on':
raise template.TemplateSyntaxError("second argument to '%s' tag must be 'on'" % bits[0])
if bits[4] != 'as':
raise template.TemplateSyntaxError("fourth argument to '%s' tag must be 'as'" % bits[0])
return RatingByRequestNode(bits[1], bits[3], bits[5])
register.tag('rating_by_request', do_rating_by_request)

class RatingByUserNode(RatingByRequestNode):
def render(self, context):
try:
user = template.resolve_variable(self.request, context)
obj = template.resolve_variable(self.obj, context)
field = getattr(obj, self.field_name)
except template.VariableDoesNotExist:
return ''
try:
vote = field.get_rating_for_user(user)
context[self.context_var] = vote
except ObjectDoesNotExist:
context[self.context_var] = 0
return ''

def do_rating_by_user(parser, token):
"""
Retrieves the ``Vote`` cast by a user on a particular object and
stores it in a context variable. If the user has not voted, the
context variable will be 0.
Example usage::
{% rating_by_user user on instance as vote %}
"""

bits = token.contents.split()
if len(bits) != 6:
raise template.TemplateSyntaxError("'%s' tag takes exactly five arguments" % bits[0])
if bits[2] != 'on':
raise template.TemplateSyntaxError("second argument to '%s' tag must be 'on'" % bits[0])
if bits[4] != 'as':
raise template.TemplateSyntaxError("fourth argument to '%s' tag must be 'as'" % bits[0])
return RatingByUserNode(bits[1], bits[3], bits[5])
register.tag('rating_by_user', do_rating_by_user)

0 comments on commit d17aa52

Please sign in to comment.