From 6d4c81e68beaab9f94d23ad4cc333fd6f11be0fa Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 4 Jan 2021 00:19:48 +1100 Subject: [PATCH] Add ability to filter by 'expired' status in API --- InvenTree/stock/api.py | 11 +++++++++++ InvenTree/stock/models.py | 3 +++ 2 files changed, 14 insertions(+) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index b74ac200f7..571ec1dd6f 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -525,6 +525,17 @@ class StockList(generics.ListCreateAPIView): # Exclude items which are instaled in another item queryset = queryset.filter(belongs_to=None) + # Filter by 'expired' status + expired = params.get('expired', None) + + if expired is not None: + expired = str2bool(expired) + + if expired: + queryset = queryset.filter(StockItem.EXPIRED_FILTER) + else: + queryset = queryset.exclude(StockItem.EXPIRED_FILTER) + # Filter by customer customer = params.get('customer', None) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 24db434d38..d2d6ec51ee 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -150,6 +150,9 @@ class StockItem(MPTTModel): status__in=StockStatus.AVAILABLE_CODES ) + # A query filter which can be used to filter StockItem objects which have expired + EXPIRED_FILTER = IN_STOCK_FILTER & ~Q(expiry_date=None) & Q(expiry_date__lt=datetime.now().date()) + def save(self, *args, **kwargs): """ Save this StockItem to the database. Performs a number of checks: