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, 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, \
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
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)
44 createbundleform = None
47 form = PatchForm(instance = patch)
48 if request.user.is_authenticated():
49 createbundleform = CreateBundleForm()
51 if request.method == 'POST':
52 action = request.POST.get('action', None)
54 action = action.lower()
56 if action == 'createbundle':
57 bundle = Bundle(owner = request.user, project = patch.project)
58 createbundleform = CreateBundleForm(instance = bundle,
60 if createbundleform.is_valid():
61 createbundleform.save()
62 bundle.append_patch(patch)
64 createbundleform = CreateBundleForm()
65 context.add_message('Bundle %s created' % bundle.name)
67 elif action == 'addtobundle':
68 bundle = get_object_or_404(Bundle, id = \
69 request.POST.get('bundle_id'))
71 bundle.append_patch(patch)
73 context.add_message('Patch added to bundle "%s"' % bundle.name)
75 context.add_message("Couldn't add patch '%s' to bundle %s: %s" \
76 % (patch.name, bundle.name, ex.message))
78 # all other actions require edit privs
80 return HttpResponseForbidden()
83 form = PatchForm(data = request.POST, instance = patch)
86 context.add_message('Patch updated')
88 elif action == 'archive':
91 context.add_message('Patch archived')
93 elif action == 'unarchive':
94 patch.archived = False
96 context.add_message('Patch un-archived')
101 elif action == 'delete':
105 context['patch'] = patch
106 context['patchform'] = form
107 context['createbundleform'] = createbundleform
108 context['project'] = patch.project
110 return render_to_response('patchwork/patch.html', context)
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', '')
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', '')
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)
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
145 if request.method == 'POST':
146 action = request.POST.get('action', None)
148 action = action.lower()
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):
156 for patch_id in get_patch_ids(request.POST):
158 patch = Patch.objects.get(id = patch_id)
159 except Patch.DoesNotExist:
163 (errors, form) = set_patches(request.user, project, action, \
166 context['errors'] = errors
169 elif request.user.is_authenticated() and \
170 project in request.user.get_profile().maintainer_projects.all():
171 form = MultiplePatchForm(project)
173 patches = Patch.objects.filter(project=project).order_by(order)
174 patches = context.filters.apply(patches)
176 paginator = Paginator(request, patches)
179 'page': paginator.current_page,
185 return render_to_response('patchwork/list.html', context)