X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=inline;f=apps%2Fpatchwork%2Fmodels.py;h=a672f9ad41ebe5f16879db45a655a42b2c5643c8;hb=6ce62d26739ebf0dd81ecff5284adf3fbe2aed23;hp=d0c2a6ed5d4c467fe289aa2ea1eddc64096c6786;hpb=6cf8d6e128b9117f10431eb9b534e9c8b1024cdf;p=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)