]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/utils.py
[views] Change default ordering to latest-first
[patchwork] / apps / patchwork / utils.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 patchwork.forms import MultiplePatchForm
22 from patchwork.models import Bundle, Project, State, UserProfile
23 from django.conf import settings
24 from django.shortcuts import render_to_response, get_object_or_404
25
26 def get_patch_ids(d, prefix = 'patch_id'):
27     ids = []
28
29     for (k, v) in d.items():
30         a = k.split(':')
31         if len(a) != 2:
32             continue
33         if a[0] != prefix:
34             continue
35         if not v:
36             continue
37         ids.append(a[1])
38
39     return ids
40
41 class Order(object):
42     order_map = {
43         'date':         'date',
44         'name':         'name',
45         'state':        'state__ordering',
46         'submitter':    'submitter__name',
47         'delegate':     'delegate__username',
48     }
49     default_order = ('date', True)
50
51     def __init__(self, str = None):
52         self.reversed = False
53
54         if str is None or str == '':
55             (self.order, self.reversed) = self.default_order
56             return
57
58         reversed = False
59         if str[0] == '-':
60             str = str[1:]
61             reversed = True
62
63         if str not in self.order_map.keys():
64             (self.order, self.reversed) = self.default_order
65             return
66
67         self.order = str
68         self.reversed = reversed
69
70     def __str__(self):
71         str = self.order
72         if self.reversed:
73             str = '-' + str
74         return str
75
76     def name(self):
77         return self.order
78
79     def reversed_name(self):
80         if self.reversed:
81             return self.order
82         else:
83             return '-' + self.order
84
85     def query(self):
86         q = self.order_map[self.order]
87         if self.reversed:
88             q = '-' + q
89         return q
90
91 bundle_actions = ['create', 'add', 'remove']
92 def set_bundle(user, project, action, data, patches, context):
93     # set up the bundle
94     bundle = None
95     if action == 'create':
96         bundle = Bundle(owner = user, project = project,
97                 name = data['bundle_name'])
98         bundle.save()
99         str = 'added to new bundle "%s"' % bundle.name
100         auth_required = False
101
102     elif action =='add':
103         bundle = get_object_or_404(Bundle, id = data['bundle_id'])
104         str = 'added to bundle "%s"' % bundle.name
105         auth_required = False
106
107     elif action =='remove':
108         bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])
109         str = 'removed from bundle "%s"' % bundle.name
110         auth_required = False
111
112     if not bundle:
113         return ['no such bundle']
114
115     for patch in patches:
116         if action == 'create' or action == 'add':
117             bundle.patches.add(patch)
118
119         elif action == 'remove':
120             bundle.patches.remove(patch)
121
122     if len(patches) > 0:
123         if len(patches) == 1:
124             str = 'patch ' + str
125         else:
126             str = 'patches ' + str
127         context.add_message(str)
128
129     bundle.save()
130
131     return []
132
133
134 def set_patches(user, project, action, data, patches, context):
135     errors = []
136     form = MultiplePatchForm(project = project, data = data)
137
138     try:
139         project = Project.objects.get(id = data['project'])
140     except:
141         errors = ['No such project']
142         return (errors, form)
143
144     str = ''
145
146     # this may be a bundle action, which doesn't modify a patch. in this
147     # case, don't require a valid form, or patch editing permissions
148     if action in bundle_actions:
149         errors = set_bundle(user, project, action, data, patches, context)
150         return (errors, form)
151
152     if not form.is_valid():
153         errors = ['The submitted form data was invalid']
154         return (errors, form)
155
156     for patch in patches:
157         if not patch.is_editable(user):
158             errors.append('You don\'t have permissions to edit the ' + \
159                     'patch "%s"' \
160                     % patch.name)
161             continue
162
163         if action == 'update':
164             form.save(patch)
165             str = 'updated'
166
167         elif action == 'ack':
168             pass
169
170         elif action == 'archive':
171             patch.archived = True
172             patch.save()
173             str = 'archived'
174
175         elif action == 'unarchive':
176             patch.archived = True
177             patch.save()
178             str = 'un-archived'
179
180         elif action == 'delete':
181             patch.delete()
182             str = 'un-archived'
183
184
185     if len(patches) > 0:
186         if len(patches) == 1:
187             str = 'patch ' + str
188         else:
189             str = 'patches ' + str
190         context.add_message(str)
191
192     return (errors, form)
193
194 def userprofile_register_callback(user):
195     profile = UserProfile(user = user)
196     profile.save()
197