mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Split tree generation off into a separate function
This commit is contained in:
parent
c6b1a80703
commit
1cbbe9e7c6
@ -59,19 +59,20 @@ class TreeSerializer(views.APIView):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def generate_tree(self, items):
|
||||||
|
|
||||||
top_items = self.model.objects.filter(parent=None).order_by('name')
|
|
||||||
|
|
||||||
nodes = []
|
nodes = []
|
||||||
|
|
||||||
top_count = 0
|
top_count = 0
|
||||||
|
|
||||||
|
# Construct the top-level items
|
||||||
|
top_items = [i for i in items if i.parent is None]
|
||||||
|
|
||||||
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_count += item.item_count
|
||||||
|
|
||||||
top = {
|
self.tree = {
|
||||||
'pk': None,
|
'pk': None,
|
||||||
'text': self.title,
|
'text': self.title,
|
||||||
'href': self.root_url,
|
'href': self.root_url,
|
||||||
@ -79,8 +80,15 @@ class TreeSerializer(views.APIView):
|
|||||||
'tags': [top_count],
|
'tags': [top_count],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
""" Respond to a GET request for the Tree """
|
||||||
|
|
||||||
|
items = self.model.objects.all()
|
||||||
|
|
||||||
|
self.generate_tree(items)
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
'tree': [top]
|
'tree': [self.tree]
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonResponse(response, safe=False)
|
return JsonResponse(response, safe=False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user