diff --git a/evapp/views.py b/evapp/views.py index 2c02b96..0cadd02 100644 --- a/evapp/views.py +++ b/evapp/views.py @@ -15,53 +15,19 @@ from .settings import MAILS, EVA_MAIL def success(request): return HttpResponse("gut gemacht!") -def send_mail_to_department(department, data): - 'send a mail to the given department with the nececcary notifications' - - print(f'send mail to department {department}...') - - # model = form.save() # maybe we dont need to save here and get the model instance in another way? - context = {'data': data} - - # add all relevant fields of the form to the template context - # data = MAILS[department]['DATA'] - # for key in data: - # context['data'][key] = form.cleaned_data[key] - - 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!') - class EvaFormView(CookieWizardView): - # model = Employee template_name = 'evapp/employee_form.html' form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm] instance = None data = {} - # form_class = EmployeeForm + # 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) - # 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') - #this makes shure, that we use the same model instance for all steps def get_form_instance(self,step): if self.instance == None: @@ -72,12 +38,33 @@ class EvaFormView(CookieWizardView): # form = form_list[0] #.save() print ('INSTANCE_DICT') print(self.instance_dict) + # save data to database for form in form_list: form.save() - # form_data = [form.cleaned_data for form in form_list] - # print(form_data) + # send data to departments for dep in MAILS: - send_mail_to_department(dep, self.get_all_cleaned_data()) + self.send_mail_to_department(dep) return HttpResponseRedirect('success') - # return super().form_valid(form) + 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} + + 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.insatnce.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!')