summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/templatetags/pwurl.py
blob: 98bc1ca8cc5f60ba8af27a9dfaf556e832b29e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
from patchwork.filters import filterclasses
import re

register = template.Library()

# params to preserve across views
list_params = [ c.param for c in filterclasses ] + ['order', 'page']

class ListURLNode(template.defaulttags.URLNode):
    def __init__(self, *args, **kwargs):
        super(ListURLNode, self).__init__(*args, **kwargs)
        self.params = {}
        for (k, v) in kwargs:
            if k in list_params:
                self.params[k] = v

    def render(self, context):
        self.view_name = template.Variable('list_view.view')
        str = super(ListURLNode, self).render(context)
        if str == '':
            return str
        params = []
        try:
            qs_var = template.Variable('list_view.params')
            params = dict(qs_var.resolve(context))
        except Exception:
            pass

        params.update(self.params)

        if not params:
            return str

        return str + '?' + '&'.join(['%s=%s' % (k, escape(v)) \
                        for (k, v) in params.iteritems()])

@register.tag
def listurl(parser, token):
    bits = token.contents.split(' ', 1)
    if len(bits) < 1:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    args = ['']
    kwargs = {}
    if len(bits) > 1:
        for arg in bits[2].split(','):
            if '=' in arg:
                k, v = arg.split('=', 1)
                k = k.strip()
                kwargs[k] = parser.compile_filter(v)
            else:
                args.append(parser.compile_filter(arg))
    return PatchworkURLNode(bits[1], args, kwargs)