]> git.ozlabs.org Git - patchwork/commitdiff
bundles: Add check for duplicate bundles
authorJeremy Kerr <jk@ozlabs.org>
Mon, 22 Oct 2012 04:19:27 +0000 (12:19 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 22 Oct 2012 05:56:46 +0000 (13:56 +0800)
... rather than failing with an IntegrityError.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/tests/bundles.py
apps/patchwork/utils.py

index 53eee2516c6459ebec6991cac2f1565bf067737c..e500b3e5412b3180ed119b84c423c45a642125dc 100644 (file)
@@ -179,6 +179,38 @@ class BundleCreateFromListTest(BundleTestBase):
         # test that no new bundles are present
         self.failUnlessEqual(n_bundles, Bundle.objects.count())
 
+    def testCreateDuplicateName(self):
+        newbundlename = 'testbundle-dup'
+        patch = self.patches[0]
+
+        params = {'form': 'patchlistform',
+                  'bundle_name': newbundlename,
+                  'action': 'Create',
+                  'project': defaults.project.id,
+                  'patch_id:%d' % patch.id: 'checked'}
+
+        response = self.client.post(
+                '/project/%s/list/' % defaults.project.linkname,
+                params)
+
+        n_bundles = Bundle.objects.count()
+        self.assertContains(response, 'Bundle %s created' % newbundlename)
+        self.assertContains(response, 'added to bundle %s' % newbundlename,
+            count = 1)
+
+        bundle = Bundle.objects.get(name = newbundlename)
+        self.failUnlessEqual(bundle.patches.count(), 1)
+        self.failUnlessEqual(bundle.patches.all()[0], patch)
+
+        response = self.client.post(
+                '/project/%s/list/' % defaults.project.linkname,
+                params)
+
+        self.assertNotContains(response, 'Bundle %s created' % newbundlename)
+        self.assertContains(response, 'You already have a bundle called')
+        self.assertEqual(Bundle.objects.count(), n_bundles)
+        self.assertEqual(bundle.patches.count(), 1)
+
 class BundleCreateFromPatchTest(BundleTestBase):
     def testCreateNonEmptyBundle(self):
         newbundlename = 'testbundle-new'
index e7619c319091ce2dd9e78396f24ffbe8dac1f6db..1771167ae12f35fb55479c35aa865be496ce2664 100644 (file)
@@ -26,6 +26,7 @@ from django.contrib.sites.models import Site
 from django.conf import settings
 from django.core.mail import EmailMessage
 from django.db.models import Max
+from django.db.utils import IntegrityError
 from patchwork.forms import MultiplePatchForm
 from patchwork.models import Bundle, Project, BundlePatch, UserProfile, \
         PatchChangeNotification, EmailOptout
@@ -107,6 +108,9 @@ def set_bundle(user, project, action, data, patches, context):
         if not bundle_name:
             return ['No bundle name was specified']
 
+        if Bundle.objects.filter(owner = user, name = bundle_name).count() > 0:
+            return ['You already have a bundle called "%s"' % bundle_name]
+
         bundle = Bundle(owner = user, project = project,
                 name = bundle_name)
         bundle.save()