From 45223fb6074c517f0f19dc0da0fe77d4093ceb8c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 9 May 2019 20:30:23 +1000 Subject: [PATCH] Display count badges in sidenav tree Uses the 'tags' parameter as according to the docs - https://github.com/jonmiles/bootstrap-treeview - Part - Stock --- InvenTree/InvenTree/models.py | 11 +++++++++++ InvenTree/InvenTree/views.py | 5 +++++ InvenTree/part/models.py | 11 +++++------ InvenTree/static/script/inventree/sidenav.js | 3 ++- InvenTree/stock/models.py | 12 ++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index c9c9e2966b..a28b27a5eb 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -41,6 +41,17 @@ class InvenTreeTree(models.Model): null=True, related_name='children') + @property + def item_count(self): + """ Return the number of items which exist *under* this node in the tree. + + Here an 'item' is considered to be the 'leaf' at the end of each branch, + and the exact nature here will depend on the class implementation. + + The default implementation returns zero + """ + return 0 + def getUniqueParents(self, unique=None): """ Return a flat set of all parent items that exist above this node. If any parents are repeated (which would be very bad!), the process is halted diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index d2d8e48e35..967b3b16c4 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -33,6 +33,7 @@ class TreeSerializer(views.APIView): data = { 'text': item.name, 'href': item.get_absolute_url(), + 'tags': [item.item_count], } if item.has_children: @@ -51,12 +52,16 @@ class TreeSerializer(views.APIView): nodes = [] + top_count = 0 + for item in top_items: nodes.append(self.itemToJson(item)) + top_count += item.item_count top = { 'text': self.title, 'nodes': nodes, + 'tags': [top_count], } response = { diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index deaf2a799a..35a2e8336d 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -47,18 +47,17 @@ class PartCategory(InvenTreeTree): verbose_name = "Part Category" verbose_name_plural = "Part Categories" + @property + def item_count(self): + return self.partcount + @property def partcount(self): """ Return the total part count under this category (including children of child categories) """ - count = self.parts.count() - - for child in self.children.all(): - count += child.partcount - - return count + return len(Part.objects.filter(category__in=self.getUniqueChildren())) @property def has_parts(self): diff --git a/InvenTree/static/script/inventree/sidenav.js b/InvenTree/static/script/inventree/sidenav.js index 1beefd0ee1..93ee6f05ff 100644 --- a/InvenTree/static/script/inventree/sidenav.js +++ b/InvenTree/static/script/inventree/sidenav.js @@ -9,7 +9,8 @@ function loadTree(url, tree, data) { if (response.tree) { $(tree).treeview({ data: response.tree, - enableLinks: true + enableLinks: true, + showTags: true, }); var saved_exp = sessionStorage.getItem('inventree-sidenav-expanded-items').split(","); diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 537397f66b..f5ecb1be33 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -48,6 +48,18 @@ class StockLocation(InvenTreeTree): } ) + @property + def stock_item_count(self): + """ Return the number of StockItem objects which live in or under this category + """ + + return len(StockItem.objects.filter(location__in=self.getUniqueChildren())) + + @property + def item_count(self): + + return self.stock_item_count + @receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log') def before_delete_stock_location(sender, instance, using, **kwargs):