]> git.ozlabs.org Git - patchwork/blob - patchwork/templatetags/patch.py
Move to a more recent django project structure
[patchwork] / patchwork / templatetags / patch.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 from django import template
21 import re
22
23 register = template.Library()
24
25 @register.tag(name = 'ifpatcheditable')
26 def do_patch_is_editable(parser, token):
27     try:
28         tag_name, patch_var = token.split_contents()
29     except ValueError:
30         raise template.TemplateSyntaxError("%r tag requires one argument" \
31                 % token.contents.split()[0])
32
33     end_tag = 'endifpatcheditable'
34     nodelist_true = parser.parse([end_tag, 'else'])
35
36     token = parser.next_token()
37     if token.contents == 'else':
38         nodelist_false = parser.parse([end_tag])
39         parser.delete_first_token()
40     else:
41         nodelist_false = template.NodeList()
42
43     return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
44
45 class EditablePatchNode(template.Node):
46     def __init__(self, patch_var, nodelist_true, nodelist_false):
47         self.nodelist_true = nodelist_true
48         self.nodelist_false = nodelist_false
49         self.patch_var = template.Variable(patch_var)
50         self.user_var = template.Variable('user')
51
52     def render(self, context):
53         try:
54             patch = self.patch_var.resolve(context)
55             user = self.user_var.resolve(context)
56         except template.VariableDoesNotExist:
57             return ''
58
59         if not user.is_authenticated():
60             return self.nodelist_false.render(context)
61
62         if not patch.is_editable(user):
63             return self.nodelist_false.render(context)
64
65         return self.nodelist_true.render(context)