]> git.ozlabs.org Git - patchwork/blob - patchwork/templatetags/listurl.py
trivial: Resolve some PEP8 issues
[patchwork] / patchwork / templatetags / listurl.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 from django.utils.html import escape
22 from django.utils.encoding import smart_str
23 from patchwork.filters import filterclasses
24 from django.conf import settings
25 from django.core.urlresolvers import reverse, NoReverseMatch
26
27 register = template.Library()
28
29 # params to preserve across views
30 list_params = [c.param for c in filterclasses] + ['order', 'page']
31
32
33 class ListURLNode(template.defaulttags.URLNode):
34
35     def __init__(self, kwargs):
36         super(ListURLNode, self).__init__(None, [], {}, False)
37         self.params = {}
38         for (k, v) in kwargs.iteritems():
39             if k in list_params:
40                 self.params[k] = v
41
42     def render(self, context):
43         view_name = template.Variable('list_view.view').resolve(context)
44         kwargs = template.Variable('list_view.view_params').resolve(context)
45
46         str = None
47         try:
48             str = reverse(view_name, args=[], kwargs=kwargs)
49         except NoReverseMatch:
50             try:
51                 project_name = settings.SETTINGS_MODULE.split('.')[0]
52                 str = reverse(project_name + '.' + view_name,
53                               args=[], kwargs=kwargs)
54             except NoReverseMatch:
55                 raise
56
57         if str is None:
58             return ''
59
60         params = []
61         try:
62             qs_var = template.Variable('list_view.params')
63             params = dict(qs_var.resolve(context))
64         except Exception:
65             pass
66
67         for (k, v) in self.params.iteritems():
68             params[smart_str(k, 'ascii')] = v.resolve(context)
69
70         if not params:
71             return str
72
73         return str + '?' + '&'.join(
74             ['%s=%s' % (k, escape(v)) for (k, v) in params.iteritems()])
75
76
77 @register.tag
78 def listurl(parser, token):
79     bits = token.contents.split(' ', 1)
80     if len(bits) < 1:
81         raise template.TemplateSyntaxError(
82             "'%s' takes at least one argument (path to a view)" % bits[0])
83     kwargs = {}
84     if len(bits) > 1:
85         for arg in bits[1].split(','):
86             if '=' in arg:
87                 k, v = arg.split('=', 1)
88                 k = k.strip()
89                 kwargs[k] = parser.compile_filter(v)
90             else:
91                 raise template.TemplateSyntaxError(
92                     "'%s' requires name=value params" % bits[0])
93     return ListURLNode(kwargs)