1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
20 from django import template
23 register = template.Library()
25 @register.tag(name = 'ifpatcheditable')
26 def do_patch_is_editable(parser, token):
28 tag_name, patch_var = token.split_contents()
30 raise template.TemplateSyntaxError("%r tag requires one argument" \
31 % token.contents.split()[0])
33 end_tag = 'endifpatcheditable'
34 nodelist_true = parser.parse([end_tag, 'else'])
36 token = parser.next_token()
37 if token.contents == 'else':
38 nodelist_false = parser.parse([end_tag])
39 parser.delete_first_token()
41 nodelist_false = template.NodeList()
43 return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
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')
52 def render(self, context):
54 patch = self.patch_var.resolve(context)
55 user = self.user_var.resolve(context)
56 except template.VariableDoesNotExist:
59 if not user.is_authenticated():
60 return self.nodelist_false.render(context)
62 if not patch.is_editable(user):
63 return self.nodelist_false.render(context)
65 return self.nodelist_true.render(context)