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
21 from django import template
24 register = template.Library()
26 @register.tag(name = 'ifpatcheditable')
27 def do_patch_is_editable(parser, token):
29 tag_name, name, cur_order = token.split_contents()
31 raise template.TemplateSyntaxError("%r tag requires two arguments" \
32 % token.contents.split()[0])
34 end_tag = 'endifpatcheditable'
35 nodelist_true = parser.parse([end_tag, 'else'])
37 token = parser.next_token()
38 if token.contents == 'else':
39 nodelist_false = parser.parse([end_tag])
40 parser.delete_first_token()
42 nodelist_false = template.NodeList()
44 return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
46 class EditablePatchNode(template.Node):
47 def __init__(self, patch_var, nodelist_true, nodelist_false):
48 self.nodelist_true = nodelist_true
49 self.nodelist_false = nodelist_false
50 self.patch_var = template.Variable(patch_var)
51 self.user_var = template.Variable('user')
53 def render(self, context):
55 patch = self.patch_var.resolve(context)
56 user = self.user_var.resolve(context)
57 except template.VariableDoesNotExist:
60 if not user.is_authenticated():
61 return self.nodelist_false.render(context)
63 if not patch.is_editable(user):
64 return self.nodelist_false.render(context)
66 return self.nodelist_true.render(context)