diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index ad0d70bfe8..8a19059aa3 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -238,6 +238,18 @@ class AjaxCreateView(AjaxMixin, CreateView): - Handles form validation via AJAX POST requests """ + def pre_save(self, **kwargs): + """ + Hook for doing something before the form is validated + """ + pass + + def post_save(self, **kwargs): + """ + Hook for doing something with the created object after it is saved + """ + pass + def get(self, request, *args, **kwargs): """ Creates form with initial data, and renders JSON response """ @@ -255,26 +267,29 @@ class AjaxCreateView(AjaxMixin, CreateView): - Return status info (success / failure) """ self.request = request - form = self.get_form() + self.form = self.get_form() # Extra JSON data sent alongside form data = { - 'form_valid': form.is_valid(), + 'form_valid': self.form.is_valid(), } - if form.is_valid(): - obj = form.save() + if self.form.is_valid(): + + self.pre_save() + self.object = self.form.save() + self.post_save() # Return the PK of the newly-created object - data['pk'] = obj.pk - data['text'] = str(obj) + data['pk'] = self.object.pk + data['text'] = str(object) try: - data['url'] = obj.get_absolute_url() + data['url'] = self.object.get_absolute_url() except AttributeError: pass - return self.renderJsonResponse(request, form, data) + return self.renderJsonResponse(request, self.form, data) class AjaxUpdateView(AjaxMixin, UpdateView): diff --git a/InvenTree/order/views.py b/InvenTree/order/views.py index 01ad682e6a..d78c9c059e 100644 --- a/InvenTree/order/views.py +++ b/InvenTree/order/views.py @@ -91,6 +91,12 @@ class PurchaseOrderCreate(AjaxCreateView): return initials + def post_save(self, **kwargs): + # Record the user who created this purchase order + + self.object.created_by = self.request.user + self.object.save() + class PurchaseOrderEdit(AjaxUpdateView): """ View for editing a PurchaseOrder using a modal form """