From 659c7f3d3403864ae01ddb39ea88e0ebce136d8b Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Tue, 12 Jan 2021 10:57:21 +0100 Subject: [PATCH] more steps toward a formtools wizard. stil not really working --- evapp/templates/evapp/employee_form.html | 24 +++++++++++++++--- evapp/views.py | 32 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/evapp/templates/evapp/employee_form.html b/evapp/templates/evapp/employee_form.html index 435df50..73bd060 100644 --- a/evapp/templates/evapp/employee_form.html +++ b/evapp/templates/evapp/employee_form.html @@ -30,13 +30,31 @@

E V A - Eintritt, Veränderung, Austritt

- +

Schritt {{ wizard.steps.step1 }} von {{ wizard.steps.count }}

{% csrf_token %} - {{form}} + {% if choice %} + Du hast {{choice}} ausgewählt. + {% endif %} + {{ wizard.management_form }} + {% if wizard.form.forms %} + {{ wizard.form.management_form }} + {% for form in wizard.form.forms %} + {{ form }} + {% endfor %} + {% else %} + {{ wizard.form }} + {% endif %}
- +

+ * Pflichtfeld +

+ {% if wizard.steps.prev %} + + {% endif %} +

+

{% endblock %} diff --git a/evapp/views.py b/evapp/views.py index 98da803..5a27ea8 100644 --- a/evapp/views.py +++ b/evapp/views.py @@ -5,26 +5,28 @@ from django.urls import reverse from django.http import HttpResponse from django.core.mail import send_mail, BadHeaderError from django.template.loader import get_template +from formtools.wizard.views import CookieWizardView +from django.shortcuts import render from .models import Employee -from .forms import EmployeeForm +from .forms import EmployeeForm, PersonalForm, WorkingForm from .settings import MAILS, EVA_MAIL def success(request): return HttpResponse("gut gemacht!") -def send_mail_to_department(department, form): +def send_mail_to_department(department, data): 'send a mail to the given department with the nececcary notifications' print(f'send mail to department {department}...') - model = form.save() - context = {'data': {}} + # model = form.save() # maybe we dont need to save here and get the model instance in another way? + context = {'data': data} # add all relevant fields of the form to the template context - data = MAILS[department]['DATA'] - for key in data: - context['data'][key] = form.cleaned_data[key] + # data = MAILS[department]['DATA'] + # for key in data: + # context['data'][key] = form.cleaned_data[key] try: mail_template = get_template(f'evapp/{department}_mail.txt') @@ -42,15 +44,23 @@ def send_mail_to_department(department, form): return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!') -class EvaFormView(CreateView): +class EvaFormView(CookieWizardView): model = Employee - form_class = EmployeeForm + template_name = 'evapp/employee_form.html' + form_list = [PersonalForm, WorkingForm] + + # form_class = EmployeeForm def get_success_url(self): return reverse('success') - def form_valid(self, form): + def done(self, form_list, **kwargs): + # self.model.save() + form_data = [form.cleaned_data for form in form_list] for dep in MAILS: - send_mail_to_department(dep, form) + send_mail_to_department(dep, form_data) + return render(self.request, 'evapp/dataloop.txt', { + 'data': form_data, + }) return super().form_valid(form)