From ab261c61184c4b6568e6cfc7f7cb9197316107de Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Thu, 14 Jan 2021 14:20:17 +0100 Subject: [PATCH] filter context for mails for relevant data --- evapp/views.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/evapp/views.py b/evapp/views.py index 0cadd02..5f3db15 100644 --- a/evapp/views.py +++ b/evapp/views.py @@ -20,7 +20,7 @@ class EvaFormView(CookieWizardView): template_name = 'evapp/employee_form.html' form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm] instance = None - data = {} + # data = {} # we need this to display all the data in the last step def get_context_data(self, form, **kwargs): @@ -35,24 +35,29 @@ class EvaFormView(CookieWizardView): return self.instance def done(self, form_list, **kwargs): - # form = form_list[0] #.save() 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}...') - # TODO: don't send ALL the data - context = {'data': self.data} + # 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') @@ -63,7 +68,7 @@ class EvaFormView(CookieWizardView): [MAILS[department]['MAIL']], fail_silently=False) except BadHeaderError: - self.insatnce.delete() + self.instance.delete() return HttpResponse('Invalid header found. Data not saved!') except SMTPException: self.instance.delete()