mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor error messaging for stock adjustment API
This commit is contained in:
parent
e04828214a
commit
51314a0261
@ -143,22 +143,36 @@ class StockAdjust(APIView):
|
|||||||
elif 'items' in request.data:
|
elif 'items' in request.data:
|
||||||
_items = request.data['items']
|
_items = request.data['items']
|
||||||
else:
|
else:
|
||||||
raise ValidationError({'items': _('Request must contain list of stock items')})
|
_items = []
|
||||||
|
|
||||||
|
if len(_items) == 0:
|
||||||
|
raise ValidationError(_('Request must contain list of stock items'))
|
||||||
|
|
||||||
# List of validated items
|
# List of validated items
|
||||||
self.items = []
|
self.items = []
|
||||||
|
|
||||||
|
# List of error messages
|
||||||
|
errors = []
|
||||||
|
|
||||||
for entry in _items:
|
for entry in _items:
|
||||||
|
|
||||||
if not type(entry) == dict:
|
if not type(entry) == dict:
|
||||||
raise ValidationError({'error': _('Improperly formatted data')})
|
raise ValidationError(_('Improperly formatted data'))
|
||||||
|
|
||||||
|
# Look for a 'pk' value (use 'id' as a backup)
|
||||||
|
pk = entry.get('pk', entry.get('id', None))
|
||||||
|
|
||||||
|
try:
|
||||||
|
pk = int(pk)
|
||||||
|
except ValueError:
|
||||||
|
raise ValidationError(_('Each entry must contain a valid integer primary-key'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Look for 'pk' value first, with 'id' as a backup
|
|
||||||
pk = entry.get('pk', entry.get('id', None))
|
|
||||||
item = StockItem.objects.get(pk=pk)
|
item = StockItem.objects.get(pk=pk)
|
||||||
except (ValueError, StockItem.DoesNotExist):
|
except (StockItem.DoesNotExist):
|
||||||
raise ValidationError({'pk': _('Each entry must contain a valid pk field')})
|
raise ValidationError({
|
||||||
|
pk: [_('Primary key does not match valid stock item')]
|
||||||
|
})
|
||||||
|
|
||||||
if self.allow_missing_quantity and 'quantity' not in entry:
|
if self.allow_missing_quantity and 'quantity' not in entry:
|
||||||
entry['quantity'] = item.quantity
|
entry['quantity'] = item.quantity
|
||||||
@ -166,16 +180,21 @@ class StockAdjust(APIView):
|
|||||||
try:
|
try:
|
||||||
quantity = Decimal(str(entry.get('quantity', None)))
|
quantity = Decimal(str(entry.get('quantity', None)))
|
||||||
except (ValueError, TypeError, InvalidOperation):
|
except (ValueError, TypeError, InvalidOperation):
|
||||||
raise ValidationError({'quantity': _("Each entry must contain a valid quantity value")})
|
raise ValidationError({
|
||||||
|
pk: [_('Invalid quantity value')]
|
||||||
|
})
|
||||||
|
|
||||||
if quantity < 0:
|
if quantity < 0:
|
||||||
raise ValidationError({'quantity': _('Quantity field must not be less than zero')})
|
raise ValidationError({
|
||||||
|
pk: [_('Quantity must not be less than zero')]
|
||||||
|
})
|
||||||
|
|
||||||
self.items.append({
|
self.items.append({
|
||||||
'item': item,
|
'item': item,
|
||||||
'quantity': quantity
|
'quantity': quantity
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# Extract 'notes' field
|
||||||
self.notes = str(request.data.get('notes', ''))
|
self.notes = str(request.data.get('notes', ''))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user