]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/bundle.py
Bundle reordering support
[patchwork] / apps / patchwork / views / bundle.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 from django.contrib.auth.decorators import login_required
21 from django.contrib.auth.models import User
22 from django.shortcuts import render_to_response, get_object_or_404
23 from patchwork.requestcontext import PatchworkRequestContext
24 from django.http import HttpResponse, HttpResponseRedirect
25 import django.core.urlresolvers
26 from patchwork.models import Patch, Bundle, BundlePatch, Project
27 from patchwork.utils import get_patch_ids
28 from patchwork.forms import BundleForm, DeleteBundleForm
29 from patchwork.views import generic_list
30 from patchwork.filters import DelegateFilter
31 from patchwork.paginator import Paginator
32
33 @login_required
34 def setbundle(request):
35     context = PatchworkRequestContext(request)
36
37     bundle = None
38
39     if request.method == 'POST':
40         action = request.POST.get('action', None)
41         if action is None:
42             pass
43         elif action == 'create':
44             project = get_object_or_404(Project,
45                     id = request.POST.get('project'))
46             bundle = Bundle(owner = request.user, project = project,
47                     name = request.POST['name'])
48             bundle.save()
49             patch_id = request.POST.get('patch_id', None)
50             if patch_id:
51                 patch = get_object_or_404(Patch, id = patch_id)
52                 try:
53                     bundle.append_patch(patch)
54                 except Exception:
55                     pass
56             bundle.save()
57         elif action == 'add':
58             bundle = get_object_or_404(Bundle,
59                     owner = request.user, id = request.POST['id'])
60             bundle.save()
61
62             patch_id = request.get('patch_id', None)
63             if patch_id:
64                 patch_ids = patch_id
65             else:
66                 patch_ids = get_patch_ids(request.POST)
67
68             for id in patch_ids:
69                 try:
70                     patch = Patch.objects.get(id = id)
71                     bundle.append_patch(patch)
72                 except ex:
73                     pass
74
75             bundle.save()
76         elif action == 'delete':
77             try:
78                 bundle = Bundle.objects.get(owner = request.user,
79                         id = request.POST['id'])
80                 bundle.delete()
81             except Exception:
82                 pass
83
84             bundle = None
85
86     else:
87         bundle = get_object_or_404(Bundle, owner = request.user,
88                 id = request.POST['bundle_id'])
89
90     if 'error' in context:
91         pass
92
93     if bundle:
94         return HttpResponseRedirect(
95                 django.core.urlresolvers.reverse(
96                     'patchwork.views.bundle.bundle',
97                     kwargs = {'bundle_id': bundle.id}
98                     )
99                 )
100     else:
101         return HttpResponseRedirect(
102                 django.core.urlresolvers.reverse(
103                     'patchwork.views.bundle.list')
104                 )
105
106 @login_required
107 def bundles(request):
108     context = PatchworkRequestContext(request)
109
110     if request.method == 'POST':
111         form_name = request.POST.get('form_name', '')
112
113         if form_name == DeleteBundleForm.name:
114             form = DeleteBundleForm(request.POST)
115             if form.is_valid():
116                 bundle = get_object_or_404(Bundle,
117                                 id = form.cleaned_data['bundle_id'])
118                 bundle.delete()
119
120     bundles = Bundle.objects.filter(owner = request.user)
121     for bundle in bundles:
122         bundle.delete_form = DeleteBundleForm(auto_id = False,
123                                 initial = {'bundle_id': bundle.id})
124
125     context['bundles'] = bundles
126
127     return render_to_response('patchwork/bundles.html', context)
128
129 @login_required
130 def bundle(request, bundle_id):
131     bundle = get_object_or_404(Bundle, id = bundle_id)
132     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
133
134     if request.method == 'POST' and request.POST.get('form') == 'bundle':
135         action = request.POST.get('action', '').lower()
136         if action == 'delete':
137             bundle.delete()
138             return HttpResponseRedirect(
139                     django.core.urlresolvers.reverse(
140                         'patchwork.views.user.profile')
141                     )
142         elif action == 'update':
143             form = BundleForm(request.POST, instance = bundle)
144             if form.is_valid():
145                 form.save()
146     else:
147         form = BundleForm(instance = bundle)
148
149     if request.method == 'POST' and request.POST.get('form') == 'reorderform':
150         order = get_object_or_404(BundlePatch, bundle = bundle,
151                         patch__id = request.POST.get('order_start')).order
152
153         for patch_id in request.POST.getlist('neworder'):
154             bundlepatch = get_object_or_404(BundlePatch,
155                         bundle = bundle, patch__id = patch_id)
156             bundlepatch.order = order
157             bundlepatch.save()
158             order += 1
159
160     context = generic_list(request, bundle.project,
161             'patchwork.views.bundle.bundle',
162             view_args = {'bundle_id': bundle_id},
163             filter_settings = filter_settings,
164             patches = bundle.ordered_patches(),
165             editable_order = True)
166
167     context['bundle'] = bundle
168     context['bundleform'] = form
169
170     return render_to_response('patchwork/bundle.html', context)
171
172 @login_required
173 def mbox(request, bundle_id):
174     bundle = get_object_or_404(Bundle, id = bundle_id)
175     response = HttpResponse(mimetype='text/plain')
176     response['Content-Disposition'] = 'attachment; filename=bundle-%d.mbox' % \
177             bundle.id
178     response.write(bundle.mbox())
179     return response
180
181 def public(request, username, bundlename):
182     user = get_object_or_404(User, username = username)
183     bundle = get_object_or_404(Bundle, name = bundlename, public = True)
184     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
185     context = generic_list(request, bundle.project,
186             'patchwork.views.bundle.public',
187             view_args = {'username': username, 'bundlename': bundlename},
188             filter_settings = filter_settings,
189             patches = bundle.patches.all())
190
191     context['bundle'] = bundle
192
193     return render_to_response('patchwork/bundle-public.html', context)