2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-12 18:15:40 +00:00

Add "updated_before" and "updated_after" filter for stock API

This commit is contained in:
Oliver Walters
2021-01-15 16:39:50 +11:00
parent ef3ac43c4a
commit fc32d99327

View File

@ -644,7 +644,7 @@ class StockList(generics.ListCreateAPIView):
queryset = queryset.filter(Q(sales_order_allocations__isnull=True) & Q(allocations__isnull=True)) queryset = queryset.filter(Q(sales_order_allocations__isnull=True) & Q(allocations__isnull=True))
# Do we wish to filter by "active parts" # Do we wish to filter by "active parts"
active = self.request.query_params.get('active', None) active = params.get('active', None)
if active is not None: if active is not None:
active = str2bool(active) active = str2bool(active)
@ -683,7 +683,7 @@ class StockList(generics.ListCreateAPIView):
raise ValidationError({"part": "Invalid Part ID specified"}) raise ValidationError({"part": "Invalid Part ID specified"})
# Does the client wish to filter by the 'ancestor'? # Does the client wish to filter by the 'ancestor'?
anc_id = self.request.query_params.get('ancestor', None) anc_id = params.get('ancestor', None)
if anc_id: if anc_id:
try: try:
@ -696,9 +696,9 @@ class StockList(generics.ListCreateAPIView):
raise ValidationError({"ancestor": "Invalid ancestor ID specified"}) raise ValidationError({"ancestor": "Invalid ancestor ID specified"})
# Does the client wish to filter by stock location? # Does the client wish to filter by stock location?
loc_id = self.request.query_params.get('location', None) loc_id = params.get('location', None)
cascade = str2bool(self.request.query_params.get('cascade', True)) cascade = str2bool(params.get('cascade', True))
if loc_id is not None: if loc_id is not None:
@ -718,7 +718,7 @@ class StockList(generics.ListCreateAPIView):
pass pass
# Does the client wish to filter by part category? # Does the client wish to filter by part category?
cat_id = self.request.query_params.get('category', None) cat_id = params.get('category', None)
if cat_id: if cat_id:
try: try:
@ -729,35 +729,68 @@ class StockList(generics.ListCreateAPIView):
raise ValidationError({"category": "Invalid category id specified"}) raise ValidationError({"category": "Invalid category id specified"})
# Filter by StockItem status # Filter by StockItem status
status = self.request.query_params.get('status', None) status = params.get('status', None)
if status: if status:
queryset = queryset.filter(status=status) queryset = queryset.filter(status=status)
# Filter by supplier_part ID # Filter by supplier_part ID
supplier_part_id = self.request.query_params.get('supplier_part', None) supplier_part_id = params.get('supplier_part', None)
if supplier_part_id: if supplier_part_id:
queryset = queryset.filter(supplier_part=supplier_part_id) queryset = queryset.filter(supplier_part=supplier_part_id)
# Filter by company (either manufacturer or supplier) # Filter by company (either manufacturer or supplier)
company = self.request.query_params.get('company', None) company = params.get('company', None)
if company is not None: if company is not None:
queryset = queryset.filter(Q(supplier_part__supplier=company) | Q(supplier_part__manufacturer=company)) queryset = queryset.filter(Q(supplier_part__supplier=company) | Q(supplier_part__manufacturer=company))
# Filter by supplier # Filter by supplier
supplier = self.request.query_params.get('supplier', None) supplier = params.get('supplier', None)
if supplier is not None: if supplier is not None:
queryset = queryset.filter(supplier_part__supplier=supplier) queryset = queryset.filter(supplier_part__supplier=supplier)
# Filter by manufacturer # Filter by manufacturer
manufacturer = self.request.query_params.get('manufacturer', None) manufacturer = params.get('manufacturer', None)
if manufacturer is not None: if manufacturer is not None:
queryset = queryset.filter(supplier_part__manufacturer=manufacturer) queryset = queryset.filter(supplier_part__manufacturer=manufacturer)
"""
Filter by the 'last updated' date of the stock item(s):
- updated_before=? : Filter stock items which were last updated *before* the provided date
- updated_after=? : Filter stock items which were last updated *after* the provided date
"""
date_fmt = '%Y-%m-%d' # ISO format date string
updated_before = params.get('updated_before', None)
updated_after = params.get('updated_after', None)
if updated_before:
try:
updated_before = datetime.strptime(str(updated_before), date_fmt).date()
queryset = queryset.filter(updated__lte=updated_before)
print("Before:", updated_before.isoformat())
except (ValueError, TypeError):
# Account for improperly formatted date string
print("After before:", str(updated_before))
pass
if updated_after:
try:
updated_after = datetime.strptime(str(updated_after), date_fmt).date()
queryset = queryset.filter(updated__gte=updated_after)
print("After:", updated_after.isoformat())
except (ValueError, TypeError):
# Account for improperly formatted date string
print("After error:", str(updated_after))
pass
# Also ensure that we pre-fecth all the related items # Also ensure that we pre-fecth all the related items
queryset = queryset.prefetch_related( queryset = queryset.prefetch_related(
'part', 'part',