~widelands-dev/widelands-website/solitaire_html_documentation

« back to all changes in this revision

Viewing changes to djangoratings/templatetags/ratings.py

  • Committer: franku
  • Date: 2016-05-18 19:31:46 UTC
  • mto: This revision was merged to the branch mainline in revision 409.
  • Revision ID: somal@arcor.de-20160518193146-w5dmezymi3wlnhvl
added djangoratings and adjust it to django 1.8; updated update_problems.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Template tags for Django
 
3
"""
 
4
# TODO: add in Jinja tags if Coffin is available
 
5
 
 
6
from django import template
 
7
from django.contrib.contenttypes.models import ContentType
 
8
from django.db.models import ObjectDoesNotExist
 
9
 
 
10
from djangoratings.models import Vote
 
11
 
 
12
register = template.Library()
 
13
 
 
14
class RatingByRequestNode(template.Node):
 
15
    def __init__(self, request, obj, context_var):
 
16
        self.request = request
 
17
        self.obj, self.field_name = obj.split('.')
 
18
        self.context_var = context_var
 
19
    
 
20
    def render(self, context):
 
21
        try:
 
22
            request = template.resolve_variable(self.request, context)
 
23
            obj = template.resolve_variable(self.obj, context)
 
24
            field = getattr(obj, self.field_name)
 
25
        except (template.VariableDoesNotExist, AttributeError):
 
26
            return ''
 
27
        try:
 
28
            vote = field.get_rating_for_user(request.user, request.META['REMOTE_ADDR'], request.COOKIES)
 
29
            context[self.context_var] = vote
 
30
        except ObjectDoesNotExist:
 
31
            context[self.context_var] = 0
 
32
        return ''
 
33
 
 
34
def do_rating_by_request(parser, token):
 
35
    """
 
36
    Retrieves the ``Vote`` cast by a user on a particular object and
 
37
    stores it in a context variable. If the user has not voted, the
 
38
    context variable will be 0.
 
39
    
 
40
    Example usage::
 
41
    
 
42
        {% rating_by_request request on instance as vote %}
 
43
    """
 
44
    
 
45
    bits = token.contents.split()
 
46
    if len(bits) != 6:
 
47
        raise template.TemplateSyntaxError("'%s' tag takes exactly five arguments" % bits[0])
 
48
    if bits[2] != 'on':
 
49
        raise template.TemplateSyntaxError("second argument to '%s' tag must be 'on'" % bits[0])
 
50
    if bits[4] != 'as':
 
51
        raise template.TemplateSyntaxError("fourth argument to '%s' tag must be 'as'" % bits[0])
 
52
    return RatingByRequestNode(bits[1], bits[3], bits[5])
 
53
register.tag('rating_by_request', do_rating_by_request)
 
54
 
 
55
class RatingByUserNode(RatingByRequestNode):
 
56
    def render(self, context):
 
57
        try:
 
58
            user = template.resolve_variable(self.request, context)
 
59
            obj = template.resolve_variable(self.obj, context)
 
60
            field = getattr(obj, self.field_name)
 
61
        except template.VariableDoesNotExist:
 
62
            return ''
 
63
        try:
 
64
            vote = field.get_rating_for_user(user)
 
65
            context[self.context_var] = vote
 
66
        except ObjectDoesNotExist:
 
67
            context[self.context_var] = 0
 
68
        return ''
 
69
 
 
70
def do_rating_by_user(parser, token):
 
71
    """
 
72
    Retrieves the ``Vote`` cast by a user on a particular object and
 
73
    stores it in a context variable. If the user has not voted, the
 
74
    context variable will be 0.
 
75
    
 
76
    Example usage::
 
77
    
 
78
        {% rating_by_user user on instance as vote %}
 
79
    """
 
80
    
 
81
    bits = token.contents.split()
 
82
    if len(bits) != 6:
 
83
        raise template.TemplateSyntaxError("'%s' tag takes exactly five arguments" % bits[0])
 
84
    if bits[2] != 'on':
 
85
        raise template.TemplateSyntaxError("second argument to '%s' tag must be 'on'" % bits[0])
 
86
    if bits[4] != 'as':
 
87
        raise template.TemplateSyntaxError("fourth argument to '%s' tag must be 'as'" % bits[0])
 
88
    return RatingByUserNode(bits[1], bits[3], bits[5])
 
89
register.tag('rating_by_user', do_rating_by_user)