mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add isNull function to query against null keys
This commit is contained in:
parent
d17056820b
commit
6e65a736e7
@ -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,
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user