]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/bundle.py
d8e4e2fc82ca65282bd78037cec9b5719d03a62f
[patchwork] / apps / patchwork / views / bundle.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 from django.contrib.auth.decorators import login_required
21 from django.contrib.auth.models import User
22 from django.shortcuts import render_to_response, get_object_or_404
23 from patchwork.requestcontext import PatchworkRequestContext
24 from django.http import HttpResponse, HttpResponseRedirect
25 import django.core.urlresolvers
26 from patchwork.models import Patch, Bundle, Project
27 from patchwork.utils import get_patch_ids
28 from patchwork.forms import BundleForm, DeleteBundleForm
29 from patchwork.views import generic_list
30 from patchwork.filters import DelegateFilter
31 from patchwork.paginator import Paginator
32
33 @login_required
34 def setbundle(request):
35     context = PatchworkRequestContext(request)
36
37     bundle = None
38
39     if request.method == 'POST':
40         action = request.POST.get('action', None)
41         if action is None:
42             pass
43         elif action == 'create':
44             project = get_object_or_404(Project,
45                     id = request.POST.get('project'))
46             bundle = Bundle(owner = request.user, project = project,
47                     name = request.POST['name'])
48             bundle.save()
49             patch_id = request.POST.get('patch_id', None)
50             if patch_id:
51                 patch = get_object_or_404(Patch, id = patch_id)
52                 bundle.patches.add(patch)
53             bundle.save()
54         elif action == 'add':
55             bundle = get_object_or_404(Bundle,
56                     owner = request.user, id = request.POST['id'])
57             bundle.save()
58
59             patch_id = request.get('patch_id', None)
60             if patch_id:
61                 patch_ids = patch_id
62             else:
63                 patch_ids = get_patch_ids(request.POST)
64
65             for id in patch_ids:
66                 try:
67                     patch = Patch.objects.get(id = id)
68                     bundle.patches.add(patch)
69                 except ex:
70                     pass
71
72             bundle.save()
73         elif action == 'delete':
74             try:
75                 bundle = Bundle.objects.get(owner = request.user,
76                         id = request.POST['id'])
77                 bundle.delete()
78             except Exception:
79                 pass
80
81             bundle = None
82
83     else:
84         bundle = get_object_or_404(Bundle, owner = request.user,
85                 id = request.POST['bundle_id'])
86
87     if 'error' in context:
88         pass
89
90     if bundle:
91         return HttpResponseRedirect(
92                 django.core.urlresolvers.reverse(
93                     'patchwork.views.bundle.bundle',
94                     kwargs = {'bundle_id': bundle.id}
95                     )
96                 )
97     else:
98         return HttpResponseRedirect(
99                 django.core.urlresolvers.reverse(
100                     'patchwork.views.bundle.list')
101                 )
102
103 @login_required
104 def bundles(request):
105     context = PatchworkRequestContext(request)
106
107     if request.method == 'POST':
108         form_name = request.POST.get('form_name', '')
109
110         if form_name == DeleteBundleForm.name:
111             form = DeleteBundleForm(request.POST)
112             if form.is_valid():
113                 bundle = get_object_or_404(Bundle,
114                                 id = form.cleaned_data['bundle_id'])
115                 bundle.delete()
116
117     bundles = Bundle.objects.filter(owner = request.user)
118     for bundle in bundles:
119         bundle.delete_form = DeleteBundleForm(auto_id = False,
120                                 initial = {'bundle_id': bundle.id})
121
122     context['bundles'] = bundles
123
124     return render_to_response('patchwork/bundles.html', context)
125
126 @login_required
127 def bundle(request, bundle_id):
128     bundle = get_object_or_404(Bundle, id = bundle_id)
129     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
130
131     if request.method == 'POST' and request.POST.get('form') == 'bundle':
132         action = request.POST.get('action', '').lower()
133         if action == 'delete':
134             bundle.delete()
135             return HttpResponseRedirect(
136                     django.core.urlresolvers.reverse(
137                         'patchwork.views.user.profile')
138                     )
139         elif action == 'update':
140             form = BundleForm(request.POST, instance = bundle)
141             if form.is_valid():
142                 form.save()
143     else:
144         form = BundleForm(instance = bundle)
145
146     context = generic_list(request, bundle.project,
147             'patchwork.views.bundle.bundle',
148             view_args = {'bundle_id': bundle_id},
149             filter_settings = filter_settings,
150             patches = bundle.patches.all())
151
152     context['bundle'] = bundle
153     context['bundleform'] = form
154
155     return render_to_response('patchwork/bundle.html', context)
156
157 @login_required
158 def mbox(request, bundle_id):
159     bundle = get_object_or_404(Bundle, id = bundle_id)
160     response = HttpResponse(mimetype='text/plain')
161     response['Content-Disposition'] = 'attachment; filename=bundle-%d.mbox' % \
162             bundle.id
163     response.write(bundle.mbox())
164     return response
165
166 def public(request, username, bundlename):
167     user = get_object_or_404(User, username = username)
168     bundle = get_object_or_404(Bundle, name = bundlename, public = True)
169     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
170     context = generic_list(request, bundle.project,
171             'patchwork.views.bundle.public',
172             view_args = {'username': username, 'bundlename': bundlename},
173             filter_settings = filter_settings,
174             patches = bundle.patches.all())
175
176     context['bundle'] = bundle
177
178     return render_to_response('patchwork/bundle-public.html', context)