Update StockLocation and PartCategory models

- Use the MPTT functionality once more
This commit is contained in:
Oliver Walters 2019-09-08 19:13:13 +10:00
parent 4d7fba9f14
commit 678157aac4
2 changed files with 42 additions and 16 deletions

View File

@ -64,21 +64,32 @@ class PartCategory(InvenTreeTree):
verbose_name = "Part Category" verbose_name = "Part Category"
verbose_name_plural = "Part Categories" verbose_name_plural = "Part Categories"
def get_parts(self, cascade=True):
""" Return a queryset for all parts under this category.
args:
cascade - If True, also look under subcategories (default = True)
"""
if cascade:
""" Select any parts which exist in this category or any child categories """
query = Part.objects.filter(category__in=self.getUniqueChildren(include_self=True))
else:
query = Part.objects.filter(category=self.pk)
return query
@property @property
def item_count(self): def item_count(self):
return self.partcount() return self.partcount()
def partcount(self, cascade=True, active=True): def partcount(self, cascade=True, active=False):
""" Return the total part count under this category """ Return the total part count under this category
(including children of child categories) (including children of child categories)
""" """
cats = [self.id] query = self.get_parts(cascade=cascade)
if cascade:
cats += [cat for cat in self.getUniqueChildren()]
query = Part.objects.filter(category__in=cats)
if active: if active:
query = query.filter(active=True) query = query.filter(active=True)
@ -88,7 +99,7 @@ class PartCategory(InvenTreeTree):
@property @property
def has_parts(self): def has_parts(self):
""" True if there are any parts in this category """ """ True if there are any parts in this category """
return self.parts.count() > 0 return self.partcount() > 0
@receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log') @receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log')

View File

@ -34,9 +34,6 @@ class StockLocation(InvenTreeTree):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('stock-location-detail', kwargs={'pk': self.id}) return reverse('stock-location-detail', kwargs={'pk': self.id})
def has_items(self):
return self.stock_items.count() > 0
def format_barcode(self): def format_barcode(self):
""" Return a JSON string for formatting a barcode for this StockLocation object """ """ Return a JSON string for formatting a barcode for this StockLocation object """
@ -49,16 +46,34 @@ class StockLocation(InvenTreeTree):
} }
) )
def get_stock_items(self, cascade=True):
""" Return a queryset for all stock items under this category.
Args:
cascade: If True, also look under sublocations (default = True)
"""
if cascade:
query = StockItem.objects.filter(location__in=self.getUniqueChildren(include_self=True))
else:
query = StockItem.objects.filter(location=self.pk)
return query
def stock_item_count(self, cascade=True): def stock_item_count(self, cascade=True):
""" Return the number of StockItem objects which live in or under this category """ Return the number of StockItem objects which live in or under this category
""" """
if cascade: return self.get_stock_items(cascade).count()
query = StockItem.objects.filter(location__in=self.getUniqueChildren())
else: def has_items(self, cascade=True):
query = StockItem.objects.filter(location=self) """ Return True if there are StockItems existing in this category.
Args:
cascade: If True, also search an sublocations (default = True)
"""
return self.stock_item_count(cascade) > 0
return query.count()
@property @property
def item_count(self): def item_count(self):