]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/patch.py
tox: Add tox.ini file
[patchwork] / apps / patchwork / views / patch.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
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, patch_to_mbox
27
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)
33
34     form = None
35     createbundleform = None
36
37     if editable:
38         form = PatchForm(instance = patch)
39     if request.user.is_authenticated():
40         createbundleform = CreateBundleForm()
41
42     if request.method == 'POST':
43         action = request.POST.get('action', None)
44         if action:
45             action = action.lower()
46
47         if action == 'createbundle':
48             bundle = Bundle(owner = request.user, project = patch.project)
49             createbundleform = CreateBundleForm(instance = bundle,
50                     data = request.POST)
51             if createbundleform.is_valid():
52                 createbundleform.save()
53                 bundle.append_patch(patch)
54                 bundle.save()
55                 createbundleform = CreateBundleForm()
56                 context.add_message('Bundle %s created' % bundle.name)
57
58         elif action == 'addtobundle':
59             bundle = get_object_or_404(Bundle, id = \
60                         request.POST.get('bundle_id'))
61             try:
62                 bundle.append_patch(patch)
63                 bundle.save()
64                 context.add_message('Patch added to bundle "%s"' % bundle.name)
65             except Exception, ex:
66                 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
67                         % (patch.name, bundle.name, ex.message))
68
69         # all other actions require edit privs
70         elif not editable:
71             return HttpResponseForbidden()
72
73         elif action is None:
74             form = PatchForm(data = request.POST, instance = patch)
75             if form.is_valid():
76                 form.save()
77                 context.add_message('Patch updated')
78
79     context['patch'] = patch
80     context['patchform'] = form
81     context['createbundleform'] = createbundleform
82     context['project'] = patch.project
83
84     return render_to_response('patchwork/patch.html', context)
85
86 def content(request, patch_id):
87     patch = get_object_or_404(Patch, id=patch_id)
88     response = HttpResponse(content_type="text/x-patch")
89     response.write(patch.content)
90     response['Content-Disposition'] = 'attachment; filename=' + \
91         patch.filename().replace(';', '').replace('\n', '')
92     return response
93
94 def mbox(request, patch_id):
95     patch = get_object_or_404(Patch, id=patch_id)
96     response = HttpResponse(content_type="text/plain")
97     response.write(patch_to_mbox(patch).as_string(True))
98     response['Content-Disposition'] = 'attachment; filename=' + \
99         patch.filename().replace(';', '').replace('\n', '')
100     return response
101
102
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)