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)
35 createbundleform = None
38 form = PatchForm(instance = patch)
39 if request.user.is_authenticated():
40 createbundleform = CreateBundleForm()
42 if request.method == 'POST':
43 action = request.POST.get('action', None)
45 action = action.lower()
47 if action == 'createbundle':
48 bundle = Bundle(owner = request.user, project = patch.project)
49 createbundleform = CreateBundleForm(instance = bundle,
51 if createbundleform.is_valid():
52 createbundleform.save()
53 bundle.append_patch(patch)
55 createbundleform = CreateBundleForm()
56 context.add_message('Bundle %s created' % bundle.name)
58 elif action == 'addtobundle':
59 bundle = get_object_or_404(Bundle, id = \
60 request.POST.get('bundle_id'))
62 bundle.append_patch(patch)
64 context.add_message('Patch added to bundle "%s"' % bundle.name)
66 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
67 % (patch.name, bundle.name, ex.message))
69 # all other actions require edit privs
71 return HttpResponseForbidden()
74 form = PatchForm(data = request.POST, instance = patch)
77 context.add_message('Patch updated')
79 context['patch'] = patch
80 context['patchform'] = form
81 context['createbundleform'] = createbundleform
82 context['project'] = patch.project
84 return render_to_response('patchwork/patch.html', context)
86 def content(request, patch_id):
87 patch = get_object_or_404(Patch, id=patch_id)
88 response = HttpResponse(mimetype="text/x-patch")
89 response.write(patch.content)
90 response['Content-Disposition'] = 'attachment; filename=' + \
91 patch.filename().replace(';', '').replace('\n', '')
94 def mbox(request, patch_id):
95 patch = get_object_or_404(Patch, id=patch_id)
96 response = HttpResponse(mimetype="text/plain")
97 response.write(patch.mbox().as_string(True))
98 response['Content-Disposition'] = 'attachment; filename=' + \
99 patch.filename().replace(';', '').replace('\n', '')
103 def list(request, project_id):
104 project = get_object_or_404(Project, linkname=project_id)
105 context = generic_list(request, project, 'patchwork.views.patch.list',
106 view_args = {'project_id': project.linkname})
107 return render_to_response('patchwork/list.html', context)