]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/patch.py
Bundle reordering support
[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         elif action == 'archive':
89             patch.archived = True
90             patch.save()
91             context.add_message('Patch archived')
92
93         elif action == 'unarchive':
94             patch.archived = False
95             patch.save()
96             context.add_message('Patch un-archived')
97
98         elif action == 'ack':
99             pass
100
101         elif action == 'delete':
102             patch.delete()
103
104
105     context['patch'] = patch
106     context['patchform'] = form
107     context['createbundleform'] = createbundleform
108     context['project'] = patch.project
109
110     return render_to_response('patchwork/patch.html', context)
111
112 def content(request, patch_id):
113     patch = get_object_or_404(Patch, id=patch_id)
114     response = HttpResponse(mimetype="text/x-patch")
115     response.write(patch.content)
116     response['Content-Disposition'] = 'attachment; filename=' + \
117         patch.filename().replace(';', '').replace('\n', '')
118     return response
119
120 def mbox(request, patch_id):
121     patch = get_object_or_404(Patch, id=patch_id)
122     response = HttpResponse(mimetype="text/plain")
123     response.write(patch.mbox().as_string(True))
124     response['Content-Disposition'] = 'attachment; filename=' + \
125         patch.filename().replace(';', '').replace('\n', '')
126     return response
127
128
129 def list(request, project_id):
130     project = get_object_or_404(Project, linkname=project_id)
131     context = generic_list(request, project, 'patchwork.views.patch.list',
132             view_args = {'project_id': project.linkname})
133     return render_to_response('patchwork/list.html', context)
134
135     context = PatchworkRequestContext(request,
136             list_view = 'patchwork.views.patch.list',
137             list_view_params = {'project_id': project_id})
138     order = get_order(request)
139     project = get_object_or_404(Project, linkname=project_id)
140     context.project = project
141
142     form = None
143     errors = []
144
145     if request.method == 'POST':
146         action = request.POST.get('action', None)
147         if action:
148             action = action.lower()
149
150         # special case: the user may have hit enter in the 'create bundle'
151         # text field, so if non-empty, assume the create action:
152         if request.POST.get('bundle_name', False):
153             action = 'create'
154
155         ps = []
156         for patch_id in get_patch_ids(request.POST):
157             try:
158                 patch = Patch.objects.get(id = patch_id)
159             except Patch.DoesNotExist:
160                 pass
161             ps.append(patch)
162
163         (errors, form) = set_patches(request.user, project, action, \
164                                         request.POST, ps)
165         if errors:
166             context['errors'] = errors
167
168
169     elif request.user.is_authenticated() and \
170             project in request.user.get_profile().maintainer_projects.all():
171         form = MultiplePatchForm(project)
172
173     patches = Patch.objects.filter(project=project).order_by(order)
174     patches = context.filters.apply(patches)
175
176     paginator = Paginator(request, patches)
177
178     context.update({
179             'page':             paginator.current_page,
180             'patchform':        form,
181             'project':          project,
182             'errors':           errors,
183             })
184
185     return render_to_response('patchwork/list.html', context)