Improved API endpoint for category parameter templates

This commit is contained in:
eeintech 2020-11-16 16:10:00 -05:00
parent d8e7c2a932
commit ac2797c7a1
2 changed files with 29 additions and 16 deletions

View File

@ -113,9 +113,9 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
class CategoryParameters(generics.ListAPIView): class CategoryParameters(generics.ListAPIView):
""" API endpoint for accessing a list of PartCategory objects. """ API endpoint for accessing a list of PartCategoryParameterTemplate objects.
- GET: Return a list of PartCategory objects - GET: Return a list of PartCategoryParameterTemplate objects
""" """
queryset = PartCategoryParameterTemplate.objects.all() queryset = PartCategoryParameterTemplate.objects.all()
@ -124,20 +124,35 @@ class CategoryParameters(generics.ListAPIView):
def get_queryset(self): def get_queryset(self):
""" """
Custom filtering: Custom filtering:
- Allow filtering by "null" parent to retrieve top-level part categories - Allow filtering by "null" parent to retrieve all categories parameter templates
- Allow filtering by category
- Allow traversing all parent categories
""" """
cat_id = self.kwargs.get('pk', None) try:
cat_id = int(self.request.query_params.get('category', None))
except TypeError:
cat_id = None
fetch_parent = str2bool(self.request.query_params.get('fetch_parent', 'true'))
queryset = super().get_queryset() queryset = super().get_queryset()
if cat_id is not None: if isinstance(cat_id, int):
try: try:
cat_id = int(cat_id) category = PartCategory.objects.get(pk=cat_id)
queryset = queryset.filter(category=cat_id) except PartCategory.DoesNotExist:
except ValueError: # Return empty queryset
pass return PartCategoryParameterTemplate.objects.none()
category_list = [cat_id]
if fetch_parent:
parent_categories = category.get_ancestors()
for parent in parent_categories:
category_list.append(parent.pk)
queryset = queryset.filter(category__in=category_list)
return queryset return queryset
@ -895,8 +910,8 @@ part_api_urls = [
# Base URL for PartCategory API endpoints # Base URL for PartCategory API endpoints
url(r'^category/', include([ url(r'^category/', include([
url(r'^(?P<pk>\d+)/parameters/?', CategoryParameters.as_view(), name='api-part-category-parameters'),
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'), url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
url(r'^parameters/?', CategoryParameters.as_view(), name='api-part-category-parameters'),
url(r'^$', CategoryList.as_view(), name='api-part-category-list'), url(r'^$', CategoryList.as_view(), name='api-part-category-list'),
])), ])),

View File

@ -423,9 +423,8 @@ class PartParameterTemplateSerializer(InvenTreeModelSerializer):
class CategoryParameterTemplateSerializer(InvenTreeModelSerializer): class CategoryParameterTemplateSerializer(InvenTreeModelSerializer):
""" Serializer for PartCategoryParameterTemplate """ """ Serializer for PartCategoryParameterTemplate """
parameter_template_detail = PartParameterTemplateSerializer(source='parameter_template', parameter_template = PartParameterTemplateSerializer(many=False,
many=False, read_only=True)
read_only=True)
class Meta: class Meta:
model = PartCategoryParameterTemplate model = PartCategoryParameterTemplate
@ -433,6 +432,5 @@ class CategoryParameterTemplateSerializer(InvenTreeModelSerializer):
'pk', 'pk',
'category', 'category',
'parameter_template', 'parameter_template',
'parameter_template_detail',
'default_value', 'default_value',
] ]