From 6e65a736e7cd6f5671f9e3f3f416197c485b9d2d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 3 Apr 2020 09:31:26 +1100 Subject: [PATCH] Add isNull function to query against null keys --- InvenTree/InvenTree/helpers.py | 15 +++++++++++++++ InvenTree/part/api.py | 21 +++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 023bf5c451..0abe5313ee 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -52,6 +52,21 @@ def str2bool(text, test=True): return str(text).lower() in ['0', 'n', 'no', 'none', 'f', 'false', 'off', ] +def isNull(text): + """ + Test if a string 'looks' like a null value. + This is useful for querying the API against a null key. + + Args: + text: Input text + + Returns: + True if the text looks like a null value + """ + + return str(text).strip().lower() in ['top', 'null', 'none', 'empty', 'false', '-1'] + + def decimal2string(d): """ Format a Decimal number as a string, diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 0538125828..cf5a4b2deb 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -27,7 +27,7 @@ from . import serializers as part_serializers from InvenTree.status_codes import OrderStatus, StockStatus, BuildStatus from InvenTree.views import TreeSerializer -from InvenTree.helpers import str2bool +from InvenTree.helpers import str2bool, isNull class PartCategoryTree(TreeSerializer): @@ -69,15 +69,16 @@ class CategoryList(generics.ListCreateAPIView): if cat_id is not None: - # Integer id? - try: - cat_id = int(cat_id) - queryset = queryset.filter(parent=cat_id) - except ValueError: - - # Look for top-level categories? - if str(cat_id).lower() in ['top', 'null', 'none', 'false', '-1']: - queryset = queryset.filter(parent=None) + # Look for top-level categories + if isNull(cat_id): + queryset = queryset.filter(parent=None) + + else: + try: + cat_id = int(cat_id) + queryset = queryset.filter(parent=cat_id) + except ValueError: + pass return queryset