]> git.ozlabs.org Git - patchwork/blob - patchwork/requestcontext.py
3652edc8f8c58f0dbe3dc0227fbca4e1185a02aa
[patchwork] / 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 django.conf import settings
24 from patchwork.filters import Filters
25 from patchwork.models import Bundle, Project
26
27 def bundle(request):
28     user = request.user
29     if not user.is_authenticated():
30         return {}
31     return {'bundles': Bundle.objects.filter(owner = user)}
32
33
34 class PatchworkRequestContext(RequestContext):
35     def __init__(self, request, project = None,
36             dict = None, processors = None,
37             list_view = None, list_view_params = {}):
38         self._project = project
39         self.filters = Filters(request)
40         if processors is None:
41             processors = []
42         processors.append(bundle)
43         super(PatchworkRequestContext, self). \
44                 __init__(request, dict, processors);
45
46         self.update({
47                 'filters': self.filters,
48                 'messages': [],
49             })
50         if list_view:
51             params = self.filters.params()
52             for param in ['order', 'page']:
53                 data = {}
54                 if request.method == 'GET':
55                     data = request.GET
56                 elif request.method == 'POST':
57                     data = request.POST
58
59                 value = data.get(param, None)
60                 if value:
61                         params.append((param, value))
62             self.update({
63                 'list_view': {
64                         'view':         list_view,
65                         'view_params':  list_view_params,
66                         'params':       params
67                 }})
68
69         self.projects = Project.objects.all()
70
71         self.update({
72                 'project': self.project,
73                 'site': Site.objects.get_current(),
74                 'settings': settings,
75                 'other_projects': len(self.projects) > 1
76             })
77
78     def _set_project(self, project):
79         self._project = project
80         self.filters.set_project(project)
81         self.update({'project': self._project})
82
83     def _get_project(self):
84         return self._project
85
86     project = property(_get_project, _set_project)
87
88     def add_message(self, message):
89         self['messages'].append(message)