Add isNull function to query against null keys

This commit is contained in:
Oliver Walters 2020-04-03 09:31:26 +11:00
parent d17056820b
commit 6e65a736e7
2 changed files with 26 additions and 10 deletions

View File

@ -52,6 +52,21 @@ def str2bool(text, test=True):
return str(text).lower() in ['0', 'n', 'no', 'none', 'f', 'false', 'off', ] 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): def decimal2string(d):
""" """
Format a Decimal number as a string, Format a Decimal number as a string,

View File

@ -27,7 +27,7 @@ from . import serializers as part_serializers
from InvenTree.status_codes import OrderStatus, StockStatus, BuildStatus from InvenTree.status_codes import OrderStatus, StockStatus, BuildStatus
from InvenTree.views import TreeSerializer from InvenTree.views import TreeSerializer
from InvenTree.helpers import str2bool from InvenTree.helpers import str2bool, isNull
class PartCategoryTree(TreeSerializer): class PartCategoryTree(TreeSerializer):
@ -69,15 +69,16 @@ class CategoryList(generics.ListCreateAPIView):
if cat_id is not None: if cat_id is not None:
# Integer id? # Look for top-level categories
try: if isNull(cat_id):
cat_id = int(cat_id) queryset = queryset.filter(parent=None)
queryset = queryset.filter(parent=cat_id)
except ValueError:
# Look for top-level categories? else:
if str(cat_id).lower() in ['top', 'null', 'none', 'false', '-1']: try:
queryset = queryset.filter(parent=None) cat_id = int(cat_id)
queryset = queryset.filter(parent=cat_id)
except ValueError:
pass
return queryset return queryset