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