diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index a258d58962..9bf7294335 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -113,9 +113,9 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView): 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() @@ -124,21 +124,36 @@ class CategoryParameters(generics.ListAPIView): def get_queryset(self): """ 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() - if cat_id is not None: - - try: - cat_id = int(cat_id) - queryset = queryset.filter(category=cat_id) - except ValueError: - pass + if isinstance(cat_id, int): + try: + category = PartCategory.objects.get(pk=cat_id) + except PartCategory.DoesNotExist: + # Return empty queryset + 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 @@ -895,8 +910,8 @@ part_api_urls = [ # Base URL for PartCategory API endpoints url(r'^category/', include([ - url(r'^(?P\d+)/parameters/?', CategoryParameters.as_view(), name='api-part-category-parameters'), url(r'^(?P\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'), ])), diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index bda49da671..7bec904ce0 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -423,9 +423,8 @@ class PartParameterTemplateSerializer(InvenTreeModelSerializer): class CategoryParameterTemplateSerializer(InvenTreeModelSerializer): """ Serializer for PartCategoryParameterTemplate """ - parameter_template_detail = PartParameterTemplateSerializer(source='parameter_template', - many=False, - read_only=True) + parameter_template = PartParameterTemplateSerializer(many=False, + read_only=True) class Meta: model = PartCategoryParameterTemplate @@ -433,6 +432,5 @@ class CategoryParameterTemplateSerializer(InvenTreeModelSerializer): 'pk', 'category', 'parameter_template', - 'parameter_template_detail', 'default_value', ]