From f5150f549ad000b3b453bc84abfb921a67b02e7f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 3 Apr 2020 09:37:03 +1100 Subject: [PATCH] Part API changes - Allow filtering parts with null parent (top-level category parts) - Option to include sub-category parts or not --- .../InvenTree/static/script/inventree/part.js | 4 +++ InvenTree/part/api.py | 25 ++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/part.js b/InvenTree/InvenTree/static/script/inventree/part.js index f05c5670da..dceb08dd5f 100644 --- a/InvenTree/InvenTree/static/script/inventree/part.js +++ b/InvenTree/InvenTree/static/script/inventree/part.js @@ -95,6 +95,10 @@ function loadPartTable(table, url, options={}) { query.active = true; } + // Include sub-category search + // TODO - Make this user-configurable! + query.cascade = true; + var columns = [ { field: 'pk', diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index cf5a4b2deb..55e711ff5f 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -243,12 +243,25 @@ class PartList(generics.ListCreateAPIView): # Start with all objects parts_list = Part.objects.all() - if cat_id: - try: - category = PartCategory.objects.get(pk=cat_id) - parts_list = parts_list.filter(category__in=category.getUniqueChildren()) - except PartCategory.DoesNotExist: - pass + cascade = str2bool(self.request.query_params.get('cascade', False)) + + if cat_id is not None: + + if isNull(cat_id): + parts_list = parts_list.filter(category=None) + else: + try: + cat_id = int(cat_id) + category = PartCategory.objects.get(pk=cat_id) + + # If '?cascade=true' then include parts which exist in sub-categories + if cascade: + parts_list = parts_list.filter(category__in=category.getUniqueChildren()) + # Just return parts directly in the requested category + else: + parts_list = parts_list.filter(category=cat_id) + except (ValueError, PartCategory.DoesNotExist): + pass # Ensure that related models are pre-loaded to reduce DB trips parts_list = self.get_serializer_class().setup_eager_loading(parts_list)