Further fixes for API / serializer classes

- Correctly catch and re-throw errors
- Provide request to serializer context
This commit is contained in:
Oliver 2021-10-05 12:12:37 +11:00
parent 29588ff2c5
commit ddcfc8996c
5 changed files with 30 additions and 21 deletions

View File

@ -222,6 +222,7 @@ class BuildAllocate(generics.CreateAPIView):
context = super().get_serializer_context()
context['build'] = self.get_build()
context['request'] = self.request
return context

View File

@ -6,7 +6,7 @@ JSON serializers for Build API
from __future__ import unicode_literals
from django.db import transaction
from django.core.exceptions import ValidationError as DjangoValidationError
from django.utils.translation import ugettext_lazy as _
from django.db.models import Case, When, Value
@ -270,14 +270,18 @@ class BuildAllocationSerializer(serializers.Serializer):
quantity = item['quantity']
output = item.get('output', None)
# Create a new BuildItem to allocate stock
BuildItem.objects.create(
build=build,
bom_item=bom_item,
stock_item=stock_item,
quantity=quantity,
install_into=output
)
try:
# Create a new BuildItem to allocate stock
BuildItem.objects.create(
build=build,
bom_item=bom_item,
stock_item=stock_item,
quantity=quantity,
install_into=output
)
except (ValidationError, DjangoValidationError) as exc:
# Catch model errors and re-throw as DRF errors
raise ValidationError(detail=serializers.as_serializer_error(exc))
class BuildItemSerializer(InvenTreeModelSerializer):

View File

@ -111,7 +111,7 @@ class BuildAllocationTest(BuildAPITest):
expected_code=400
).data
self.assertIn('Allocation items must be provided', str(data['items']))
self.assertIn('Allocation items must be provided', str(data))
# No new BuildItem objects have been created during this test
self.assertEqual(self.n, BuildItem.objects.count())

View File

@ -7,14 +7,11 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.conf.urls import url, include
from django.db import transaction
from django.core.exceptions import ValidationError as DjangoValidationError
from django_filters import rest_framework as rest_filters
from rest_framework import generics
from rest_framework import filters, status
from rest_framework.response import Response
from rest_framework import serializers
from rest_framework.serializers import ValidationError
@ -235,6 +232,7 @@ class POReceive(generics.CreateAPIView):
# Pass the purchase order through to the serializer for validation
context['order'] = self.get_order()
context['request'] = self.request
return context

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import models, transaction
from django.db.models import Case, When, Value
from django.db.models import BooleanField, ExpressionWrapper, F
@ -305,6 +306,7 @@ class POReceiveSerializer(serializers.Serializer):
data = self.validated_data
request = self.context['request']
order = self.context['order']
items = data['items']
@ -331,15 +333,19 @@ class POReceiveSerializer(serializers.Serializer):
# Now we can actually receive the items into stock
with transaction.atomic():
for item in items:
order.receive_line_item(
item['line_item'],
item['location'],
item['quantity'],
self.request.user,
status=item['status'],
barcode=item.get('barcode', ''),
)
try:
order.receive_line_item(
item['line_item'],
item['location'],
item['quantity'],
request.user,
status=item['status'],
barcode=item.get('barcode', ''),
)
except (ValidationError, DjangoValidationError) as exc:
# Catch model errors and re-throw as DRF errors
raise ValidationError(detail=serializers.as_serializer_error(exc))
class Meta:
fields = [