1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
26 def generic_list(request, project, view,
27 view_args = {}, filter_settings = [], patches = None,
28 editable_order = False):
30 context = PatchworkRequestContext(request,
32 list_view_params = view_args)
34 context.project = project
35 order = Order(request.REQUEST.get('order'), editable = editable_order)
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.
42 if request.method == 'POST':
45 properties_form = None
46 if project.is_editable(user):
48 # we only pass the post data to the MultiplePatchForm if that was
49 # the actual form submitted
51 if data and data.get('form', '') == 'patchlistform':
54 properties_form = MultiplePatchForm(project, data = data_tmp)
56 if request.method == 'POST' and data.get('form') == 'patchlistform':
57 action = data.get('action', '').lower()
59 # special case: the user may have hit enter in the 'create bundle'
60 # text field, so if non-empty, assume the create action:
61 if data.get('bundle_name', False):
64 ps = Patch.objects.filter(id__in = get_patch_ids(data))
66 if action in bundle_actions:
67 errors = set_bundle(user, project, action, data, ps, context)
69 elif properties_form and action == properties_form.action:
70 errors = process_multiplepatch_form(properties_form, user,
76 context['errors'] = errors
78 for (filterclass, setting) in filter_settings:
79 if isinstance(setting, dict):
80 context.filters.set_status(filterclass, **setting)
81 elif isinstance(setting, list):
82 context.filters.set_status(filterclass, *setting)
84 context.filters.set_status(filterclass, setting)
87 patches = Patch.objects.filter(project=project)
89 patches = context.filters.apply(patches)
90 if not editable_order:
91 patches = patches.order_by(order.query())
93 paginator = Paginator(request, patches)
96 'page': paginator.current_page,
97 'patchform': properties_form,
105 def process_multiplepatch_form(form, user, action, patches, context):
107 if not form.is_valid() or action != form.action:
108 return ['The submitted form data was invalid']
110 if len(patches) == 0:
111 context.add_message("No patches selected; nothing updated")
115 for patch in patches:
116 if not patch.is_editable(user):
117 errors.append("You don't have permissions to edit patch '%s'"
124 if changed_patches == 1:
125 context.add_message("1 patch updated")
126 elif changed_patches > 1:
127 context.add_message("%d patches updated" % changed_patches)
129 context.add_message("No patches updated")