Logic fix for part filtering

This commit is contained in:
Oliver Walters 2020-04-11 22:57:16 +10:00
parent e86bc4fa6d
commit e1c6ad7761

View File

@ -149,6 +149,13 @@ class PartList(generics.ListCreateAPIView):
- GET: Return list of objects - GET: Return list of objects
- POST: Create a new Part object - POST: Create a new Part object
The Part object list can be filtered by:
- category: Filter by PartCategory reference
- cascade: If true, include parts from sub-categories
- is_template: Is the part a template part?
- variant_of: Filter by variant_of Part reference
- assembly: Filter by assembly field
""" """
serializer_class = part_serializers.PartSerializer serializer_class = part_serializers.PartSerializer
@ -291,23 +298,23 @@ class PartList(generics.ListCreateAPIView):
cascade = str2bool(self.request.query_params.get('cascade', False)) cascade = str2bool(self.request.query_params.get('cascade', False))
if cat_id is not None: if cat_id is None:
# Top-level parts
if isNull(cat_id): if not cascade:
parts_list = parts_list.filter(category=None) 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 else:
if cascade: try:
parts_list = parts_list.filter(category__in=category.getUniqueChildren()) category = PartCategory.objects.get(pk=cat_id)
# Just return parts directly in the requested category
else: # If '?cascade=true' then include parts which exist in sub-categories
parts_list = parts_list.filter(category=cat_id) if cascade:
except (ValueError, PartCategory.DoesNotExist): parts_list = parts_list.filter(category__in=category.getUniqueChildren())
pass # 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 # Ensure that related models are pre-loaded to reduce DB trips
parts_list = self.get_serializer_class().setup_eager_loading(parts_list) parts_list = self.get_serializer_class().setup_eager_loading(parts_list)