]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
models: Make use of aggregates
authorStephen Finucane <stephen@that.guru>
Thu, 20 Oct 2016 07:28:53 +0000 (08:28 +0100)
committerStephen Finucane <stephen@that.guru>
Sat, 22 Oct 2016 10:22:36 +0000 (11:22 +0100)
We're well past Django 1.1 now, so resolve a TODO to use aggregate
support introduced in this version. As part of this change, replace
the use of 'count' to check for presence of matching objects with
'exists'.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Daniel Axtens <dja@axtens.net>
patchwork/models.py

index f8759a5c8f8b93b46de4aba624fa0467e8c947b7..6f3257f9514c08eb2499f1d433e59656a99ad6c4 100644 (file)
@@ -577,25 +577,19 @@ class Bundle(models.Model):
         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).order_by('-order') \
-            .values('order')
+        orders = BundlePatch.objects.filter(bundle=self).aggregate(
+            models.Max('order'))
 
-        if len(orders) > 0:
-            max_order = orders[0]['order']
+        if orders and orders['order__max']:
+            max_order = orders['order__max']
         else:
             max_order = 0
 
-        # see if the patch is already in this bundle
-        if BundlePatch.objects.filter(bundle=self,
-                                      patch=patch).count():
+        if BundlePatch.objects.filter(bundle=self, patch=patch).exists():
             return
 
-        bp = BundlePatch.objects.create(bundle=self, patch=patch,
-                                        order=max_order + 1)
-        bp.save()
-
-        return bp
+        return BundlePatch.objects.create(bundle=self, patch=patch,
+                                          order=max_order + 1)
 
     def public_url(self):
         if not self.public: