]> git.ozlabs.org Git - patchwork/blob - patchwork/views/bundle.py
Move to a more recent django project structure
[patchwork] / 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, HttpResponseNotFound
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, patch_to_mbox
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 def bundle(request, username, bundlename):
129     bundle = get_object_or_404(Bundle, owner__username = username,
130                                 name = bundlename)
131     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
132
133     is_owner = request.user == bundle.owner
134
135     if not (is_owner or bundle.public):
136         return HttpResponseNotFound()
137
138     if is_owner:
139         if request.method == 'POST' and request.POST.get('form') == 'bundle':
140             action = request.POST.get('action', '').lower()
141             if action == 'delete':
142                 bundle.delete()
143                 return HttpResponseRedirect(
144                         django.core.urlresolvers.reverse(
145                             'patchwork.views.user.profile')
146                         )
147             elif action == 'update':
148                 form = BundleForm(request.POST, instance = bundle)
149                 if form.is_valid():
150                     form.save()
151
152                 # if we've changed the bundle name, redirect to new URL
153                 bundle = Bundle.objects.get(pk = bundle.pk)
154                 if bundle.name != bundlename:
155                     return HttpResponseRedirect(bundle.get_absolute_url())
156
157             else:
158                 form = BundleForm(instance = bundle)
159         else:
160             form = BundleForm(instance = bundle)
161
162         if request.method == 'POST' and \
163                            request.POST.get('form') == 'reorderform':
164             order = get_object_or_404(BundlePatch, bundle = bundle,
165                             patch__id = request.POST.get('order_start')).order
166
167             for patch_id in request.POST.getlist('neworder'):
168                 bundlepatch = get_object_or_404(BundlePatch,
169                             bundle = bundle, patch__id = patch_id)
170                 bundlepatch.order = order
171                 bundlepatch.save()
172                 order += 1
173     else:
174         form = None
175
176     context = generic_list(request, bundle.project,
177             'patchwork.views.bundle.bundle',
178             view_args = {'username': bundle.owner.username,
179                          'bundlename': bundle.name},
180             filter_settings = filter_settings,
181             patches = bundle.ordered_patches(),
182             editable_order = is_owner)
183
184     context['bundle'] = bundle
185     context['bundleform'] = form
186
187     return render_to_response('patchwork/bundle.html', context)
188
189 def mbox(request, username, bundlename):
190     bundle = get_object_or_404(Bundle, owner__username = username,
191                                 name = bundlename)
192
193     if not (request.user == bundle.owner or bundle.public):
194         return HttpResponseNotFound()
195
196     mbox = '\n'.join([patch_to_mbox(p).as_string(True)
197                         for p in bundle.ordered_patches()])
198
199     response = HttpResponse(content_type='text/plain')
200     response['Content-Disposition'] = \
201         'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name)
202
203     response.write(mbox)
204     return response
205
206 @login_required
207 def bundle_redir(request, bundle_id):
208     bundle = get_object_or_404(Bundle, id = bundle_id, owner = request.user)
209     return HttpResponseRedirect(bundle.get_absolute_url())
210
211 @login_required
212 def mbox_redir(request, bundle_id):
213     bundle = get_object_or_404(Bundle, id = bundle_id, owner = request.user)
214     return HttpResponseRedirect(django.core.urlresolvers.reverse(
215                                 'patchwork.views.bundle.mbox', kwargs = {
216                                     'username': request.user.username,
217                                     'bundlename': bundle.name,
218                                 }))
219
220
221