mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor PartCategoryParameter API
This commit is contained in:
parent
c08cb43c39
commit
25ff74835d
@ -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')
|
||||
|
@ -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_list = [cat_id]
|
||||
category = PartCategory.objects.get(pk=category)
|
||||
|
||||
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)
|
||||
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'),
|
||||
])),
|
||||
|
@ -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',
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user