]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/utils.py
views: Refactor generic_list() to make it less complicated
[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
53         if self.editable:
54             return
55
56         if str is None or str == '':
57             (self.order, self.reversed) = self.default_order
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             (self.order, self.reversed) = self.default_order
67             return
68
69         self.order = str
70         self.reversed = reversed
71
72     def __str__(self):
73         str = self.order
74         if self.reversed:
75             str = '-' + str
76         return str
77
78     def name(self):
79         return self.order
80
81     def reversed_name(self):
82         if self.reversed:
83             return self.order
84         else:
85             return '-' + self.order
86
87     def query(self):
88         q = self.order_map[self.order]
89         if self.reversed:
90             q = '-' + q
91         return q
92
93 bundle_actions = ['create', 'add', 'remove']
94 def set_bundle(user, project, action, data, patches, context):
95     # set up the bundle
96     bundle = None
97     if action == 'create':
98         bundle_name = data['bundle_name'].strip()
99         if not bundle_name:
100             return ['No bundle name was specified']
101
102         bundle = Bundle(owner = user, project = project,
103                 name = bundle_name)
104         bundle.save()
105         context.add_message("Bundle %s created" % bundle.name)
106
107     elif action =='add':
108         bundle = get_object_or_404(Bundle, id = data['bundle_id'])
109
110     elif action =='remove':
111         bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])
112
113     if not bundle:
114         return ['no such bundle']
115
116     for patch in patches:
117         if action == 'create' or action == 'add':
118             bundlepatch_count = BundlePatch.objects.filter(bundle = bundle,
119                         patch = patch).count()
120             if bundlepatch_count == 0:
121                 bundle.append_patch(patch)
122                 context.add_message("Patch '%s' added to bundle %s" % \
123                         (patch.name, bundle.name))
124             else:
125                 context.add_message("Patch '%s' already in bundle %s" % \
126                         (patch.name, bundle.name))
127
128         elif action == 'remove':
129             try:
130                 bp = BundlePatch.objects.get(bundle = bundle, patch = patch)
131                 bp.delete()
132                 context.add_message("Patch '%s' removed from bundle %s\n" % \
133                         (patch.name, bundle.name))
134             except Exception:
135                 pass
136
137     bundle.save()
138
139     return []