mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Display count badges in sidenav tree
Uses the 'tags' parameter as according to the docs - https://github.com/jonmiles/bootstrap-treeview - Part - Stock
This commit is contained in:
		@@ -41,6 +41,17 @@ class InvenTreeTree(models.Model):
 | 
				
			|||||||
                               null=True,
 | 
					                               null=True,
 | 
				
			||||||
                               related_name='children')
 | 
					                               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):
 | 
					    def getUniqueParents(self, unique=None):
 | 
				
			||||||
        """ Return a flat set of all parent items that exist above this node.
 | 
					        """ 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
 | 
					        If any parents are repeated (which would be very bad!), the process is halted
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,6 +33,7 @@ class TreeSerializer(views.APIView):
 | 
				
			|||||||
        data = {
 | 
					        data = {
 | 
				
			||||||
            'text': item.name,
 | 
					            'text': item.name,
 | 
				
			||||||
            'href': item.get_absolute_url(),
 | 
					            'href': item.get_absolute_url(),
 | 
				
			||||||
 | 
					            'tags': [item.item_count],
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if item.has_children:
 | 
					        if item.has_children:
 | 
				
			||||||
@@ -51,12 +52,16 @@ class TreeSerializer(views.APIView):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        nodes = []
 | 
					        nodes = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        top_count = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for item in top_items:
 | 
					        for item in top_items:
 | 
				
			||||||
            nodes.append(self.itemToJson(item))
 | 
					            nodes.append(self.itemToJson(item))
 | 
				
			||||||
 | 
					            top_count += item.item_count
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        top = {
 | 
					        top = {
 | 
				
			||||||
            'text': self.title,
 | 
					            'text': self.title,
 | 
				
			||||||
            'nodes': nodes,
 | 
					            'nodes': nodes,
 | 
				
			||||||
 | 
					            'tags': [top_count],
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        response = {
 | 
					        response = {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,18 +47,17 @@ class PartCategory(InvenTreeTree):
 | 
				
			|||||||
        verbose_name = "Part Category"
 | 
					        verbose_name = "Part Category"
 | 
				
			||||||
        verbose_name_plural = "Part Categories"
 | 
					        verbose_name_plural = "Part Categories"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def item_count(self):
 | 
				
			||||||
 | 
					        return self.partcount
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def partcount(self):
 | 
					    def partcount(self):
 | 
				
			||||||
        """ 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)
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        count = self.parts.count()
 | 
					        return len(Part.objects.filter(category__in=self.getUniqueChildren()))
 | 
				
			||||||
 | 
					 | 
				
			||||||
        for child in self.children.all():
 | 
					 | 
				
			||||||
            count += child.partcount
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return count
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def has_parts(self):
 | 
					    def has_parts(self):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,8 @@ function loadTree(url, tree, data) {
 | 
				
			|||||||
            if (response.tree) {
 | 
					            if (response.tree) {
 | 
				
			||||||
                $(tree).treeview({
 | 
					                $(tree).treeview({
 | 
				
			||||||
                    data: response.tree,
 | 
					                    data: response.tree,
 | 
				
			||||||
                    enableLinks: true
 | 
					                    enableLinks: true,
 | 
				
			||||||
 | 
					                    showTags: true,
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var saved_exp = sessionStorage.getItem('inventree-sidenav-expanded-items').split(",");
 | 
					                var saved_exp = sessionStorage.getItem('inventree-sidenav-expanded-items').split(",");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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')
 | 
					@receiver(pre_delete, sender=StockLocation, dispatch_uid='stocklocation_delete_log')
 | 
				
			||||||
def before_delete_stock_location(sender, instance, using, **kwargs):
 | 
					def before_delete_stock_location(sender, instance, using, **kwargs):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user