Correctly handle serialization of newly created stock

This commit is contained in:
Oliver 2021-11-04 23:18:59 +11:00
parent 3be4acf3ef
commit b41dbba2b0
2 changed files with 26 additions and 1 deletions

View File

@ -445,8 +445,14 @@ class StockList(generics.ListCreateAPIView):
# Finally, save the item (with user information) # Finally, save the item (with user information)
item.save(user=user) item.save(user=user)
# Serialize the stock, if required
if serials: if serials:
"""
Serialize the stock, if required
- Note that the "original" stock item needs to be created first, so it can be serialized
- It is then immediately deleted
"""
try: try:
item.serializeStock( item.serializeStock(
quantity, quantity,
@ -455,6 +461,19 @@ class StockList(generics.ListCreateAPIView):
notes=notes, notes=notes,
location=item.location, location=item.location,
) )
headers = self.get_success_headers(serializer.data)
# Delete the original item
item.delete()
response_data = {
'quantity': quantity,
'serial_numbers': serials,
}
return Response(response_data, status=status.HTTP_201_CREATED, headers=headers)
except DjangoValidationError as e: except DjangoValidationError as e:
raise ValidationError({ raise ValidationError({
'quantity': e.messages, 'quantity': e.messages,

View File

@ -115,6 +115,10 @@ function canDelete(OPTIONS) {
*/ */
function getApiEndpointOptions(url, callback) { function getApiEndpointOptions(url, callback) {
if (!url) {
return;
}
// Return the ajax request object // Return the ajax request object
$.ajax({ $.ajax({
url: url, url: url,
@ -727,6 +731,8 @@ function submitFormData(fields, options) {
break; break;
default: default:
$(options.modal).modal('hide'); $(options.modal).modal('hide');
console.log(`upload error at ${options.url}`);
showApiError(xhr, options.url); showApiError(xhr, options.url);
break; break;
} }