From 55e7f365df75489d562b2f141f0b595906ff98a0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 26 Apr 2018 08:18:53 +1000 Subject: [PATCH] Added AjaxUpdateView class Also cleaned up the modal form javascript --- InvenTree/InvenTree/views.py | 38 ++++++++++++++++++--- InvenTree/part/templates/part/bom.html | 10 +++++- InvenTree/part/templates/part/index.html | 6 +++- InvenTree/static/script/modal_form.js | 43 +++++++++++------------- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index aea55a598a..acb4707ebc 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -39,7 +39,7 @@ class AjaxView(object): class AjaxCreateView(AjaxView, CreateView): - def post(self, request): + def post(self, request, *args, **kwargs): if request.is_ajax(): form = self.form_class(request.POST) @@ -55,9 +55,39 @@ class AjaxCreateView(AjaxView, CreateView): return self.renderJsonResponse(request, form, data) else: - return super(CreateView, self).post(request) + return super(CreateView, self).post(request, *args, **kwargs) - def get(self, request): + def get(self, request, *args, **kwargs): + + response = super(CreateView, self).get(request, *args, **kwargs) + + if request.is_ajax(): + form = self.form_class(initial=self.get_initial()) + + return self.renderJsonResponse(request, form) + + else: + return response + + +class AjaxUpdateView(AjaxView, UpdateView): + + def post(self, request, *args, **kwargs): + + if request.is_ajax(): + form = self.form_class(request.POST) + + data = {'form_valid': form.is_valid()} + + if form.is_valid(): + obj = form.save() + + return self.renderJsonResponse(request, form, data) + + else: + return super(UpdateView, self).post(request, *args, **kwargs) + + def get(self, request, *args, **kwargs): response = super(CreateView, self).get(request) @@ -67,4 +97,4 @@ class AjaxCreateView(AjaxView, CreateView): return self.renderJsonResponse(request, form) else: - return response + return super(UpdateView, self).get(request, *args, **kwargss) diff --git a/InvenTree/part/templates/part/bom.html b/InvenTree/part/templates/part/bom.html index 0e9e28b6b1..96653a9518 100644 --- a/InvenTree/part/templates/part/bom.html +++ b/InvenTree/part/templates/part/bom.html @@ -19,7 +19,9 @@ {{ sub_part.name }} {{ sub_part.description }} {{ bom_item.quantity }} - Edit + + + {% endwith %} {% endfor %} @@ -31,4 +33,10 @@ +{% endblock %} + +{% block javascript %} + + + {% endblock %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/index.html b/InvenTree/part/templates/part/index.html index 9881c0079b..65ac4b9edd 100644 --- a/InvenTree/part/templates/part/index.html +++ b/InvenTree/part/templates/part/index.html @@ -37,7 +37,11 @@ diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index cc1f609b3a..7834bf620d 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -1,28 +1,25 @@ -/* Bind a button to launch a modal form and handle AJAX requests */ -function bindModalForm(modal, button, url, data) { - $(button).click(function () { - $.ajax({ - url: url, // Where to request the data from - type: 'get', // GET request - data: data, // Any additional context data (e.g. initial values) - dataType: 'json', - beforeSend: function() { - $(modal).modal('show'); - }, - success: function(response) { - if (response.html_form) { - var target = modal + ' .modal-content'; - $(target).html(response.html_form); - } else { - alert('JSON response missing form data'); - $(modal).modal('hide'); - } - }, - error: function (xhr, ajaxOptions, thrownError) { - alert('Error requesting form data:\n' + thrownError); +function launchModalForm(modal, url, data) { + $.ajax({ + url: url, // Where to request the data from + type: 'get', // GET request + data: data, // Any additional context data (e.g. initial values) + dataType: 'json', + beforeSend: function() { + $(modal).modal('show'); + }, + success: function(response) { + if (response.html_form) { + var target = modal + ' .modal-content'; + $(target).html(response.html_form); + } else { + alert('JSON response missing form data'); $(modal).modal('hide'); } - }); + }, + error: function (xhr, ajaxOptions, thrownError) { + alert('Error requesting form data:\n' + thrownError); + $(modal).modal('hide'); + } }); $(modal).on('submit', '.js-modal-form', function() {