]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/utils.py
Order: always set Order.order
[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.models import Bundle, Project, BundlePatch
22 from django.shortcuts import get_object_or_404
23
24 def get_patch_ids(d, prefix = 'patch_id'):
25     ids = []
26
27     for (k, v) in d.items():
28         a = k.split(':')
29         if len(a) != 2:
30             continue
31         if a[0] != prefix:
32             continue
33         if not v:
34             continue
35         ids.append(a[1])
36
37     return ids
38
39 class Order(object):
40     order_map = {
41         'date':         'date',
42         'name':         'name',
43         'state':        'state__ordering',
44         'submitter':    'submitter__name',
45         'delegate':     'delegate__username',
46     }
47     default_order = ('date', True)
48
49     def __init__(self, str = None, editable = False):
50         self.reversed = False
51         self.editable = editable
52         (self.order, self.reversed) = self.default_order
53
54         if self.editable:
55             return
56
57         if str is None or str == '':
58             return
59
60         reversed = False
61         if str[0] == '-':
62             str = str[1:]
63             reversed = True
64
65         if str not in self.order_map.keys():
66             return
67
68         self.order = str
69         self.reversed = reversed
70
71     def __str__(self):
72         str = self.order
73         if self.reversed:
74             str = '-' + str
75         return str
76
77     def name(self):
78         return self.order
79
80     def reversed_name(self):
81         if self.reversed:
82             return self.order
83         else:
84             return '-' + self.order
85
86     def query(self):
87         q = self.order_map[self.order]
88         if self.reversed:
89             q = '-' + q
90         return q
91
92 bundle_actions = ['create', 'add', 'remove']
93 def set_bundle(user, project, action, data, patches, context):
94     # set up the bundle
95     bundle = None
96     if action == 'create':
97         bundle_name = data['bundle_name'].strip()
98         if not bundle_name:
99             return ['No bundle name was specified']
100
101         bundle = Bundle(owner = user, project = project,
102                 name = bundle_name)
103         bundle.save()
104         context.add_message("Bundle %s created" % bundle.name)
105
106     elif action =='add':
107         bundle = get_object_or_404(Bundle, id = data['bundle_id'])
108
109     elif action =='remove':
110         bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])
111
112     if not bundle:
113         return ['no such bundle']
114
115     for patch in patches:
116         if action == 'create' or action == 'add':
117             bundlepatch_count = BundlePatch.objects.filter(bundle = bundle,
118                         patch = patch).count()
119             if bundlepatch_count == 0:
120                 bundle.append_patch(patch)
121                 context.add_message("Patch '%s' added to bundle %s" % \
122                         (patch.name, bundle.name))
123             else:
124                 context.add_message("Patch '%s' already in bundle %s" % \
125                         (patch.name, bundle.name))
126
127         elif action == 'remove':
128             try:
129                 bp = BundlePatch.objects.get(bundle = bundle, patch = patch)
130                 bp.delete()
131                 context.add_message("Patch '%s' removed from bundle %s\n" % \
132                         (patch.name, bundle.name))
133             except Exception:
134                 pass
135
136     bundle.save()
137
138     return []