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
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
34 def setbundle(request):
35 context = PatchworkRequestContext(request)
39 if request.method == 'POST':
40 action = request.POST.get('action', None)
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'])
49 patch_id = request.POST.get('patch_id', None)
51 patch = get_object_or_404(Patch, id = patch_id)
53 bundle.append_patch(patch)
58 bundle = get_object_or_404(Bundle,
59 owner = request.user, id = request.POST['id'])
62 patch_id = request.get('patch_id', None)
66 patch_ids = get_patch_ids(request.POST)
70 patch = Patch.objects.get(id = id)
71 bundle.append_patch(patch)
76 elif action == 'delete':
78 bundle = Bundle.objects.get(owner = request.user,
79 id = request.POST['id'])
87 bundle = get_object_or_404(Bundle, owner = request.user,
88 id = request.POST['bundle_id'])
90 if 'error' in context:
94 return HttpResponseRedirect(
95 django.core.urlresolvers.reverse(
96 'patchwork.views.bundle.bundle',
97 kwargs = {'bundle_id': bundle.id}
101 return HttpResponseRedirect(
102 django.core.urlresolvers.reverse(
103 'patchwork.views.bundle.list')
107 def bundles(request):
108 context = PatchworkRequestContext(request)
110 if request.method == 'POST':
111 form_name = request.POST.get('form_name', '')
113 if form_name == DeleteBundleForm.name:
114 form = DeleteBundleForm(request.POST)
116 bundle = get_object_or_404(Bundle,
117 id = form.cleaned_data['bundle_id'])
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})
125 context['bundles'] = bundles
127 return render_to_response('patchwork/bundles.html', context)
130 def bundle(request, bundle_id):
131 bundle = get_object_or_404(Bundle, id = bundle_id)
132 filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
134 if request.method == 'POST' and request.POST.get('form') == 'bundle':
135 action = request.POST.get('action', '').lower()
136 if action == 'delete':
138 return HttpResponseRedirect(
139 django.core.urlresolvers.reverse(
140 'patchwork.views.user.profile')
142 elif action == 'update':
143 form = BundleForm(request.POST, instance = bundle)
147 form = BundleForm(instance = bundle)
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
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
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)
167 context['bundle'] = bundle
168 context['bundleform'] = form
170 return render_to_response('patchwork/bundle.html', context)
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' % \
178 response.write(bundle.mbox())
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())
191 context['bundle'] = bundle
193 return render_to_response('patchwork/bundle-public.html', context)