WMDE
/
eva
forked from tohe/eva
7
1
Fork 0
eva/evapp/views.py

93 lines
3.4 KiB
Python

from smtplib import SMTPException
import collections
from django.views.generic.edit import CreateView
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect
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, DEPARTMENT_CHOICES, LAPTOP_CHOICES, OS_CHOICES,\
MOBILE_CHOICES, LANG_CHOICES, ACCOUNT_CHOICES, TRANSPONDER_CHOICES
from .forms import PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm
from .settings import MAILS, EVA_MAIL
def success(request):
return HttpResponse("gut gemacht!")
class EvaFormView(CookieWizardView):
template_name = 'evapp/employee_form.html'
form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm]
instance = None
# we need this to display all the data in the last step
def get_context_data(self, form, **kwargs):
context = super().get_context_data(form=form, **kwargs)
context.update({'data': self.beautify_data(self.get_all_cleaned_data())})
return context
#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
def done(self, form_list, **kwargs):
print ('INSTANCE_DICT')
print(self.instance_dict)
# save data to database
for form in form_list:
form.save()
# send data to departments
for dep in MAILS:
self.send_mail_to_department(dep)
return HttpResponseRedirect('success')
# send a mail to the department with all needed data
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}...')
# only the relevant data should be in the context
data = self.get_all_cleaned_data()
newdata = {k: v for k, v in data.items() if (k in MAILS[department]['DATA'])}
context = {'data': newdata}
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:
self.instance.delete()
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!')
# 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):
# update data with keys from *_CHOICES if present there
choices = {**DEPARTMENT_CHOICES, **LAPTOP_CHOICES, **TRANSPONDER_CHOICES,
**OS_CHOICES, **MOBILE_CHOICES, **LANG_CHOICES,}
data.update({k:choices[v] for k,v in data.items() \
if isinstance(v,collections.abc.Hashable) \
and v in choices})
return data