filter context for mails for relevant data

This commit is contained in:
Benni Bärmann 2021-01-14 14:20:17 +01:00
parent 92f3896e7b
commit ab261c6118
1 changed files with 10 additions and 5 deletions

View File

@ -20,7 +20,7 @@ class EvaFormView(CookieWizardView):
template_name = 'evapp/employee_form.html' template_name = 'evapp/employee_form.html'
form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm] form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm]
instance = None instance = None
data = {} # data = {}
# we need this to display all the data in the last step # we need this to display all the data in the last step
def get_context_data(self, form, **kwargs): def get_context_data(self, form, **kwargs):
@ -35,24 +35,29 @@ class EvaFormView(CookieWizardView):
return self.instance return self.instance
def done(self, form_list, **kwargs): def done(self, form_list, **kwargs):
# form = form_list[0] #.save()
print ('INSTANCE_DICT') print ('INSTANCE_DICT')
print(self.instance_dict) print(self.instance_dict)
# save data to database # save data to database
for form in form_list: for form in form_list:
form.save() form.save()
# send data to departments # send data to departments
for dep in MAILS: for dep in MAILS:
self.send_mail_to_department(dep) self.send_mail_to_department(dep)
return HttpResponseRedirect('success') return HttpResponseRedirect('success')
# send a mail to the department with all needed data
def send_mail_to_department(self, department): def send_mail_to_department(self, department):
'send a mail to the given department with the nececcary notifications' 'send a mail to the given department with the nececcary notifications'
print(f'send mail to department {department}...') print(f'send mail to department {department}...')
# TODO: don't send ALL the data # only the relevant data should be in the context
context = {'data': self.data} 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: try:
mail_template = get_template(f'evapp/{department}_mail.txt') mail_template = get_template(f'evapp/{department}_mail.txt')
@ -63,7 +68,7 @@ class EvaFormView(CookieWizardView):
[MAILS[department]['MAIL']], [MAILS[department]['MAIL']],
fail_silently=False) fail_silently=False)
except BadHeaderError: except BadHeaderError:
self.insatnce.delete() self.instance.delete()
return HttpResponse('Invalid header found. Data not saved!') return HttpResponse('Invalid header found. Data not saved!')
except SMTPException: except SMTPException:
self.instance.delete() self.instance.delete()