Part API changes

- Allow filtering parts with null parent (top-level category parts)
- Option to include sub-category parts or not
This commit is contained in:
Oliver Walters 2020-04-03 09:37:03 +11:00
parent 6e65a736e7
commit f5150f549a
2 changed files with 23 additions and 6 deletions

View File

@ -95,6 +95,10 @@ function loadPartTable(table, url, options={}) {
query.active = true; query.active = true;
} }
// Include sub-category search
// TODO - Make this user-configurable!
query.cascade = true;
var columns = [ var columns = [
{ {
field: 'pk', field: 'pk',

View File

@ -243,11 +243,24 @@ class PartList(generics.ListCreateAPIView):
# Start with all objects # Start with all objects
parts_list = Part.objects.all() parts_list = Part.objects.all()
if cat_id: 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: try:
cat_id = int(cat_id)
category = PartCategory.objects.get(pk=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()) parts_list = parts_list.filter(category__in=category.getUniqueChildren())
except PartCategory.DoesNotExist: # Just return parts directly in the requested category
else:
parts_list = parts_list.filter(category=cat_id)
except (ValueError, PartCategory.DoesNotExist):
pass pass
# Ensure that related models are pre-loaded to reduce DB trips # Ensure that related models are pre-loaded to reduce DB trips