summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/requestcontext.py
blob: 3b1afaf6c849ce5025979f903478aae287c69cf9 (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
# 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.template import RequestContext
from django.utils.html import escape
from django.contrib.sites.models import Site
from django.conf import settings
from patchwork.filters import Filters
from patchwork.models import Bundle, Project

def bundle(request):
    user = request.user
    if not user.is_authenticated():
        return {}
    return {'bundles': Bundle.objects.filter(owner = user)}

def _params_as_qs(params):
    return '&'.join([ '%s=%s' % (escape(k), escape(v)) for k, v in params ])

def _params_as_hidden_fields(params):
    return '\n'.join([ '<input type="hidden" name="%s" value="%s"/>' % \
                (escape(k), escape(v)) for k, v in params ])

class PatchworkRequestContext(RequestContext):
    def __init__(self, request, project = None,
            dict = None, processors = None,
            list_view = None, list_view_params = {}):
        self._project = project
        self.filters = Filters(request)
        if processors is None:
            processors = []
        processors.append(bundle)
        super(PatchworkRequestContext, self). \
                __init__(request, dict, processors);

        self.update({
                'filters': self.filters,
                'messages': [],
            })
        if list_view:
            params = self.filters.params()
            for param in ['order', 'page']:
                value = request.REQUEST.get(param, None)
                if value:
                        params.append((param, value))
            self.update({
                'list_view': {
                        'view':         list_view,
                        'view_params':  list_view_params,
                        'params':       params
                }})

        self.projects = Project.objects.all()

        self.update({
                'project': self.project,
                'site': Site.objects.get_current(),
                'settings': settings,
                'other_projects': len(self.projects) > 1
            })

    def _set_project(self, project):
        self._project = project
        self.filters.set_project(project)
        self.update({'project': self._project})

    def _get_project(self):
        return self._project

    project = property(_get_project, _set_project)

    def add_message(self, message):
        self['messages'].append(message)