]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/__init__.py
e4043bbb5a2585202cd409eaf80a8754e7775f49
[patchwork] / apps / patchwork / views / __init__.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
21 from base import *
22 from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle
23 from patchwork.paginator import Paginator
24 from patchwork.forms import MultiplePatchForm
25
26 def generic_list(request, project, view,
27         view_args = {}, filter_settings = [], patches = None,
28         editable_order = False):
29
30     context = PatchworkRequestContext(request,
31             list_view = view,
32             list_view_params = view_args)
33
34     context.project = project
35     order = Order(request.REQUEST.get('order'), editable = editable_order)
36
37     # Explicitly set data to None because request.POST will be an empty dict
38     # when the form is not submitted, but passing a non-None data argument to
39     # a forms.Form will make it bound and we don't want that to happen unless
40     # there's been a form submission.
41     data = None
42     if request.method == 'POST':
43         data = request.POST
44     user = request.user
45     properties_form = None
46     if (user.is_authenticated()
47             and project in user.get_profile().maintainer_projects.all()):
48         properties_form = MultiplePatchForm(project, data = data)
49
50     if request.method == 'POST' and data.get('form') == 'patchlistform':
51         action = data.get('action', '').lower()
52
53         # special case: the user may have hit enter in the 'create bundle'
54         # text field, so if non-empty, assume the create action:
55         if data.get('bundle_name', False):
56             action = 'create'
57
58         ps = Patch.objects.filter(id__in = get_patch_ids(data))
59
60         if action in bundle_actions:
61             errors = set_bundle(user, project, action, data, ps, context)
62
63         elif properties_form and action == properties_form.action:
64             errors = process_multiplepatch_form(properties_form, user,
65                                                 action, ps, context)
66         else:
67             errors = []
68
69         if errors:
70             context['errors'] = errors
71
72     for (filterclass, setting) in filter_settings:
73         if isinstance(setting, dict):
74             context.filters.set_status(filterclass, **setting)
75         elif isinstance(setting, list):
76             context.filters.set_status(filterclass, *setting)
77         else:
78             context.filters.set_status(filterclass, setting)
79
80     if patches is None:
81         patches = Patch.objects.filter(project=project)
82
83     patches = context.filters.apply(patches)
84     if not editable_order:
85         patches = patches.order_by(order.query())
86
87     paginator = Paginator(request, patches)
88
89     context.update({
90             'page':             paginator.current_page,
91             'patchform':        properties_form,
92             'project':          project,
93             'order':            order,
94             })
95
96     return context
97
98
99 def process_multiplepatch_form(form, user, action, patches, context):
100     errors = []
101     if not form.is_valid() or action != form.action:
102         return ['The submitted form data was invalid']
103
104     if len(patches) == 0:
105         context.add_message("No patches selected; nothing updated")
106         return errors
107
108     changed_patches = 0
109     for patch in patches:
110         if not patch.is_editable(user):
111             errors.append("You don't have permissions to edit patch '%s'"
112                             % patch.name)
113             continue
114
115         changed_patches += 1
116         form.save(patch)
117
118     if changed_patches == 1:
119         context.add_message("1 patch updated")
120     elif changed_patches > 1:
121         context.add_message("%d patches updated" % changed_patches)
122     else:
123         context.add_message("No patches updated")
124
125     return errors