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, State, UserProfile
23 from django.conf import settings
24 from django.shortcuts import render_to_response, get_object_or_404
26 def get_patch_ids(d, prefix = 'patch_id'):
29 for (k, v) in d.items():
45 'state': 'state__ordering',
46 'submitter': 'submitter__name',
47 'delegate': 'delegate__username',
49 default_order = ('date', True)
51 def __init__(self, str = None, editable = False):
53 self.editable = editable
58 if str is None or str == '':
59 (self.order, self.reversed) = self.default_order
67 if str not in self.order_map.keys():
68 (self.order, self.reversed) = self.default_order
72 self.reversed = reversed
83 def reversed_name(self):
87 return '-' + self.order
90 q = self.order_map[self.order]
95 bundle_actions = ['create', 'add', 'remove']
96 def set_bundle(user, project, action, data, patches, context):
99 if action == 'create':
100 bundle = Bundle(owner = user, project = project,
101 name = data['bundle_name'])
103 context.add_message("Bundle %s created" % bundle.name)
106 bundle = get_object_or_404(Bundle, id = data['bundle_id'])
108 elif action =='remove':
109 bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])
112 return ['no such bundle']
114 for patch in patches:
115 if action == 'create' or action == 'add':
117 bundle.append_patch(patch)
118 context.add_message("Patch '%s' added to bundle %s" % \
119 (patch.name, bundle.name))
120 except Exception, ex:
121 context.add_message("Couldn't add patch '%s' to bundle: %s" % \
122 (patch.name, ex.message))
124 elif action == 'remove':
126 bp = BundlePatch.objects.get(bundle = bundle, patch = patch)
128 context.add_message("Patch '%s' removed from bundle %s\n" % \
129 (patch.name, bundle.name))
138 def set_patches(user, project, action, data, patches, context):
140 form = MultiplePatchForm(project = project, data = data)
143 project = Project.objects.get(id = data['project'])
145 errors = ['No such project']
146 return (errors, form)
150 # this may be a bundle action, which doesn't modify a patch. in this
151 # case, don't require a valid form, or patch editing permissions
152 if action in bundle_actions:
153 errors = set_bundle(user, project, action, data, patches, context)
154 return (errors, form)
156 if not form.is_valid():
157 errors = ['The submitted form data was invalid']
158 return (errors, form)
160 for patch in patches:
161 if not patch.is_editable(user):
162 errors.append('You don\'t have permissions to edit the ' + \
167 if action == 'update':
171 elif action == 'ack':
174 elif action == 'archive':
175 patch.archived = True
179 elif action == 'unarchive':
180 patch.archived = True
184 elif action == 'delete':
190 if len(patches) == 1:
193 str = 'patches ' + str
194 context.add_message(str)
196 return (errors, form)
198 def userprofile_register_callback(user):
199 profile = UserProfile(user = user)