summaryrefslogtreecommitdiffstats
path: root/patchwork/templatetags/listurl.py
blob: 5fe03e469c43d1b46b3015abec468dda9e43ee8a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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 django.utils.encoding import smart_str
from patchwork.filters import filterclasses
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch
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, kwargs):
        super(ListURLNode, self).__init__(None, [], {}, False)
        self.params = {}
        for (k, v) in kwargs.iteritems():
            if k in list_params:
                self.params[k] = v

    def render(self, context):
        view_name = template.Variable('list_view.view').resolve(context)
        kwargs = template.Variable('list_view.view_params') \
                      .resolve(context)

        str = None
        try:
            str = reverse(view_name, args=[], kwargs=kwargs)
        except NoReverseMatch:
            try:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                str = reverse(project_name + '.' + view_name,
                               args=[], kwargs=kwargs)
            except NoReverseMatch:
                raise
        
        if str is None:
            return ''

        params = []
        try:
            qs_var = template.Variable('list_view.params')
            params = dict(qs_var.resolve(context))
        except Exception:
            pass

        for (k, v) in self.params.iteritems():
            params[smart_str(k,'ascii')] = v.resolve(context)

        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])
    kwargs = {}
    if len(bits) > 1:
        for arg in bits[1].split(','):
            if '=' in arg:
                k, v = arg.split('=', 1)
                k = k.strip()
                kwargs[k] = parser.compile_filter(v)
            else:
                raise TemplateSyntaxError("'%s' requires name=value params" \
                                          % bits[0])
    return ListURLNode(kwargs)

class ListFieldsNode(template.Node):
    def __init__(self, params):
        self.params = params

    def render(self, context):
        self.view_name = template.Variable('list_view.view').resolve(context)
        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 = ''
        for (k, v) in params.iteritems():
            str += '<input type="hidden" name="%s" value="%s"\>' % \
                   (k, escape(v))

        return mark_safe(str)

@register.tag
def listfields(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])
    params = {}
    if len(bits) > 2:
        for arg in bits[2].split(','):
            if '=' in arg:
                k, v = arg.split('=', 1)
                k = k.strip()
                params[k] = parser.compile_filter(v)
            else:
                raise TemplateSyntaxError("'%s' requires name=value params" \
                                          % bits[0])
    return ListFieldsNode(bits[1], params)