]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/requestcontext.py
e9a267519fa3e1bf772daa31f511f5437aef1afe
[patchwork] / apps / patchwork / requestcontext.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.template import RequestContext
21 from django.utils.html import escape
22 from django.contrib.sites.models import Site
23 from patchwork.filters import Filters
24 from patchwork.models import Bundle, Project
25
26 def bundle(request):
27     user = request.user
28     if not user.is_authenticated():
29         return {}
30     return {'bundles': Bundle.objects.filter(owner = user)}
31
32 def _params_as_qs(params):
33     return '&'.join([ '%s=%s' % (escape(k), escape(v)) for k, v in params ])
34
35 def _params_as_hidden_fields(params):
36     return '\n'.join([ '<input type="hidden" name="%s" value="%s"/>' % \
37                 (escape(k), escape(v)) for k, v in params ])
38
39 class PatchworkRequestContext(RequestContext):
40     def __init__(self, request, project = None,
41             dict = None, processors = None,
42             list_view = None, list_view_params = {}):
43         self._project = project
44         self.filters = Filters(request)
45         if processors is None:
46             processors = []
47         processors.append(bundle)
48         super(PatchworkRequestContext, self). \
49                 __init__(request, dict, processors);
50
51         self.update({'filters': self.filters})
52         if list_view:
53             params = self.filters.params()
54             for param in ['order', 'page']:
55                 value = request.REQUEST.get(param, None)
56                 if value:
57                         params.append((param, value))
58             self.update({
59                 'list_view': {
60                         'view':         list_view,
61                         'view_params':  list_view_params,
62                         'params':       params
63                 }})
64
65         self.projects = Project.objects.all()
66
67         self.update({
68                 'project': self.project,
69                 'site': Site.objects.get_current(),
70                 'other_projects': len(self.projects) > 1
71             })
72
73     def _set_project(self, project):
74         self._project = project
75         self.filters.set_project(project)
76         self.update({'project': self._project})
77
78     def _get_project(self):
79         return self._project
80
81     project = property(_get_project, _set_project)
82
83     def add_message(self, message):
84         self['messages'].append(message)