Simplify extended filters

This commit is contained in:
Oliver Walters 2019-05-16 19:14:43 +10:00
parent e1558a7a96
commit b4df96aaee
5 changed files with 4 additions and 37 deletions

View File

@ -12,7 +12,6 @@ from rest_framework.response import Response
from rest_framework import filters from rest_framework import filters
from rest_framework import generics, permissions from rest_framework import generics, permissions
from django.db.models import Q
from django.conf.urls import url, include from django.conf.urls import url, include
from django.urls import reverse from django.urls import reverse
@ -109,20 +108,7 @@ class PartList(generics.ListCreateAPIView):
if cat_id: if cat_id:
try: try:
category = PartCategory.objects.get(pk=cat_id) category = PartCategory.objects.get(pk=cat_id)
parts_list = parts_list.filter(category__in=category.getUniqueChildren())
# Filter by the supplied category
flt = Q(category=cat_id)
if self.request.query_params.get('include_child_categories', None):
childs = category.getUniqueChildren()
for child in childs:
# Ignore the top-level category (already filtered)
if str(child) == str(cat_id):
continue
flt |= Q(category=child)
parts_list = parts_list.filter(flt)
except PartCategory.DoesNotExist: except PartCategory.DoesNotExist:
pass pass

View File

@ -153,7 +153,6 @@
query: { query: {
{% if category %} {% if category %}
category: {{ category.id }}, category: {{ category.id }},
include_child_categories: true,
{% endif %} {% endif %}
}, },
buttons: ['#part-options'], buttons: ['#part-options'],

View File

@ -105,13 +105,6 @@ class PartAPITest(APITestCase):
url = reverse('api-part-list') url = reverse('api-part-list')
data = {'category': 1} data = {'category': 1}
response = self.client.get(url, data, format='json')
# There should be 1 part in this category
self.assertEqual(len(response.data), 0)
data['include_child_categories'] = 1
# Now request to include child categories # Now request to include child categories
response = self.client.get(url, data, format='json') response = self.client.get(url, data, format='json')

View File

@ -18,7 +18,6 @@ from .serializers import LocationSerializer
from .serializers import StockTrackingSerializer from .serializers import StockTrackingSerializer
from InvenTree.views import TreeSerializer from InvenTree.views import TreeSerializer
from InvenTree.helpers import str2bool
from rest_framework.serializers import ValidationError from rest_framework.serializers import ValidationError
from rest_framework.views import APIView from rest_framework.views import APIView
@ -249,7 +248,6 @@ class StockList(generics.ListCreateAPIView):
""" """
If the query includes a particular location, If the query includes a particular location,
we may wish to also request stock items from all child locations. we may wish to also request stock items from all child locations.
This is set by the optional param 'include_child_categories'
""" """
# Does the client wish to filter by stock location? # Does the client wish to filter by stock location?
@ -261,11 +259,7 @@ class StockList(generics.ListCreateAPIView):
if loc_id: if loc_id:
try: try:
location = StockLocation.objects.get(pk=loc_id) location = StockLocation.objects.get(pk=loc_id)
if str2bool(self.request.query_params.get('include_child_locations', None)):
stock_list = stock_list.filter(location__in=location.getUniqueChildren()) stock_list = stock_list.filter(location__in=location.getUniqueChildren())
else:
stock_list = stock_list.filter(location=location.id)
except StockLocation.DoesNotExist: except StockLocation.DoesNotExist:
pass pass
@ -276,11 +270,7 @@ class StockList(generics.ListCreateAPIView):
if cat_id: if cat_id:
try: try:
category = PartCategory.objects.get(pk=cat_id) category = PartCategory.objects.get(pk=cat_id)
if str2bool(self.request.query_params.get('include_child_categories', None)):
stock_list = stock_list.filter(part__category__in=category.getUniqueChildren()) stock_list = stock_list.filter(part__category__in=category.getUniqueChildren())
else:
stock_list = stock_list.filter(category=category.id)
except PartCategory.DoesNotExist: except PartCategory.DoesNotExist:
pass pass

View File

@ -196,7 +196,6 @@
params: { params: {
{% if location %} {% if location %}
location: {{ location.id }}, location: {{ location.id }},
include_child_locations: true,
{% endif %} {% endif %}
}, },
url: "{% url 'api-stock-list' %}", url: "{% url 'api-stock-list' %}",