mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Auto-populate expiry date for stockitem when created via the API
(Now with unit testing!)
This commit is contained in:
parent
da02ab3eac
commit
9dc9c0fcb7
@ -35,6 +35,8 @@ from InvenTree.api import AttachmentMixin
|
|||||||
|
|
||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from rest_framework.serializers import ValidationError
|
from rest_framework.serializers import ValidationError
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@ -342,10 +344,18 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
# A location was *not* specified - try to infer it
|
# A location was *not* specified - try to infer it
|
||||||
if 'location' not in request.data:
|
if 'location' not in request.data:
|
||||||
location = item.part.get_default_location()
|
location = item.part.get_default_location()
|
||||||
|
|
||||||
if location is not None:
|
if location is not None:
|
||||||
item.location = location
|
item.location = location
|
||||||
item.save()
|
item.save()
|
||||||
|
|
||||||
|
# An expiry date was *not* specified - try to infer it!
|
||||||
|
if 'expiry_date' not in request.data:
|
||||||
|
|
||||||
|
if item.part.default_expiry > 0:
|
||||||
|
item.expiry_date = datetime.now().date() + timedelta(days=item.part.default_expiry)
|
||||||
|
item.save()
|
||||||
|
|
||||||
# Return a response
|
# Return a response
|
||||||
headers = self.get_success_headers(serializer.data)
|
headers = self.get_success_headers(serializer.data)
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||||
|
@ -70,9 +70,10 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
|
|||||||
{% block page_data %}
|
{% block page_data %}
|
||||||
<h3>
|
<h3>
|
||||||
{% trans "Stock Item" %}
|
{% trans "Stock Item" %}
|
||||||
{% stock_status_label item.status large=True %}
|
|
||||||
{% if item.is_expired %}
|
{% if item.is_expired %}
|
||||||
<span class='label label-large label-large-red'>{% trans "Expired" %}</span>
|
<span class='label label-large label-large-red'>{% trans "Expired" %}</span>
|
||||||
|
{% else %}
|
||||||
|
{% stock_status_label item.status large=True %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</h3>
|
</h3>
|
||||||
<hr>
|
<hr>
|
||||||
|
@ -368,6 +368,56 @@ class StockItemTest(StockAPITestCase):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
def test_default_expiry(self):
|
||||||
|
"""
|
||||||
|
Test that the "default_expiry" functionality works via the API.
|
||||||
|
|
||||||
|
- If an expiry_date is specified, use that
|
||||||
|
- Otherwise, check if the referenced part has a default_expiry defined
|
||||||
|
- If so, use that!
|
||||||
|
- Otherwise, no expiry
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- Part <25> has a default_expiry of 10 days
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# First test - create a new StockItem without an expiry date
|
||||||
|
data = {
|
||||||
|
'part': 4,
|
||||||
|
'quantity': 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
response = self.client.post(self.list_url, data)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
self.assertIsNone(response.data['expiry_date'])
|
||||||
|
|
||||||
|
# Second test - create a new StockItem with an explicit expiry date
|
||||||
|
data['expiry_date'] = '2022-12-12'
|
||||||
|
|
||||||
|
response = self.client.post(self.list_url, data)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
self.assertIsNotNone(response.data['expiry_date'])
|
||||||
|
self.assertEqual(response.data['expiry_date'], '2022-12-12')
|
||||||
|
|
||||||
|
# Third test - create a new StockItem for a Part which has a default expiry time
|
||||||
|
data = {
|
||||||
|
'part': 25,
|
||||||
|
'quantity': 10
|
||||||
|
}
|
||||||
|
|
||||||
|
response = self.client.post(self.list_url, data)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
# Expected expiry date is 10 days in the future
|
||||||
|
expiry = datetime.now().date() + timedelta(10)
|
||||||
|
|
||||||
|
self.assertEqual(response.data['expiry_date'], expiry.isoformat())
|
||||||
|
|
||||||
|
|
||||||
class StocktakeTest(StockAPITestCase):
|
class StocktakeTest(StockAPITestCase):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user