2021-01-06 09:59:47 +00:00
|
|
|
from smtplib import SMTPException
|
2021-01-14 14:10:29 +00:00
|
|
|
import collections
|
2021-01-06 09:59:47 +00:00
|
|
|
|
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
|
|
|
|
2021-01-14 14:10:29 +00:00
|
|
|
from .models import Employee, DEPARTMENT_CHOICES, LAPTOP_CHOICES, OS_CHOICES,\
|
|
|
|
MOBILE_CHOICES, LANG_CHOICES, ACCOUNT_CHOICES, TRANSPONDER_CHOICES
|
2021-01-13 14:21:22 +00:00
|
|
|
from .forms import PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm
|
2021-01-20 07:52:22 +00:00
|
|
|
from .settings import MAILS, EVA_MAIL, BASIC_DATA
|
2020-12-23 10:42:57 +00:00
|
|
|
|
2021-01-05 13:10:28 +00:00
|
|
|
def success(request):
|
2021-01-19 10:11:02 +00:00
|
|
|
return HttpResponse("Vielen Dank! Du hast E.V.A. erfolgreich ausgefüllt. Die Mails an die Abteilungen wurden versendet.")
|
2021-01-05 13:10:28 +00:00
|
|
|
|
2021-01-06 09:20:06 +00:00
|
|
|
|
2021-01-12 09:57:21 +00:00
|
|
|
class EvaFormView(CookieWizardView):
|
|
|
|
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-05 13:10:28 +00:00
|
|
|
|
2021-01-14 12:15:11 +00:00
|
|
|
# we need this to display all the data in the last step
|
2021-01-14 11:44:04 +00:00
|
|
|
def get_context_data(self, form, **kwargs):
|
|
|
|
context = super().get_context_data(form=form, **kwargs)
|
2021-01-14 14:10:29 +00:00
|
|
|
context.update({'data': self.beautify_data(self.get_all_cleaned_data())})
|
2021-01-14 11:44:04 +00:00
|
|
|
return context
|
|
|
|
|
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
|
|
|
print ('INSTANCE_DICT')
|
|
|
|
print(self.instance_dict)
|
2021-01-14 13:20:17 +00:00
|
|
|
|
2021-01-14 12:15:11 +00:00
|
|
|
# save data to database
|
2021-01-13 08:51:13 +00:00
|
|
|
for form in form_list:
|
|
|
|
form.save()
|
2021-01-14 13:20:17 +00:00
|
|
|
|
2021-01-14 12:15:11 +00:00
|
|
|
# send data to departments
|
2021-01-06 09:20:06 +00:00
|
|
|
for dep in MAILS:
|
2021-01-14 12:15:11 +00:00
|
|
|
self.send_mail_to_department(dep)
|
2021-01-14 08:37:45 +00:00
|
|
|
return HttpResponseRedirect('success')
|
2021-01-06 09:20:06 +00:00
|
|
|
|
2021-01-14 13:20:17 +00:00
|
|
|
# send a mail to the department with all needed data
|
2021-01-14 12:15:11 +00:00
|
|
|
def send_mail_to_department(self, department):
|
|
|
|
'send a mail to the given department with the nececcary notifications'
|
|
|
|
|
|
|
|
print(f'send mail to department {department}...')
|
|
|
|
|
2021-01-14 13:20:17 +00:00
|
|
|
data = self.get_all_cleaned_data()
|
2021-01-20 07:52:22 +00:00
|
|
|
# some data should be in every mail
|
|
|
|
newdata = {k: v for k, v in data.items() if (k in BASIC_DATA)}
|
|
|
|
# only the relevant data should be in the context
|
|
|
|
newdata.update({k: v for k, v in data.items() if (k in MAILS[department]['DATA'])})
|
2021-01-14 14:10:29 +00:00
|
|
|
|
2021-01-19 09:32:31 +00:00
|
|
|
context = {'data': self.beautify_data(newdata)}
|
2021-01-14 12:15:11 +00:00
|
|
|
|
|
|
|
try:
|
2021-01-20 08:18:18 +00:00
|
|
|
mail_template = get_template(f'evapp/department_mail.txt')
|
2021-01-14 12:15:11 +00:00
|
|
|
send_mail(
|
|
|
|
'EVA: Neuzugang',
|
|
|
|
mail_template.render(context),
|
|
|
|
EVA_MAIL,
|
|
|
|
[MAILS[department]['MAIL']],
|
|
|
|
fail_silently=False)
|
|
|
|
except BadHeaderError:
|
2021-01-14 13:20:17 +00:00
|
|
|
self.instance.delete()
|
2021-01-14 12:15:11 +00:00
|
|
|
return HttpResponse('Invalid header found. Data not saved!')
|
|
|
|
except SMTPException:
|
|
|
|
self.instance.delete()
|
|
|
|
return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!')
|
2021-01-14 14:10:29 +00:00
|
|
|
|
|
|
|
# use long form for contextdata instead of short form if available
|
|
|
|
#
|
|
|
|
# ATTENTION!
|
|
|
|
# This implementation works only for unique keys over all of these dicts from model.py
|
|
|
|
#
|
|
|
|
def beautify_data(self, data):
|
2021-01-18 09:41:29 +00:00
|
|
|
|
2021-01-19 08:08:59 +00:00
|
|
|
# update values in data dictionary with keys from *_CHOICES if present there
|
2021-01-18 11:34:45 +00:00
|
|
|
choices = {**DEPARTMENT_CHOICES, **LAPTOP_CHOICES, **TRANSPONDER_CHOICES,
|
|
|
|
**OS_CHOICES, **MOBILE_CHOICES, **LANG_CHOICES,}
|
|
|
|
data.update({k:choices[v] for k,v in data.items() \
|
2021-01-18 09:41:29 +00:00
|
|
|
if isinstance(v,collections.abc.Hashable) \
|
2021-01-18 11:34:45 +00:00
|
|
|
and v in choices})
|
2021-01-18 09:41:29 +00:00
|
|
|
|
2021-01-19 09:06:29 +00:00
|
|
|
# replace values in accounts array from *_CHOICES
|
2021-01-19 09:32:31 +00:00
|
|
|
if 'accounts' in data:
|
|
|
|
data['accounts'] = [ACCOUNT_CHOICES[c] for c in data['accounts']]
|
2021-01-19 09:06:29 +00:00
|
|
|
|
|
|
|
|
2021-01-19 08:08:59 +00:00
|
|
|
# replace keys in data dictionary with verbose_name
|
|
|
|
newdata = {self.instance._meta.get_field(k).verbose_name.title() : v for k,v in data.items()}
|
|
|
|
|
|
|
|
return newdata
|