]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/templatetags/order.py
Inital commit
[patchwork] / apps / patchwork / templatetags / order.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
21 from django import template
22 import re
23
24 register = template.Library()
25
26 @register.tag(name = 'ifpatcheditable')
27 def do_patch_is_editable(parser, token):
28     try:
29         tag_name, name, cur_order = token.split_contents()
30     except ValueError:
31         raise template.TemplateSyntaxError("%r tag requires two arguments" \
32                 % token.contents.split()[0])
33
34     end_tag = 'endifpatcheditable'
35     nodelist_true = parser.parse([end_tag, 'else'])
36
37     token = parser.next_token()
38     if token.contents == 'else':
39         nodelist_false = parser.parse([end_tag])
40         parser.delete_first_token()
41     else:
42         nodelist_false = template.NodeList()
43
44     return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
45
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')
52
53     def render(self, context):
54         try:
55             patch = self.patch_var.resolve(context)
56             user = self.user_var.resolve(context)
57         except template.VariableDoesNotExist:
58             return ''
59
60         if not user.is_authenticated():
61             return self.nodelist_false.render(context)
62
63         if not patch.is_editable(user):
64             return self.nodelist_false.render(context)
65
66         return self.nodelist_true.render(context)