mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 12:35:46 +00:00
Further fixes for API / serializer classes
- Correctly catch and re-throw errors - Provide request to serializer context
This commit is contained in:
@ -222,6 +222,7 @@ class BuildAllocate(generics.CreateAPIView):
|
||||
context = super().get_serializer_context()
|
||||
|
||||
context['build'] = self.get_build()
|
||||
context['request'] = self.request
|
||||
|
||||
return context
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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())
|
||||
|
Reference in New Issue
Block a user