From 8bc4050d05630c5a8061f687c42d4c531ff207e3 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 25 Apr 2018 12:46:58 +1000 Subject: [PATCH] Modal form is working for 'create new category' - Had to set form.helper.form_tag to False (so we could control form tags manually) - Created a 'json' model view --- InvenTree/part/forms.py | 5 +-- InvenTree/part/templates/part/index.html | 29 +++++++++++++++--- InvenTree/part/urls.py | 2 +- InvenTree/part/views.py | 39 ++++++++++++++++++++++++ InvenTree/templates/modal_form.html | 33 ++++++++++---------- 5 files changed, 84 insertions(+), 24 deletions(-) diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 2a66103168..df23f1bdcf 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -42,9 +42,10 @@ class EditCategoryForm(forms.ModelForm): super(EditCategoryForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - self.helper.form_id = 'id-edit-part-form' - self.helper.form_method = 'post' + #self.helper.form_id = 'id-edit-part-form' + #self.helper.form_method = 'post' + self.helper.form_tag = False #self.helper.add_input(Submit('submit', 'Submit')) class Meta: diff --git a/InvenTree/part/templates/part/index.html b/InvenTree/part/templates/part/index.html index bc912f346b..d0bda70df7 100644 --- a/InvenTree/part/templates/part/index.html +++ b/InvenTree/part/templates/part/index.html @@ -38,21 +38,42 @@ {% block javascript %} diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index 31129ad529..51ee0736a4 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -48,7 +48,7 @@ part_bom_urls = [ part_urls = [ # Create a new category - url(r'^category/new/?', views.CategoryCreate.as_view(), name='category-create'), + url(r'^category/new/?', views.CategoryCreateJson.as_view(), name='category-create'), # Create a new part url(r'^new/?', views.PartCreate.as_view(), name='part-create'), diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 9b3ac4df09..fa571d343c 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +from django.template.loader import render_to_string + from django.shortcuts import get_object_or_404 from django.http import HttpResponseRedirect +from django.http import JsonResponse from django.views.generic import DetailView, ListView from django.views.generic.edit import UpdateView, DeleteView, CreateView @@ -130,6 +133,42 @@ class CategoryDelete(DeleteView): return HttpResponseRedirect(self.get_object().get_absolute_url()) +class CategoryCreateJson(CreateView): + model = PartCategory + template_name = 'part/partial_category_new.html' + form_class = EditCategoryForm + + def renderJsonResponse(self, request, form, data): + + context = {'form': form} + + data['html_form'] = render_to_string(self.template_name, + context, + request=request) + + return JsonResponse(data) + + def post(self, request): + form = self.form_class(request.POST) + + data = {} + + if form.is_valid(): + form.save() + data['form_valid'] = True + else: + data['form_valid'] = False + + return self.renderJsonResponse(request, form, data) + + def get(self, request, *args, **kwargs): + form = self.form_class() + + data = {} + + return self.renderJsonResponse(request, form, data) + + class CategoryCreate(CreateView): model = PartCategory template_name = 'part/partial_category_new.html' diff --git a/InvenTree/templates/modal_form.html b/InvenTree/templates/modal_form.html index bdc1529170..d5d4a8bad5 100644 --- a/InvenTree/templates/modal_form.html +++ b/InvenTree/templates/modal_form.html @@ -1,18 +1,17 @@ -{% load static %} - -
- {% csrf_token %} - - - + + {% csrf_token %} + + +
\ No newline at end of file