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..b837b3da59 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -28,11 +28,22 @@ class TreeSerializer(views.APIView): Ref: https://github.com/jonmiles/bootstrap-treeview """ + @property + def root_url(self): + """ Return the root URL for the tree. Implementation is class dependent. + + Default implementation returns # + """ + + return '#' + def itemToJson(self, item): data = { + 'pk': item.id, 'text': item.name, 'href': item.get_absolute_url(), + 'tags': [item.item_count], } if item.has_children: @@ -51,12 +62,18 @@ class TreeSerializer(views.APIView): nodes = [] + top_count = 0 + for item in top_items: nodes.append(self.itemToJson(item)) + top_count += item.item_count top = { + 'pk': None, 'text': self.title, + 'href': self.root_url, 'nodes': nodes, + 'tags': [top_count], } response = { diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 26dbc2e492..0a2bdbfef6 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -14,6 +14,7 @@ from rest_framework import generics, permissions from django.db.models import Q from django.conf.urls import url, include +from django.urls import reverse from .models import Part, PartCategory, BomItem, PartStar from .models import SupplierPart, SupplierPriceBreak @@ -30,6 +31,10 @@ class PartCategoryTree(TreeSerializer): title = "Parts" model = PartCategory + + @property + def root_url(self): + return reverse('part-index') class CategoryList(generics.ListCreateAPIView): diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 40eac96a81..f91f4d3d7d 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -47,18 +47,18 @@ 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(), + active=True)) @property def has_parts(self): diff --git a/InvenTree/part/templates/part/cat_link.html b/InvenTree/part/templates/part/cat_link.html index 181f10ebb6..1cb16d530e 100644 --- a/InvenTree/part/templates/part/cat_link.html +++ b/InvenTree/part/templates/part/cat_link.html @@ -1,7 +1,7 @@