mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Further fixes for API / serializer classes
- Correctly catch and re-throw errors - Provide request to serializer context
This commit is contained in:
parent
29588ff2c5
commit
ddcfc8996c
@ -222,6 +222,7 @@ class BuildAllocate(generics.CreateAPIView):
|
|||||||
context = super().get_serializer_context()
|
context = super().get_serializer_context()
|
||||||
|
|
||||||
context['build'] = self.get_build()
|
context['build'] = self.get_build()
|
||||||
|
context['request'] = self.request
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ JSON serializers for Build API
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from django.db.models import Case, When, Value
|
from django.db.models import Case, When, Value
|
||||||
@ -270,6 +270,7 @@ class BuildAllocationSerializer(serializers.Serializer):
|
|||||||
quantity = item['quantity']
|
quantity = item['quantity']
|
||||||
output = item.get('output', None)
|
output = item.get('output', None)
|
||||||
|
|
||||||
|
try:
|
||||||
# Create a new BuildItem to allocate stock
|
# Create a new BuildItem to allocate stock
|
||||||
BuildItem.objects.create(
|
BuildItem.objects.create(
|
||||||
build=build,
|
build=build,
|
||||||
@ -278,6 +279,9 @@ class BuildAllocationSerializer(serializers.Serializer):
|
|||||||
quantity=quantity,
|
quantity=quantity,
|
||||||
install_into=output
|
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):
|
class BuildItemSerializer(InvenTreeModelSerializer):
|
||||||
|
@ -111,7 +111,7 @@ class BuildAllocationTest(BuildAPITest):
|
|||||||
expected_code=400
|
expected_code=400
|
||||||
).data
|
).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
|
# No new BuildItem objects have been created during this test
|
||||||
self.assertEqual(self.n, BuildItem.objects.count())
|
self.assertEqual(self.n, BuildItem.objects.count())
|
||||||
|
@ -7,14 +7,11 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.conf.urls import url, include
|
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 django_filters import rest_framework as rest_filters
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from rest_framework import filters, status
|
from rest_framework import filters, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import serializers
|
|
||||||
from rest_framework.serializers import ValidationError
|
from rest_framework.serializers import ValidationError
|
||||||
|
|
||||||
|
|
||||||
@ -235,6 +232,7 @@ class POReceive(generics.CreateAPIView):
|
|||||||
|
|
||||||
# Pass the purchase order through to the serializer for validation
|
# Pass the purchase order through to the serializer for validation
|
||||||
context['order'] = self.get_order()
|
context['order'] = self.get_order()
|
||||||
|
context['request'] = self.request
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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 import models, transaction
|
||||||
from django.db.models import Case, When, Value
|
from django.db.models import Case, When, Value
|
||||||
from django.db.models import BooleanField, ExpressionWrapper, F
|
from django.db.models import BooleanField, ExpressionWrapper, F
|
||||||
@ -305,6 +306,7 @@ class POReceiveSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
data = self.validated_data
|
data = self.validated_data
|
||||||
|
|
||||||
|
request = self.context['request']
|
||||||
order = self.context['order']
|
order = self.context['order']
|
||||||
|
|
||||||
items = data['items']
|
items = data['items']
|
||||||
@ -331,15 +333,19 @@ class POReceiveSerializer(serializers.Serializer):
|
|||||||
# Now we can actually receive the items into stock
|
# Now we can actually receive the items into stock
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
for item in items:
|
for item in items:
|
||||||
|
|
||||||
|
try:
|
||||||
order.receive_line_item(
|
order.receive_line_item(
|
||||||
item['line_item'],
|
item['line_item'],
|
||||||
item['location'],
|
item['location'],
|
||||||
item['quantity'],
|
item['quantity'],
|
||||||
self.request.user,
|
request.user,
|
||||||
status=item['status'],
|
status=item['status'],
|
||||||
barcode=item.get('barcode', ''),
|
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:
|
class Meta:
|
||||||
fields = [
|
fields = [
|
||||||
|
Loading…
Reference in New Issue
Block a user