Refactor PartCategoryParameter API

This commit is contained in:
Oliver 2021-07-28 14:05:49 +10:00
parent c08cb43c39
commit 25ff74835d
3 changed files with 24 additions and 20 deletions

View File

@ -793,6 +793,8 @@ class SettingsView(TemplateView):
ctx["rates"] = Rate.objects.filter(backend="InvenTreeExchange")
ctx["categories"] = PartCategory.objects.all().order_by('tree_id', 'lft', 'name')
# When were the rates last updated?
try:
backend = ExchangeBackend.objects.get(name='InvenTreeExchange')

View File

@ -6,9 +6,10 @@ Provides a JSON API for the Part app
from __future__ import unicode_literals
from django.conf.urls import url, include
from django.db.models.expressions import Value
from django.urls import reverse
from django.http import JsonResponse
from django.db.models import Q, F, Count, Min, Max, Avg
from django.db.models import Q, F, Count, Min, Max, Avg, query
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
@ -159,7 +160,7 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = PartCategory.objects.all()
class CategoryParameters(generics.ListAPIView):
class CategoryParameterList(generics.ListAPIView):
""" API endpoint for accessing a list of PartCategoryParameterTemplate objects.
- GET: Return a list of PartCategoryParameterTemplate objects
@ -176,30 +177,27 @@ class CategoryParameters(generics.ListAPIView):
- Allow traversing all parent categories
"""
try:
cat_id = int(self.kwargs.get('pk', None))
except TypeError:
cat_id = None
fetch_parent = str2bool(self.request.query_params.get('fetch_parent', 'true'))
queryset = super().get_queryset()
if isinstance(cat_id, int):
params = self.request.query_params
category = params.get('category', None)
if category is not None:
try:
category = PartCategory.objects.get(pk=cat_id)
except PartCategory.DoesNotExist:
# Return empty queryset
return PartCategoryParameterTemplate.objects.none()
category = PartCategory.objects.get(pk=category)
category_list = [cat_id]
fetch_parent = str2bool(params.get('fetch_parent', True))
if fetch_parent:
parent_categories = category.get_ancestors()
for parent in parent_categories:
category_list.append(parent.pk)
if fetch_parent:
parents = category.get_ancestors(include_self=True)
queryset = queryset.filter(category__in=[cat.pk for cat in parents])
else:
queryset = queryset.filter(category=category)
queryset = queryset.filter(category__in=category_list)
except (ValueError, PartCategory.DoesNotExist):
pass
return queryset
@ -1094,7 +1092,8 @@ part_api_urls = [
# Base URL for PartCategory API endpoints
url(r'^category/', include([
url(r'^(?P<pk>\d+)/parameters/?', CategoryParameters.as_view(), name='api-part-category-parameters'),
url(r'^parameters/', CategoryParameterList.as_view(), name='api-part-category-parameter-list'),
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
url(r'^$', CategoryList.as_view(), name='api-part-category-list'),
])),

View File

@ -526,11 +526,14 @@ class CategoryParameterTemplateSerializer(InvenTreeModelSerializer):
parameter_template = PartParameterTemplateSerializer(many=False,
read_only=True)
category_detail = CategorySerializer(source='category', many=False, read_only=True)
class Meta:
model = PartCategoryParameterTemplate
fields = [
'pk',
'category',
'category_detail',
'parameter_template',
'default_value',
]