mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 11:10:54 +00:00
Allow API filtering by "cascading" stock locations
This commit is contained in:
@ -289,20 +289,35 @@ class StockLocationList(generics.ListCreateAPIView):
|
||||
|
||||
queryset = super().get_queryset()
|
||||
|
||||
loc_id = self.request.query_params.get('parent', None)
|
||||
params = self.request.query_params
|
||||
|
||||
if loc_id is not None:
|
||||
cascade = str2bool(params.get('cascade', False))
|
||||
|
||||
# Look for top-level locations
|
||||
if isNull(loc_id):
|
||||
loc_id = params.get('parent', None)
|
||||
|
||||
# Look for top-level locations
|
||||
if isNull(loc_id):
|
||||
|
||||
# If we allow "cascade" at the top-level, this essentially means *all* locations
|
||||
if not cascade:
|
||||
queryset = queryset.filter(parent=None)
|
||||
|
||||
else:
|
||||
try:
|
||||
loc_id = int(loc_id)
|
||||
queryset = queryset.filter(parent=loc_id)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
else:
|
||||
|
||||
try:
|
||||
location = StockLocation.objects.get(pk=loc_id)
|
||||
|
||||
# All sub-locations to be returned too?
|
||||
if cascade:
|
||||
parents = location.get_descendants(include_self=True)
|
||||
parent_ids = [p.id for p in parents]
|
||||
queryset = queryset.filter(parent__in=parent_ids)
|
||||
|
||||
else:
|
||||
queryset = queryset.filter(parent=location)
|
||||
|
||||
except (ValueError, StockLocation.DoesNotExist):
|
||||
pass
|
||||
|
||||
return queryset
|
||||
|
||||
|
@ -15,7 +15,16 @@
|
||||
<div class='panel-heading'>
|
||||
<h4>{% trans "Sublocations" %}</h4>
|
||||
</div>
|
||||
<table class='table table-striped table-condensed' id='sublocation-table'></table>
|
||||
|
||||
<div id='button-toolbar'>
|
||||
<div class='button-toolbar container-fluid' style='float: right;'>
|
||||
|
||||
</div>
|
||||
<div class='filter-list' id='filter-list-location'>
|
||||
<!-- An empty div in which the filter list will be constructed -->
|
||||
</div>
|
||||
</div>
|
||||
<table class='table table-striped table-condensed' data-toolbar='#button-toolbar' id='sublocation-table'></table>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
Reference in New Issue
Block a user