]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/patch.py
Remove unused templates and some code for processing them
[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, Person, Bundle
22 from patchwork.filters import Filters
23 from patchwork.forms import PatchForm, MultiplePatchForm, CreateBundleForm
24 from patchwork.utils import get_patch_ids, set_patches, Order
25 from patchwork.requestcontext import PatchworkRequestContext
26 from django.shortcuts import render_to_response, get_object_or_404
27 from django.http import HttpResponse, HttpResponseRedirect, \
28         HttpResponseForbidden
29 from django.contrib.auth.models import User
30 from django.contrib.auth.decorators import login_required
31 from django.contrib.auth import authenticate, login
32 import django.core.urlresolvers
33 from patchwork.paginator import Paginator
34 from patchwork.views import generic_list
35
36 def patch(request, patch_id):
37     context = PatchworkRequestContext(request)
38     patch = get_object_or_404(Patch, id=patch_id)
39     context.project = patch.project
40     editable = patch.is_editable(request.user)
41     messages = []
42
43     form = None
44     createbundleform = None
45
46     if editable:
47         form = PatchForm(instance = patch)
48     if request.user.is_authenticated():
49         createbundleform = CreateBundleForm()
50
51     if request.method == 'POST':
52         action = request.POST.get('action', None)
53         if action:
54             action = action.lower()
55
56         if action == 'createbundle':
57             bundle = Bundle(owner = request.user, project = patch.project)
58             createbundleform = CreateBundleForm(instance = bundle,
59                     data = request.POST)
60             if createbundleform.is_valid():
61                 createbundleform.save()
62                 bundle.append_patch(patch)
63                 bundle.save()
64                 createbundleform = CreateBundleForm()
65                 context.add_message('Bundle %s created' % bundle.name)
66
67         elif action == 'addtobundle':
68             bundle = get_object_or_404(Bundle, id = \
69                         request.POST.get('bundle_id'))
70             try:
71                 bundle.append_patch(patch)
72                 bundle.save()
73                 context.add_message('Patch added to bundle "%s"' % bundle.name)
74             except Exception, ex:
75                 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
76                         % (patch.name, bundle.name, ex.message))
77
78         # all other actions require edit privs
79         elif not editable:
80             return HttpResponseForbidden()
81
82         elif action is None:
83             form = PatchForm(data = request.POST, instance = patch)
84             if form.is_valid():
85                 form.save()
86                 context.add_message('Patch updated')
87
88     context['patch'] = patch
89     context['patchform'] = form
90     context['createbundleform'] = createbundleform
91     context['project'] = patch.project
92
93     return render_to_response('patchwork/patch.html', context)
94
95 def content(request, patch_id):
96     patch = get_object_or_404(Patch, id=patch_id)
97     response = HttpResponse(mimetype="text/x-patch")
98     response.write(patch.content)
99     response['Content-Disposition'] = 'attachment; filename=' + \
100         patch.filename().replace(';', '').replace('\n', '')
101     return response
102
103 def mbox(request, patch_id):
104     patch = get_object_or_404(Patch, id=patch_id)
105     response = HttpResponse(mimetype="text/plain")
106     response.write(patch.mbox().as_string(True))
107     response['Content-Disposition'] = 'attachment; filename=' + \
108         patch.filename().replace(';', '').replace('\n', '')
109     return response
110
111
112 def list(request, project_id):
113     project = get_object_or_404(Project, linkname=project_id)
114     context = generic_list(request, project, 'patchwork.views.patch.list',
115             view_args = {'project_id': project.linkname})
116     return render_to_response('patchwork/list.html', context)