Improved speed of stock tree

This commit is contained in:
Oliver Walters 2019-06-18 00:09:42 +10:00
parent a796b984ff
commit 642660d76e
3 changed files with 20 additions and 12 deletions

View File

@ -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

View File

@ -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)

View File

@ -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')