summaryrefslogtreecommitdiffstats
path: root/apps/patchwork/views/bundle.py
blob: be6a937f6e6b0a420c25b51ac965d0cbc1e81817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, get_object_or_404
from patchwork.requestcontext import PatchworkRequestContext
from django.http import HttpResponse, HttpResponseRedirect
import django.core.urlresolvers
from patchwork.models import Patch, Bundle, Project
from patchwork.utils import get_patch_ids
from patchwork.forms import BundleForm
from patchwork.views import generic_list
from patchwork.filters import DelegateFilter
from patchwork.paginator import Paginator

@login_required
def setbundle(request):
    context = PatchworkRequestContext(request)

    bundle = None

    if request.method == 'POST':
        action = request.POST.get('action', None)
        if action is None:
            pass
        elif action == 'create':
            project = get_object_or_404(Project,
                    id = request.POST.get('project'))
            bundle = Bundle(owner = request.user, project = project,
                    name = request.POST['name'])
            bundle.save()
            patch_id = request.POST.get('patch_id', None)
            if patch_id:
                patch = get_object_or_404(Patch, id = patch_id)
                bundle.patches.add(patch)
            bundle.save()
        elif action == 'add':
            bundle = get_object_or_404(Bundle,
                    owner = request.user, id = request.POST['id'])
            bundle.save()

            patch_id = request.get('patch_id', None)
            if patch_id:
                patch_ids = patch_id
            else:
                patch_ids = get_patch_ids(request.POST)

            for id in patch_ids:
                try:
                    patch = Patch.objects.get(id = id)
                    bundle.patches.add(patch)
                except ex:
                    pass

            bundle.save()
        elif action == 'delete':
            try:
                bundle = Bundle.objects.get(owner = request.user,
                        id = request.POST['id'])
                bundle.delete()
            except Exception:
                pass

            bundle = None

    else:
        bundle = get_object_or_404(Bundle, owner = request.user,
                id = request.POST['bundle_id'])

    if 'error' in context:
        pass

    if bundle:
        return HttpResponseRedirect(
                django.core.urlresolvers.reverse(
                    'patchwork.views.bundle.bundle',
                    kwargs = {'bundle_id': bundle.id}
                    )
                )
    else:
        return HttpResponseRedirect(
                django.core.urlresolvers.reverse(
                    'patchwork.views.bundle.list')
                )

@login_required
def bundle(request, bundle_id):
    bundle = get_object_or_404(Bundle, id = bundle_id)
    filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]

    if request.method == 'POST' and request.POST.get('form') == 'bundle':
        action = request.POST.get('action', '').lower()
        if action == 'delete':
            bundle.delete()
            return HttpResponseRedirect(
                    django.core.urlresolvers.reverse(
                        'patchwork.views.user.profile')
                    )
        elif action == 'update':
            form = BundleForm(request.POST, instance = bundle)
            if form.is_valid():
                form.save()
    else:
        form = BundleForm(instance = bundle)

    context = generic_list(request, bundle.project,
            'patchwork.views.bundle.bundle',
            view_args = {'bundle_id': bundle_id},
            filter_settings = filter_settings,
            patches = bundle.patches.all())

    context['bundle'] = bundle
    context['bundleform'] = form

    return render_to_response('patchwork/bundle.html', context)

@login_required
def mbox(request, bundle_id):
    bundle = get_object_or_404(Bundle, id = bundle_id)
    response = HttpResponse(mimetype='text/plain')
    response.write(bundle.mbox())
    return response

def public(request, username, bundlename):
    user = get_object_or_404(User, username = username)
    bundle = get_object_or_404(Bundle, name = bundlename, public = True)
    filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)]
    context = generic_list(request, bundle.project,
            'patchwork.views.bundle.public',
            view_args = {'username': username, 'bundlename': bundlename},
            filter_settings = filter_settings,
            patches = bundle.patches.all())

    context.update({'bundle': bundle,
            'user': user});
    return render_to_response('patchwork/bundle-public.html', context)

@login_required
def set_patches(request):
    context = PatchworkRequestContext(request)