Save user information when creating a new purchase order

This commit is contained in:
Oliver Walters 2019-09-24 07:43:14 +10:00
parent cc1e580538
commit 52ec213a28
2 changed files with 29 additions and 8 deletions

View File

@ -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):

View File

@ -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 """