only one api call

This commit is contained in:
Matthias 2021-12-08 00:05:34 +01:00
parent 948270bbcb
commit 596532f312
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
3 changed files with 17 additions and 54 deletions

View File

@ -246,38 +246,6 @@ class CategoryTree(generics.ListAPIView):
queryset = PartCategory.objects.all()
serializer_class = part_serializers.CategoryTree
def filter_queryset(self, queryset):
"""
"""
queryset = super().filter_queryset(queryset)
params = self.request.query_params
cat_id = params.get('id', None)
if cat_id in (None, '', '#', ):
filter_kwargs = {}
# get whole tree
if 'ref_type' in self.kwargs and 'ref' in self.kwargs:
if 'part' in self.kwargs['ref_type']:
filter_kwargs['parts__id'] = self.kwargs['ref']
elif 'category' in self.kwargs['ref_type']:
filter_kwargs['id'] = self.kwargs['ref']
else:
filter_kwargs['id'] = None
branch = queryset.model.objects.filter(**filter_kwargs).get_ancestors(include_self=True)
queryset = queryset.filter(parent=None) | branch
self.kwargs['ancestors'] = branch
else:
queryset = queryset.filter(parent_id=cat_id)
return queryset
def get_serializer_context(self):
context = super().get_serializer_context()
if 'ancestors' in self.kwargs:
context['ancestors'] = self.kwargs['ancestors']
return context
class PartSalePriceList(generics.ListCreateAPIView):
"""
@ -1555,7 +1523,6 @@ part_api_urls = [
# Base URL for PartCategory API endpoints
url(r'^category/', include([
url(r'^tree/(?P<ref_type>[-\w]+)/(?P<ref>\d+)/', CategoryTree.as_view(), name='api-part-category-root-tree'),
url(r'^tree/', CategoryTree.as_view(), name='api-part-category-tree'),
url(r'^parameters/', CategoryParameterList.as_view(), name='api-part-category-parameter-list'),

View File

@ -79,25 +79,27 @@ class CategoryTree(InvenTreeModelSerializer):
parent = serializers.SerializerMethodField()
children = serializers.SerializerMethodField()
a_attr = serializers.SerializerMethodField()
state = serializers.SerializerMethodField()
def get_parent(self, obj):
return obj.parent.pk if obj.parent else '#'
def get_children(self, obj):
return True if obj.has_children else False
# children = serializers.SerializerMethodField()
# def get_children(self, obj):
# return True if obj.has_children else False
a_attr = serializers.SerializerMethodField()
def get_a_attr(self, obj):
return {'href': obj.get_absolute_url()}
def get_state(self, obj):
if 'ancestors' in self.context:
return {'opened': obj in self.context['ancestors'] }
return {}
# state = serializers.SerializerMethodField()
# def get_state(self, obj):
# state = {}
# if 'ancestors' in self.context:
# state['opened'] = obj in self.context['ancestors'] or obj == self.context['root']
# state['selected'] = obj == self.context['active']
# return state
class Meta:
model = PartCategory
@ -105,9 +107,9 @@ class CategoryTree(InvenTreeModelSerializer):
'id',
'text',
'parent',
'children',
#'children',
'a_attr',
'state',
#'state',
]

View File

@ -153,13 +153,7 @@ function enableSidetree(label) {
$('#tree').jstree({
'core': {
'data': {
'url': function(node) {
if (node.id == '#') {
var url_parts = String(window.location).split("/").slice(-3);
return `/api/part/category/tree/${url_parts[0]}/${url_parts[1]}/`;
}
return '/api/part/category/tree/'
},
'url': '/api/part/category/tree/',
'data': function(node) {
return {'id': node.id};
}