From e395d89a7fd30a2b5d0795a052a9817145b0afb0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 26 Apr 2018 09:26:43 +1000 Subject: [PATCH] Fix issues for AjaxUdpateView - Works properly now! - Can edit BOM item inline - Can add BOM item inline too --- InvenTree/InvenTree/views.py | 12 ++++--- InvenTree/part/forms.py | 5 +-- InvenTree/part/templates/part/bom.html | 32 +++++++++++++++---- .../part/templates/part/category_detail.html | 10 +++--- InvenTree/part/templates/part/index.html | 2 -- InvenTree/part/views.py | 10 ++++-- InvenTree/static/script/modal_form.js | 2 +- 7 files changed, 50 insertions(+), 23 deletions(-) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index acb4707ebc..07a09da1c4 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from django.template.loader import render_to_string from django.http import JsonResponse +from django.shortcuts import get_object_or_404 from django.views.generic import UpdateView, CreateView, DeleteView @@ -75,7 +76,7 @@ class AjaxUpdateView(AjaxView, UpdateView): def post(self, request, *args, **kwargs): if request.is_ajax(): - form = self.form_class(request.POST) + form = self.form_class(request.POST, instance=self.get_object()) data = {'form_valid': form.is_valid()} @@ -85,16 +86,17 @@ class AjaxUpdateView(AjaxView, UpdateView): return self.renderJsonResponse(request, form, data) else: - return super(UpdateView, self).post(request, *args, **kwargs) + response = super(UpdateView, self).post(request, *args, **kwargs) + return response def get(self, request, *args, **kwargs): - response = super(CreateView, self).get(request) + response = super(UpdateView, self).get(request, *args, **kwargs) if request.is_ajax(): - form = self.form_class(initial=self.get_initial()) + form = self.form_class(instance = self.get_object()) return self.renderJsonResponse(request, form) else: - return super(UpdateView, self).get(request, *args, **kwargss) + return response diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index df23f1bdcf..f4b9c11b06 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -63,10 +63,11 @@ class EditBomItemForm(forms.ModelForm): super(EditBomItemForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - self.helper.form_id = 'id-edit-part-form' + #self.helper.form_id = 'id-edit-part-form' self.helper.form_method = 'post' - self.helper.add_input(Submit('submit', 'Submit')) + self.helper.form_tag = False + #self.helper.add_input(Submit('submit', 'Submit')) class Meta: model = BomItem diff --git a/InvenTree/part/templates/part/bom.html b/InvenTree/part/templates/part/bom.html index 96653a9518..8322243a0b 100644 --- a/InvenTree/part/templates/part/bom.html +++ b/InvenTree/part/templates/part/bom.html @@ -1,9 +1,16 @@ {% extends "part/part_base.html" %} - +{% load static %} {% block details %} {% include 'part/tabs.html' with tab='bom' %} + +

Bill of Materials

@@ -18,9 +25,9 @@ - + {% endwith %} @@ -28,9 +35,7 @@
{{ sub_part.name }} {{ sub_part.description }}{{ bom_item.quantity }}{{ bom_item.quantity }} - +
- - - +
{% endblock %} @@ -39,4 +44,19 @@ + + {% endblock %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/category_detail.html b/InvenTree/part/templates/part/category_detail.html index c9e012c35c..ec87512e6b 100644 --- a/InvenTree/part/templates/part/category_detail.html +++ b/InvenTree/part/templates/part/category_detail.html @@ -54,10 +54,12 @@ diff --git a/InvenTree/part/templates/part/index.html b/InvenTree/part/templates/part/index.html index 65ac4b9edd..c4de5560c5 100644 --- a/InvenTree/part/templates/part/index.html +++ b/InvenTree/part/templates/part/index.html @@ -40,8 +40,6 @@ $(document).ready(function () { $(".js-create-cat").click(function() { launchModalForm("#modal-cat", "{% url 'category-create' %}"); }); - - //bindModalForm('#modal-cat', '.js-create-cat', "{% url 'category-create' %}"); }); diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index b7ed0c11ef..e7f515abb2 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -19,7 +19,7 @@ from .forms import EditBomItemForm from .forms import EditSupplierPartForm -from InvenTree.views import AjaxCreateView +from InvenTree.views import AjaxCreateView, AjaxUpdateView class PartIndex(ListView): model = Part @@ -168,10 +168,12 @@ class BomItemDetail(DetailView): template_name = 'part/bom-detail.html' -class BomItemCreate(CreateView): +class BomItemCreate(AjaxCreateView): model = BomItem form_class = EditBomItemForm template_name = 'part/bom-create.html' + ajax_template_name = 'modal_form.html' + ajax_form_title = 'Create BOM item' def get_initial(self): # Look for initial values @@ -186,10 +188,12 @@ class BomItemCreate(CreateView): return initials -class BomItemEdit(UpdateView): +class BomItemEdit(AjaxUpdateView): model = BomItem form_class = EditBomItemForm template_name = 'part/bom-edit.html' + ajax_template_name = 'modal_form.html' + ajax_form_title = 'Edit BOM item' class BomItemDelete(DeleteView): diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index 7834bf620d..9a85c55a63 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -26,7 +26,7 @@ function launchModalForm(modal, url, data) { var form = $(this); $.ajax({ - url: form.attr('action'), + url: url, data: form.serialize(), type: form.attr('method'), dataType: 'json',