1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
33 def setbundle(request):
34 context = PatchworkRequestContext(request)
38 if request.method == 'POST':
39 action = request.POST.get('action', None)
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'])
48 patch_id = request.POST.get('patch_id', None)
50 patch = get_object_or_404(Patch, id = patch_id)
52 bundle.append_patch(patch)
57 bundle = get_object_or_404(Bundle,
58 owner = request.user, id = request.POST['id'])
61 patch_id = request.get('patch_id', None)
65 patch_ids = get_patch_ids(request.POST)
69 patch = Patch.objects.get(id = id)
70 bundle.append_patch(patch)
75 elif action == 'delete':
77 bundle = Bundle.objects.get(owner = request.user,
78 id = request.POST['id'])
86 bundle = get_object_or_404(Bundle, owner = request.user,
87 id = request.POST['bundle_id'])
89 if 'error' in context:
93 return HttpResponseRedirect(
94 django.core.urlresolvers.reverse(
95 'patchwork.views.bundle.bundle',
96 kwargs = {'bundle_id': bundle.id}
100 return HttpResponseRedirect(
101 django.core.urlresolvers.reverse(
102 'patchwork.views.bundle.list')
106 def bundles(request):
107 context = PatchworkRequestContext(request)
109 if request.method == 'POST':
110 form_name = request.POST.get('form_name', '')
112 if form_name == DeleteBundleForm.name:
113 form = DeleteBundleForm(request.POST)
115 bundle = get_object_or_404(Bundle,
116 id = form.cleaned_data['bundle_id'])
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})
124 context['bundles'] = bundles
126 return render_to_response('patchwork/bundles.html', context)
128 def bundle(request, username, bundlename):
129 bundle = get_object_or_404(Bundle, owner__username = username,
131 filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
133 is_owner = request.user == bundle.owner
135 if not (is_owner or bundle.public):
136 return HttpResponseNotFound()
139 if request.method == 'POST' and request.POST.get('form') == 'bundle':
140 action = request.POST.get('action', '').lower()
141 if action == 'delete':
143 return HttpResponseRedirect(
144 django.core.urlresolvers.reverse(
145 'patchwork.views.user.profile')
147 elif action == 'update':
148 form = BundleForm(request.POST, instance = bundle)
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())
158 form = BundleForm(instance = bundle)
160 form = BundleForm(instance = bundle)
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
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
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)
184 context['bundle'] = bundle
185 context['bundleform'] = form
187 return render_to_response('patchwork/bundle.html', context)
189 def mbox(request, username, bundlename):
190 bundle = get_object_or_404(Bundle, owner__username = username,
193 if not (request.user == bundle.owner or bundle.public):
194 return HttpResponseNotFound()
196 mbox = '\n'.join([patch_to_mbox(p).as_string(True)
197 for p in bundle.ordered_patches()])
199 response = HttpResponse(content_type='text/plain')
200 response['Content-Disposition'] = \
201 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name)
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())
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,