From 6ce62d26739ebf0dd81ecff5284adf3fbe2aed23 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Sun, 8 Feb 2009 21:40:17 +1100 Subject: Bundle reordering support Bundles can now be reordered and saved. Add dependency on jquery in INSTALL. Signed-off-by: Jeremy Kerr --- apps/patchwork/models.py | 22 ++++++++++++++++++---- apps/patchwork/utils.py | 32 ++++++++++++++++---------------- apps/patchwork/views/bundle.py | 23 +++++++++++++++++++---- apps/patchwork/views/patch.py | 12 ++++++++---- 4 files changed, 61 insertions(+), 28 deletions(-) (limited to 'apps/patchwork') diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index d0c2a6e..a672f9a 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -295,12 +295,25 @@ class Bundle(models.Model): def n_patches(self): return self.patches.all().count() + def ordered_patches(self): + return self.patches.order_by('bundlepatch__order'); + def append_patch(self, patch): # todo: use the aggregate queries in django 1.1 - orders = BundlePatch.objects.filter(bundle = self).values('order') - max_order = max([ v for (k, v) in orders]) + orders = BundlePatch.objects.filter(bundle = self).order_by('-order') \ + .values('order') + + if len(orders) > 0: + max_order = orders[0]['order'] + else: + max_order = 0 + + # see if the patch is already in this bundle + if BundlePatch.objects.filter(bundle = self, patch = patch).count(): + raise Exception("patch is already in bundle") - bp = BundlePatch.objects.create(bundle = self, patch = patch, order = max_order + 1) + bp = BundlePatch.objects.create(bundle = self, patch = patch, + order = max_order + 1) bp.save() class Meta: @@ -327,7 +340,8 @@ class BundlePatch(models.Model): order = models.IntegerField() class Meta: - unique_together = [('bundle', 'patch'), ('bundle', 'order')] + unique_together = [('bundle', 'patch')] + ordering = ['order'] class UserPersonConfirmation(models.Model): user = models.ForeignKey(User) diff --git a/apps/patchwork/utils.py b/apps/patchwork/utils.py index 63daa85..5bd6925 100644 --- a/apps/patchwork/utils.py +++ b/apps/patchwork/utils.py @@ -19,7 +19,7 @@ from patchwork.forms import MultiplePatchForm -from patchwork.models import Bundle, Project, State, UserProfile +from patchwork.models import Bundle, Project, BundlePatch, State, UserProfile from django.conf import settings from django.shortcuts import render_to_response, get_object_or_404 @@ -100,35 +100,35 @@ def set_bundle(user, project, action, data, patches, context): bundle = Bundle(owner = user, project = project, name = data['bundle_name']) bundle.save() - str = 'added to new bundle "%s"' % bundle.name - auth_required = False + context.add_message("Bundle %s created" % bundle.name) elif action =='add': bundle = get_object_or_404(Bundle, id = data['bundle_id']) - str = 'added to bundle "%s"' % bundle.name - auth_required = False elif action =='remove': bundle = get_object_or_404(Bundle, id = data['removed_bundle_id']) - str = 'removed from bundle "%s"' % bundle.name - auth_required = False if not bundle: return ['no such bundle'] for patch in patches: if action == 'create' or action == 'add': - bundle.append_patch(patch) + try: + bundle.append_patch(patch) + context.add_message("Patch '%s' added to bundle %s" % \ + (patch.name, bundle.name)) + except Exception, ex: + context.add_message("Couldn't add patch '%s' to bundle: %s" % \ + (patch.name, ex.message)) elif action == 'remove': - bundle.patches.remove(patch) - - if len(patches) > 0: - if len(patches) == 1: - str = 'patch ' + str - else: - str = 'patches ' + str - context.add_message(str) + try: + bp = BundlePatch.objects.get(bundle = bundle, patch = patch) + bp.delete() + context.add_message("Patch '%s' removed from bundle %s\n" % \ + (patch.name, bundle.name)) + except Exception: + pass bundle.save() diff --git a/apps/patchwork/views/bundle.py b/apps/patchwork/views/bundle.py index d8e4e2f..9995fc6 100644 --- a/apps/patchwork/views/bundle.py +++ b/apps/patchwork/views/bundle.py @@ -23,7 +23,7 @@ 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.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 @@ -49,7 +49,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,7 +68,7 @@ def setbundle(request): for id in patch_ids: try: patch = Patch.objects.get(id = id) - bundle.patches.add(patch) + bundle.append_patch(patch) except ex: pass @@ -143,11 +146,23 @@ def bundle(request, bundle_id): 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 + context = generic_list(request, bundle.project, 'patchwork.views.bundle.bundle', view_args = {'bundle_id': bundle_id}, filter_settings = filter_settings, - patches = bundle.patches.all()) + patches = bundle.ordered_patches(), + editable_order = True) context['bundle'] = bundle context['bundleform'] = form diff --git a/apps/patchwork/views/patch.py b/apps/patchwork/views/patch.py index 72472ca..49843eb 100644 --- a/apps/patchwork/views/patch.py +++ b/apps/patchwork/views/patch.py @@ -59,7 +59,7 @@ def patch(request, patch_id): data = request.POST) if createbundleform.is_valid(): createbundleform.save() - bundle.patches.add(patch) + bundle.append_patch(patch) bundle.save() createbundleform = CreateBundleForm() context.add_message('Bundle %s created' % bundle.name) @@ -67,9 +67,13 @@ def patch(request, patch_id): elif action == 'addtobundle': bundle = get_object_or_404(Bundle, id = \ request.POST.get('bundle_id')) - bundle.patches.add(patch) - bundle.save() - context.add_message('Patch added to bundle "%s"' % bundle.name) + try: + bundle.append_patch(patch) + bundle.save() + context.add_message('Patch added to bundle "%s"' % bundle.name) + except Exception, ex: + context.add_message("Couldn't add patch '%s' to bundle %s: %s" \ + % (patch.name, bundle.name, ex.message)) # all other actions require edit privs elif not editable: -- cgit v1.2.3