Save user who created a stock item

- Handled differently for batch or serialized parts
This commit is contained in:
Oliver Walters 2019-09-24 07:54:18 +10:00
parent 52ec213a28
commit 41c07fc423
3 changed files with 15 additions and 7 deletions

View File

@ -129,13 +129,15 @@ class StockItem(models.Model):
else: else:
add_note = False add_note = False
user = kwargs.pop('user', None)
super(StockItem, self).save(*args, **kwargs) super(StockItem, self).save(*args, **kwargs)
if add_note: if add_note:
# This StockItem is being saved for the first time # This StockItem is being saved for the first time
self.addTransactionNote( self.addTransactionNote(
'Created stock item', 'Created stock item',
None, user,
notes="Created new stock item for part '{p}'".format(p=str(self.part)), notes="Created new stock item for part '{p}'".format(p=str(self.part)),
system=True system=True
) )

View File

@ -792,6 +792,8 @@ class StockItemCreate(AjaxCreateView):
form = self.get_form() form = self.get_form()
data = {}
valid = form.is_valid() valid = form.is_valid()
if valid: if valid:
@ -850,7 +852,7 @@ class StockItemCreate(AjaxCreateView):
URL=data.get('URL'), URL=data.get('URL'),
) )
item.save() item.save(user=request.user)
except ValidationError as e: except ValidationError as e:
form.errors['serial_numbers'] = e.messages form.errors['serial_numbers'] = e.messages
@ -861,11 +863,15 @@ class StockItemCreate(AjaxCreateView):
# We need to call _post_clean() here because it is prevented in the form implementation # We need to call _post_clean() here because it is prevented in the form implementation
form.clean() form.clean()
form._post_clean() form._post_clean()
form.save()
data = { item = form.save(commit=False)
'form_valid': valid, item.save(user=request.user)
}
data['pk'] = item.pk
data['url'] = item.get_absolute_url()
data['success'] = _("Created new stock item")
data['form_valid'] = valid
return self.renderJsonResponse(request, form, data=data) return self.renderJsonResponse(request, form, data=data)