]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/patch.py
Remove unused imports
[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
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     messages = []
34
35     form = None
36     createbundleform = None
37
38     if editable:
39         form = PatchForm(instance = patch)
40     if request.user.is_authenticated():
41         createbundleform = CreateBundleForm()
42
43     if request.method == 'POST':
44         action = request.POST.get('action', None)
45         if action:
46             action = action.lower()
47
48         if action == 'createbundle':
49             bundle = Bundle(owner = request.user, project = patch.project)
50             createbundleform = CreateBundleForm(instance = bundle,
51                     data = request.POST)
52             if createbundleform.is_valid():
53                 createbundleform.save()
54                 bundle.append_patch(patch)
55                 bundle.save()
56                 createbundleform = CreateBundleForm()
57                 context.add_message('Bundle %s created' % bundle.name)
58
59         elif action == 'addtobundle':
60             bundle = get_object_or_404(Bundle, id = \
61                         request.POST.get('bundle_id'))
62             try:
63                 bundle.append_patch(patch)
64                 bundle.save()
65                 context.add_message('Patch added to bundle "%s"' % bundle.name)
66             except Exception, ex:
67                 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
68                         % (patch.name, bundle.name, ex.message))
69
70         # all other actions require edit privs
71         elif not editable:
72             return HttpResponseForbidden()
73
74         elif action is None:
75             form = PatchForm(data = request.POST, instance = patch)
76             if form.is_valid():
77                 form.save()
78                 context.add_message('Patch updated')
79
80     context['patch'] = patch
81     context['patchform'] = form
82     context['createbundleform'] = createbundleform
83     context['project'] = patch.project
84
85     return render_to_response('patchwork/patch.html', context)
86
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', '')
93     return response
94
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', '')
101     return response
102
103
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)