summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/templatetags/patch.py
blob: bec0cabcdfca69258473b94524a1721bad41aedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from django import template
import re

register = template.Library()

@register.tag(name = 'ifpatcheditable')
def do_patch_is_editable(parser, token):
    try:
        tag_name, patch_var = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires one argument" \
                % token.contents.split()[0])

    end_tag = 'endifpatcheditable'
    nodelist_true = parser.parse([end_tag, 'else'])

    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse([end_tag])
        parser.delete_first_token()
    else:
        nodelist_false = template.NodeList()

    return EditablePatchNode(patch_var, nodelist_true, nodelist_false)

class EditablePatchNode(template.Node):
    def __init__(self, patch_var, nodelist_true, nodelist_false):
        self.nodelist_true = nodelist_true
        self.nodelist_false = nodelist_false
        self.patch_var = template.Variable(patch_var)
        self.user_var = template.Variable('user')

    def render(self, context):
        try:
            patch = self.patch_var.resolve(context)
            user = self.user_var.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        if not user.is_authenticated():
            return self.nodelist_false.render(context)

        if not patch.is_editable(user):
            return self.nodelist_false.render(context)

        return self.nodelist_true.render(context)