2021-01-06 09:59:47 +00:00
|
|
|
from smtplib import SMTPException
|
|
|
|
|
2020-12-23 14:08:27 +00:00
|
|
|
from django.views.generic.edit import CreateView
|
2021-01-05 13:10:28 +00:00
|
|
|
from django.urls import reverse
|
2021-01-14 08:37:45 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2021-01-06 09:59:47 +00:00
|
|
|
from django.core.mail import send_mail, BadHeaderError
|
|
|
|
from django.template.loader import get_template
|
2021-01-12 09:57:21 +00:00
|
|
|
from formtools.wizard.views import CookieWizardView
|
|
|
|
from django.shortcuts import render
|
2020-12-23 14:08:27 +00:00
|
|
|
|
|
|
|
from .models import Employee
|
2021-01-13 14:21:22 +00:00
|
|
|
from .forms import PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm
|
2021-01-06 09:59:47 +00:00
|
|
|
from .settings import MAILS, EVA_MAIL
|
2020-12-23 10:42:57 +00:00
|
|
|
|
2021-01-05 13:10:28 +00:00
|
|
|
def success(request):
|
|
|
|
return HttpResponse("gut gemacht!")
|
|
|
|
|
2021-01-12 09:57:21 +00:00
|
|
|
def send_mail_to_department(department, data):
|
2021-01-06 09:20:06 +00:00
|
|
|
'send a mail to the given department with the nececcary notifications'
|
|
|
|
|
2021-01-06 09:59:47 +00:00
|
|
|
print(f'send mail to department {department}...')
|
|
|
|
|
2021-01-12 09:57:21 +00:00
|
|
|
# model = form.save() # maybe we dont need to save here and get the model instance in another way?
|
|
|
|
context = {'data': data}
|
2021-01-06 10:20:11 +00:00
|
|
|
|
|
|
|
# add all relevant fields of the form to the template context
|
2021-01-12 09:57:21 +00:00
|
|
|
# data = MAILS[department]['DATA']
|
|
|
|
# for key in data:
|
|
|
|
# context['data'][key] = form.cleaned_data[key]
|
2021-01-06 10:20:11 +00:00
|
|
|
|
2021-01-06 09:59:47 +00:00
|
|
|
try:
|
|
|
|
mail_template = get_template(f'evapp/{department}_mail.txt')
|
|
|
|
send_mail(
|
|
|
|
'EVA: Neuzugang',
|
|
|
|
mail_template.render(context),
|
|
|
|
EVA_MAIL,
|
|
|
|
[MAILS[department]['MAIL']],
|
|
|
|
fail_silently=False)
|
|
|
|
except BadHeaderError:
|
|
|
|
# modell.delete()
|
|
|
|
return HttpResponse('Invalid header found. Data not saved!')
|
|
|
|
except SMTPException:
|
|
|
|
# modell.delete()
|
|
|
|
return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!')
|
2021-01-06 09:20:06 +00:00
|
|
|
|
|
|
|
|
2021-01-12 09:57:21 +00:00
|
|
|
class EvaFormView(CookieWizardView):
|
2021-01-13 08:51:13 +00:00
|
|
|
# model = Employee
|
2021-01-12 09:57:21 +00:00
|
|
|
template_name = 'evapp/employee_form.html'
|
2021-01-13 14:21:22 +00:00
|
|
|
form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm]
|
2021-01-13 08:51:13 +00:00
|
|
|
instance = None
|
2021-01-14 11:44:04 +00:00
|
|
|
data = {}
|
2021-01-12 09:57:21 +00:00
|
|
|
# form_class = EmployeeForm
|
2021-01-05 13:10:28 +00:00
|
|
|
|
2021-01-14 11:44:04 +00:00
|
|
|
def get_context_data(self, form, **kwargs):
|
|
|
|
context = super().get_context_data(form=form, **kwargs)
|
|
|
|
# self.data.update(form.cleaned_data)
|
|
|
|
# if self.steps.current == '5':
|
|
|
|
context.update({'data': self.get_all_cleaned_data()})
|
|
|
|
return context
|
|
|
|
|
|
|
|
# def get_success_url(self):
|
|
|
|
# return reverse('success')
|
2021-01-05 13:10:28 +00:00
|
|
|
|
2021-01-13 08:51:13 +00:00
|
|
|
#this makes shure, that we use the same model instance for all steps
|
|
|
|
def get_form_instance(self,step):
|
|
|
|
if self.instance == None:
|
|
|
|
self.instance = Employee()
|
|
|
|
return self.instance
|
|
|
|
|
2021-01-12 09:57:21 +00:00
|
|
|
def done(self, form_list, **kwargs):
|
2021-01-13 08:51:13 +00:00
|
|
|
# form = form_list[0] #.save()
|
|
|
|
print ('INSTANCE_DICT')
|
|
|
|
print(self.instance_dict)
|
|
|
|
for form in form_list:
|
|
|
|
form.save()
|
2021-01-14 11:44:04 +00:00
|
|
|
# form_data = [form.cleaned_data for form in form_list]
|
|
|
|
# print(form_data)
|
2021-01-06 09:20:06 +00:00
|
|
|
for dep in MAILS:
|
2021-01-14 11:44:04 +00:00
|
|
|
send_mail_to_department(dep, self.get_all_cleaned_data())
|
2021-01-14 08:37:45 +00:00
|
|
|
return HttpResponseRedirect('success')
|
2021-01-06 09:20:06 +00:00
|
|
|
|
2021-01-14 08:37:45 +00:00
|
|
|
# return super().form_valid(form)
|