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
21 from patchwork.forms import MultiplePatchForm
22 from patchwork.models import Bundle, Project, BundlePatch, UserProfile
23 from django.shortcuts import get_object_or_404
25 def get_patch_ids(d, prefix = 'patch_id'):
28 for (k, v) in d.items():
44 'state': 'state__ordering',
45 'submitter': 'submitter__name',
46 'delegate': 'delegate__username',
48 default_order = ('date', True)
50 def __init__(self, str = None, editable = False):
52 self.editable = editable
57 if str is None or str == '':
58 (self.order, self.reversed) = self.default_order
66 if str not in self.order_map.keys():
67 (self.order, self.reversed) = self.default_order
71 self.reversed = reversed
82 def reversed_name(self):
86 return '-' + self.order
89 q = self.order_map[self.order]
94 bundle_actions = ['create', 'add', 'remove']
95 def set_bundle(user, project, action, data, patches, context):
98 if action == 'create':
99 bundle_name = data['bundle_name'].strip()
101 return ['No bundle name was specified']
103 bundle = Bundle(owner = user, project = project,
106 context.add_message("Bundle %s created" % bundle.name)
109 bundle = get_object_or_404(Bundle, id = data['bundle_id'])
111 elif action =='remove':
112 bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])
115 return ['no such bundle']
117 for patch in patches:
118 if action == 'create' or action == 'add':
119 bundlepatch_count = BundlePatch.objects.filter(bundle = bundle,
120 patch = patch).count()
121 if bundlepatch_count == 0:
122 bundle.append_patch(patch)
123 context.add_message("Patch '%s' added to bundle %s" % \
124 (patch.name, bundle.name))
126 context.add_message("Patch '%s' already in bundle %s" % \
127 (patch.name, bundle.name))
129 elif action == 'remove':
131 bp = BundlePatch.objects.get(bundle = bundle, patch = patch)
133 context.add_message("Patch '%s' removed from bundle %s\n" % \
134 (patch.name, bundle.name))
143 def set_patches(user, project, action, data, patches, context):
145 form = MultiplePatchForm(project = project, data = data)
148 project = Project.objects.get(id = data['project'])
150 errors = ['No such project']
151 return (errors, form)
155 # this may be a bundle action, which doesn't modify a patch. in this
156 # case, don't require a valid form, or patch editing permissions
157 if action in bundle_actions:
158 errors = set_bundle(user, project, action, data, patches, context)
159 return (errors, form)
161 if not form.is_valid():
162 errors = ['The submitted form data was invalid']
163 return (errors, form)
165 for patch in patches:
166 if not patch.is_editable(user):
167 errors.append('You don\'t have permissions to edit the ' + \
172 if action == 'update':
178 if len(patches) == 1:
181 str = 'patches ' + str
182 context.add_message(str)
184 return (errors, form)