From 642660d76ed20540af20f27b7df87ea8b2855221 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 18 Jun 2019 00:09:42 +1000 Subject: [PATCH] Improved speed of stock tree --- InvenTree/InvenTree/models.py | 17 +++++++++++------ InvenTree/part/models.py | 3 +-- InvenTree/stock/models.py | 12 ++++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index a7fc93bf3f..4ca630625f 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -62,13 +62,18 @@ class InvenTreeTree(models.Model): If any parents are repeated (which would be very bad!), the process is halted """ - if unique is None: - unique = set() - else: - unique.add(self.id) + item = self - if self.parent and self.parent.id not in unique: - self.parent.getUniqueParents(unique) + # Prevent infinite regression + max_parents = 500 + + unique = set() + + while item.parent and max_parents > 0: + max_parents -= 1 + + unique.add(item.parent.id) + item = item.parent return unique diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 9bf8a78845..26dc54c4f9 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -66,9 +66,8 @@ class PartCategory(InvenTreeTree): @property def item_count(self): - return self.partcount + return self.partcount() - @property def partcount(self, cascade=True, active=True): """ Return the total part count under this category (including children of child categories) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index a53e61f86d..e992b4eb9f 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -49,19 +49,23 @@ class StockLocation(InvenTreeTree): } ) - @property - def stock_item_count(self): + def stock_item_count(self, cascade=True): """ Return the number of StockItem objects which live in or under this category """ - return StockItem.objects.filter(location__in=self.getUniqueChildren()).count() + if cascade: + query = StockItem.objects.filter(location__in=self.getUniqueChildren()) + else: + query = StockItem.objects.filter(location=self) + + return query.count() @property def item_count(self): """ Simply returns the number of stock items in this location. Required for tree view serializer. """ - return self.stock_item_count + return self.stock_item_count() @receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log')