Inital commit
[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
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 bundle(request, bundle_id):
105     bundle = get_object_or_404(Bundle, id = bundle_id)
106     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
107
108     if request.method == 'POST' and request.POST.get('form') == 'bundle':
109         action = request.POST.get('action', '').lower()
110         if action == 'delete':
111             bundle.delete()
112             return HttpResponseRedirect(
113                     django.core.urlresolvers.reverse(
114                         'patchwork.views.user.profile')
115                     )
116         elif action == 'update':
117             form = BundleForm(request.POST, instance = bundle)
118             if form.is_valid():
119                 form.save()
120     else:
121         form = BundleForm(instance = bundle)
122
123     context = generic_list(request, bundle.project,
124             'patchwork.views.bundle.bundle',
125             view_args = {'bundle_id': bundle_id},
126             filter_settings = filter_settings,
127             patches = bundle.patches.all())
128
129     context['bundle'] = bundle
130     context['bundleform'] = form
131
132     return render_to_response('patchwork/bundle.html', context)
133
134 @login_required
135 def mbox(request, bundle_id):
136     bundle = get_object_or_404(Bundle, id = bundle_id)
137     response = HttpResponse(mimetype='text/plain')
138     response.write(bundle.mbox())
139     return response
140
141 def public(request, username, bundlename):
142     user = get_object_or_404(User, username = username)
143     bundle = get_object_or_404(Bundle, name = bundlename, public = True)
144     filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
145     context = generic_list(request, bundle.project,
146             'patchwork.views.bundle.public',
147             view_args = {'username': username, 'bundlename': bundlename},
148             filter_settings = filter_settings,
149             patches = bundle.patches.all())
150
151     context.update({'bundle': bundle,
152             'user': user});
153     return render_to_response('patchwork/bundle-public.html', context)
154
155 @login_required
156 def set_patches(request):
157     context = PatchworkRequestContext(request)
158