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
21 from patchwork.models import Patch, Project, Bundle
22 from patchwork.forms import PatchForm, CreateBundleForm
23 from patchwork.requestcontext import PatchworkRequestContext
24 from django.shortcuts import render_to_response, get_object_or_404
25 from django.http import HttpResponse, HttpResponseForbidden
26 from patchwork.views import generic_list
28 def patch(request, patch_id):
29 context = PatchworkRequestContext(request)
30 patch = get_object_or_404(Patch, id=patch_id)
31 context.project = patch.project
32 editable = patch.is_editable(request.user)
36 createbundleform = None
39 form = PatchForm(instance = patch)
40 if request.user.is_authenticated():
41 createbundleform = CreateBundleForm()
43 if request.method == 'POST':
44 action = request.POST.get('action', None)
46 action = action.lower()
48 if action == 'createbundle':
49 bundle = Bundle(owner = request.user, project = patch.project)
50 createbundleform = CreateBundleForm(instance = bundle,
52 if createbundleform.is_valid():
53 createbundleform.save()
54 bundle.append_patch(patch)
56 createbundleform = CreateBundleForm()
57 context.add_message('Bundle %s created' % bundle.name)
59 elif action == 'addtobundle':
60 bundle = get_object_or_404(Bundle, id = \
61 request.POST.get('bundle_id'))
63 bundle.append_patch(patch)
65 context.add_message('Patch added to bundle "%s"' % bundle.name)
67 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
68 % (patch.name, bundle.name, ex.message))
70 # all other actions require edit privs
72 return HttpResponseForbidden()
75 form = PatchForm(data = request.POST, instance = patch)
78 context.add_message('Patch updated')
80 context['patch'] = patch
81 context['patchform'] = form
82 context['createbundleform'] = createbundleform
83 context['project'] = patch.project
85 return render_to_response('patchwork/patch.html', context)
87 def content(request, patch_id):
88 patch = get_object_or_404(Patch, id=patch_id)
89 response = HttpResponse(mimetype="text/x-patch")
90 response.write(patch.content)
91 response['Content-Disposition'] = 'attachment; filename=' + \
92 patch.filename().replace(';', '').replace('\n', '')
95 def mbox(request, patch_id):
96 patch = get_object_or_404(Patch, id=patch_id)
97 response = HttpResponse(mimetype="text/plain")
98 response.write(patch.mbox().as_string(True))
99 response['Content-Disposition'] = 'attachment; filename=' + \
100 patch.filename().replace(';', '').replace('\n', '')
104 def list(request, project_id):
105 project = get_object_or_404(Project, linkname=project_id)
106 context = generic_list(request, project, 'patchwork.views.patch.list',
107 view_args = {'project_id': project.linkname})
108 return render_to_response('patchwork/list.html', context)