From 41c07fc42308f4ada24662ae6eb25b563aaffc07 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 24 Sep 2019 07:54:18 +1000 Subject: [PATCH] Save user who created a stock item - Handled differently for batch or serialized parts --- InvenTree/InvenTree/views.py | 2 +- InvenTree/stock/models.py | 4 +++- InvenTree/stock/views.py | 16 +++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 8a19059aa3..91261827e7 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -245,7 +245,7 @@ class AjaxCreateView(AjaxMixin, CreateView): pass def post_save(self, **kwargs): - """ + """ Hook for doing something with the created object after it is saved """ pass diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 7b5d01c58b..f2c31083c0 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -129,13 +129,15 @@ class StockItem(models.Model): else: add_note = False + user = kwargs.pop('user', None) + super(StockItem, self).save(*args, **kwargs) if add_note: # This StockItem is being saved for the first time self.addTransactionNote( 'Created stock item', - None, + user, notes="Created new stock item for part '{p}'".format(p=str(self.part)), system=True ) diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 1bb9a27a83..d8833d59d0 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -792,6 +792,8 @@ class StockItemCreate(AjaxCreateView): form = self.get_form() + data = {} + valid = form.is_valid() if valid: @@ -850,7 +852,7 @@ class StockItemCreate(AjaxCreateView): URL=data.get('URL'), ) - item.save() + item.save(user=request.user) except ValidationError as e: 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 form.clean() form._post_clean() - form.save() + + item = form.save(commit=False) + item.save(user=request.user) - data = { - 'form_valid': valid, - } + 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)