summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/utils.py
blob: 5a8e4c0a4746f700bd201f316db32b0e7519d371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


from patchwork.models import Bundle, Project, BundlePatch
from django.shortcuts import get_object_or_404

def get_patch_ids(d, prefix = 'patch_id'):
    ids = []

    for (k, v) in d.items():
        a = k.split(':')
        if len(a) != 2:
            continue
        if a[0] != prefix:
            continue
        if not v:
            continue
        ids.append(a[1])

    return ids

class Order(object):
    order_map = {
        'date':         'date',
        'name':         'name',
        'state':        'state__ordering',
        'submitter':    'submitter__name',
        'delegate':     'delegate__username',
    }
    default_order = ('date', True)

    def __init__(self, str = None, editable = False):
        self.reversed = False
        self.editable = editable
        (self.order, self.reversed) = self.default_order

        if self.editable:
            return

        if str is None or str == '':
            return

        reversed = False
        if str[0] == '-':
            str = str[1:]
            reversed = True

        if str not in self.order_map.keys():
            return

        self.order = str
        self.reversed = reversed

    def __str__(self):
        str = self.order
        if self.reversed:
            str = '-' + str
        return str

    def name(self):
        return self.order

    def reversed_name(self):
        if self.reversed:
            return self.order
        else:
            return '-' + self.order

    def query(self):
        q = self.order_map[self.order]
        if self.reversed:
            q = '-' + q
        return q

bundle_actions = ['create', 'add', 'remove']
def set_bundle(user, project, action, data, patches, context):
    # set up the bundle
    bundle = None
    if action == 'create':
        bundle_name = data['bundle_name'].strip()
        if not bundle_name:
            return ['No bundle name was specified']

        bundle = Bundle(owner = user, project = project,
                name = bundle_name)
        bundle.save()
        context.add_message("Bundle %s created" % bundle.name)

    elif action =='add':
        bundle = get_object_or_404(Bundle, id = data['bundle_id'])

    elif action =='remove':
        bundle = get_object_or_404(Bundle, id = data['removed_bundle_id'])

    if not bundle:
        return ['no such bundle']

    for patch in patches:
        if action == 'create' or action == 'add':
            bundlepatch_count = BundlePatch.objects.filter(bundle = bundle,
                        patch = patch).count()
            if bundlepatch_count == 0:
                bundle.append_patch(patch)
                context.add_message("Patch '%s' added to bundle %s" % \
                        (patch.name, bundle.name))
            else:
                context.add_message("Patch '%s' already in bundle %s" % \
                        (patch.name, bundle.name))

        elif action == 'remove':
            try:
                bp = BundlePatch.objects.get(bundle = bundle, patch = patch)
                bp.delete()
                context.add_message("Patch '%s' removed from bundle %s\n" % \
                        (patch.name, bundle.name))
            except Exception:
                pass

    bundle.save()

    return []