mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
Add API endpoint for validating a BOM item
This commit is contained in:
parent
81f5714cb1
commit
37d9c59a0e
@ -12,7 +12,7 @@ from django.db.models import Sum
|
|||||||
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import filters
|
from rest_framework import filters, serializers
|
||||||
from rest_framework import generics, permissions
|
from rest_framework import generics, permissions
|
||||||
|
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
@ -303,7 +303,7 @@ class BomList(generics.ListCreateAPIView):
|
|||||||
|
|
||||||
filter_fields = [
|
filter_fields = [
|
||||||
'part',
|
'part',
|
||||||
'sub_part'
|
'sub_part',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -318,6 +318,35 @@ class BomDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class BomItemValidate(generics.UpdateAPIView):
|
||||||
|
""" API endpoint for validating a BomItem """
|
||||||
|
|
||||||
|
# Very simple serializers
|
||||||
|
class BomItemValidationSerializer(serializers.Serializer):
|
||||||
|
|
||||||
|
valid = serializers.BooleanField(default=False)
|
||||||
|
|
||||||
|
queryset = BomItem.objects.all()
|
||||||
|
serializer_class = BomItemValidationSerializer
|
||||||
|
|
||||||
|
def update(self, request, *args, **kwargs):
|
||||||
|
""" Perform update request """
|
||||||
|
|
||||||
|
partial = kwargs.pop('partial', False)
|
||||||
|
|
||||||
|
valid = request.data.get('valid', False)
|
||||||
|
|
||||||
|
instance = self.get_object()
|
||||||
|
|
||||||
|
serializer = self.get_serializer(instance, data=request.data, partial=partial)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
|
||||||
|
if type(instance) == BomItem:
|
||||||
|
instance.validate_hash(valid)
|
||||||
|
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
cat_api_urls = [
|
cat_api_urls = [
|
||||||
|
|
||||||
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
|
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
|
||||||
@ -345,10 +374,16 @@ part_api_urls = [
|
|||||||
url(r'^.*$', PartList.as_view(), name='api-part-list'),
|
url(r'^.*$', PartList.as_view(), name='api-part-list'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
bom_item_urls = [
|
||||||
|
|
||||||
|
url(r'^validate/?', BomItemValidate.as_view(), name='api-bom-item-validate'),
|
||||||
|
|
||||||
|
url(r'^.*$', BomDetail.as_view(), name='api-bom-item-detail'),
|
||||||
|
]
|
||||||
|
|
||||||
bom_api_urls = [
|
bom_api_urls = [
|
||||||
# BOM Item Detail
|
# BOM Item Detail
|
||||||
url(r'^(?P<pk>\d+)/?', BomDetail.as_view(), name='api-bom-detail'),
|
url(r'^(?P<pk>\d+)/', include(bom_item_urls)),
|
||||||
|
|
||||||
# Catch-all
|
# Catch-all
|
||||||
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
||||||
|
@ -1176,16 +1176,28 @@ class BomItem(models.Model):
|
|||||||
|
|
||||||
return str(hash.digest())
|
return str(hash.digest())
|
||||||
|
|
||||||
def validate_hash(self):
|
def validate_hash(self, valid=True):
|
||||||
""" Mark this item as 'valid' (store the checksum hash) """
|
""" Mark this item as 'valid' (store the checksum hash).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
valid: If true, validate the hash, otherwise invalidate it (default = True)
|
||||||
|
"""
|
||||||
|
|
||||||
|
if valid:
|
||||||
|
self.checksum = str(self.get_item_hash())
|
||||||
|
else:
|
||||||
|
self.checksum = ''
|
||||||
|
|
||||||
self.checksum = str(self.get_item_hash())
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_line_valid(self):
|
def is_line_valid(self):
|
||||||
""" Check if this line item has been validated by the user """
|
""" Check if this line item has been validated by the user """
|
||||||
|
|
||||||
|
# Ensure an empty checksum returns False
|
||||||
|
if len(self.checksum) == 0:
|
||||||
|
return False
|
||||||
|
|
||||||
return self.get_item_hash() == self.checksum
|
return self.get_item_hash() == self.checksum
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
@ -120,7 +120,7 @@ class PartAPITest(APITestCase):
|
|||||||
|
|
||||||
def test_get_bom_detail(self):
|
def test_get_bom_detail(self):
|
||||||
# Get the detail for a single BomItem
|
# Get the detail for a single BomItem
|
||||||
url = reverse('api-bom-detail', kwargs={'pk': 3})
|
url = reverse('api-bom-item-detail', kwargs={'pk': 3})
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(response.data['quantity'], 25)
|
self.assertEqual(response.data['quantity'], 25)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user