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