X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fviews%2Fbundle.py;h=7a120dd2e74ae4855dbfd7b1db51dde8f742267e;hb=99209535d901a7bb1f10ad7d1de0a9f6308b2ce7;hp=65ca583ef8d1d26804e928ca7b69e18d826f80e5;hpb=8e8be3bcc0db37a92e12fe3ee2a3351c7a6bbcd9;p=patchwork diff --git a/apps/patchwork/views/bundle.py b/apps/patchwork/views/bundle.py index 65ca583..7a120dd 100644 --- a/apps/patchwork/views/bundle.py +++ b/apps/patchwork/views/bundle.py @@ -21,14 +21,13 @@ 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 +from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound import django.core.urlresolvers -from patchwork.models import Patch, Bundle, Project +from patchwork.models import Patch, Bundle, BundlePatch, Project from patchwork.utils import get_patch_ids -from patchwork.forms import BundleForm +from patchwork.forms import BundleForm, DeleteBundleForm from patchwork.views import generic_list from patchwork.filters import DelegateFilter -from patchwork.paginator import Paginator @login_required def setbundle(request): @@ -49,7 +48,10 @@ def setbundle(request): patch_id = request.POST.get('patch_id', None) if patch_id: patch = get_object_or_404(Patch, id = patch_id) - bundle.patches.add(patch) + try: + bundle.append_patch(patch) + except Exception: + pass bundle.save() elif action == 'add': bundle = get_object_or_404(Bundle, @@ -65,8 +67,8 @@ def setbundle(request): for id in patch_ids: try: patch = Patch.objects.get(id = id) - bundle.patches.add(patch) - except ex: + bundle.append_patch(patch) + except: pass bundle.save() @@ -101,53 +103,115 @@ def setbundle(request): ) @login_required -def bundle(request, bundle_id): - bundle = get_object_or_404(Bundle, id = bundle_id) - filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)] +def bundles(request): + context = PatchworkRequestContext(request) - 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 request.method == 'POST': + form_name = request.POST.get('form_name', '') + + if form_name == DeleteBundleForm.name: + form = DeleteBundleForm(request.POST) if form.is_valid(): - form.save() + bundle = get_object_or_404(Bundle, + id = form.cleaned_data['bundle_id']) + bundle.delete() + + bundles = Bundle.objects.filter(owner = request.user) + for bundle in bundles: + bundle.delete_form = DeleteBundleForm(auto_id = False, + initial = {'bundle_id': bundle.id}) + + context['bundles'] = bundles + + return render_to_response('patchwork/bundles.html', context) + +def bundle(request, username, bundlename): + bundle = get_object_or_404(Bundle, owner__username = username, + name = bundlename) + filter_settings = [(DelegateFilter, DelegateFilter.AnyDelegate)] + + is_owner = request.user == bundle.owner + + if not (is_owner or bundle.public): + return HttpResponseNotFound() + + if is_owner: + 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() + + # if we've changed the bundle name, redirect to new URL + bundle = Bundle.objects.get(pk = bundle.pk) + if bundle.name != bundlename: + return HttpResponseRedirect(bundle.get_absolute_url()) + + else: + form = BundleForm(instance = bundle) + else: + form = BundleForm(instance = bundle) + + if request.method == 'POST' and \ + request.POST.get('form') == 'reorderform': + order = get_object_or_404(BundlePatch, bundle = bundle, + patch__id = request.POST.get('order_start')).order + + for patch_id in request.POST.getlist('neworder'): + bundlepatch = get_object_or_404(BundlePatch, + bundle = bundle, patch__id = patch_id) + bundlepatch.order = order + bundlepatch.save() + order += 1 else: - form = BundleForm(instance = bundle) + form = None context = generic_list(request, bundle.project, 'patchwork.views.bundle.bundle', - view_args = {'bundle_id': bundle_id}, + view_args = {'username': bundle.owner.username, + 'bundlename': bundle.name}, filter_settings = filter_settings, - patches = bundle.patches.all()) + patches = bundle.ordered_patches(), + editable_order = is_owner) 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) +def mbox(request, username, bundlename): + bundle = get_object_or_404(Bundle, owner__username = username, + name = bundlename) + + if not (request.user == bundle.owner or bundle.public): + return HttpResponseNotFound() + response = HttpResponse(mimetype='text/plain') + response['Content-Disposition'] = \ + 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name) 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()) +@login_required +def bundle_redir(request, bundle_id): + bundle = get_object_or_404(Bundle, id = bundle_id, owner = request.user) + return HttpResponseRedirect(bundle.get_absolute_url()) + +@login_required +def mbox_redir(request, bundle_id): + bundle = get_object_or_404(Bundle, id = bundle_id, owner = request.user) + return HttpResponseRedirect(django.core.urlresolvers.reverse( + 'patchwork.views.bundle.mbox', kwargs = { + 'username': request.user.username, + 'bundlename': bundle.name, + })) + + - context.update({'bundle': bundle, - 'user': user}); - return render_to_response('patchwork/bundle-public.html', context)