2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-14 17:01:28 +00:00

Add 'available' filter for BuildLine API endpoint (#5186)

* Add 'available' filter for BuildLine API endpoint

- Fixes missing filter
- Closes https://github.com/inventree/InvenTree/issues/1839

* Bump API version
This commit is contained in:
Oliver
2023-07-06 12:18:26 +10:00
committed by GitHub
parent 9abcc0ec34
commit dd4f5d4630
3 changed files with 34 additions and 2 deletions

View File

@ -1,6 +1,6 @@
"""JSON API for the Build app."""
from django.db.models import F
from django.db.models import F, Q
from django.urls import include, path, re_path
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User
@ -297,6 +297,25 @@ class BuildLineFilter(rest_filters.FilterSet):
else:
return queryset.filter(allocated__lt=F('quantity'))
available = rest_filters.BooleanFilter(label=_('Available'), method='filter_available')
def filter_available(self, queryset, name, value):
"""Filter by whether there is sufficient stock available for each BuildLine:
To determine this, we need to know:
- The quantity required for each BuildLine
- The quantity available for each BuildLine
- The quantity allocated for each BuildLine
"""
flt = Q(quantity__lte=F('total_available_stock') + F('allocated'))
if str2bool(value):
return queryset.filter(flt)
else:
return queryset.exclude(flt)
class BuildLineEndpoint:
"""Mixin class for BuildLine API endpoints."""