X-Git-Url: https://git.ozlabs.org/?p=patchwork;a=blobdiff_plain;f=apps%2Fpatchwork%2Fviews%2Fbundle.py;h=c99e3222dca95dd17f04b337494e270edc2bde07;hp=62b50d4989314fc504431e40407ba5ba26da4d0d;hb=67181f5c929018d5304732969f0811795c13ea37;hpb=bf0aef79a1e5b1d0b78c10df4a610b0a7b2d365f diff --git a/apps/patchwork/views/bundle.py b/apps/patchwork/views/bundle.py index 62b50d4..c99e322 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, BundlePatch, Project from patchwork.utils import get_patch_ids from patchwork.forms import BundleForm, DeleteBundleForm -from patchwork.views import generic_list +from patchwork.views import generic_list, patch_to_mbox from patchwork.filters import DelegateFilter -from patchwork.paginator import Paginator @login_required def setbundle(request): @@ -69,7 +68,7 @@ def setbundle(request): try: patch = Patch.objects.get(id = id) bundle.append_patch(patch) - except ex: + except: pass bundle.save() @@ -114,7 +113,7 @@ def bundles(request): form = DeleteBundleForm(request.POST) if form.is_valid(): bundle = get_object_or_404(Bundle, - id = form.cleaned_data['bundle_id']) + id = form.cleaned_data['bundle_id']) bundle.delete() bundles = Bundle.objects.filter(owner = request.user) @@ -126,69 +125,97 @@ def bundles(request): return render_to_response('patchwork/bundles.html', context) -@login_required -def bundle(request, bundle_id): - bundle = get_object_or_404(Bundle, id = bundle_id) +def bundle(request, username, bundlename): + bundle = get_object_or_404(Bundle, owner__username = username, + name = bundlename) 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) + is_owner = request.user == bundle.owner - 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 + if not (is_owner or bundle.public): + return HttpResponseNotFound() - 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 + 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 = 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.ordered_patches(), - editable_order = True) + 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() + + mbox = '\n'.join([patch_to_mbox(p).as_string(True) + for p in bundle.ordered_patches()]) + response = HttpResponse(mimetype='text/plain') - response['Content-Disposition'] = 'attachment; filename=bundle-%d.mbox' % \ - bundle.id - response.write(bundle.mbox()) + response['Content-Disposition'] = \ + 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name) + + response.write(mbox) return response -def public(request, username, bundlename): - user = get_object_or_404(User, username = username) - bundle = get_object_or_404(Bundle, name = bundlename, owner = user, - 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['bundle'] = bundle - return render_to_response('patchwork/bundle-public.html', context)