From 1fb7bd93850aa069522e616c7a495662cee19e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marike=20Vo=C3=9Fbeck?= Date: Wed, 18 Mar 2026 14:56:41 +0100 Subject: [PATCH] no more pdf preview, but hopefully email attachment works now --- .../templates/austritt/employee_form.html | 6 +- austritt/templates/austritt/pdf_template.html | 2 +- austritt/urls.py | 4 +- austritt/views.py | 55 ++++++++++++++----- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/austritt/templates/austritt/employee_form.html b/austritt/templates/austritt/employee_form.html index 107b02e..fc4fa16 100644 --- a/austritt/templates/austritt/employee_form.html +++ b/austritt/templates/austritt/employee_form.html @@ -51,15 +51,15 @@ {% elif wizard.steps.step1 == 4 %} {% translate "Sonstige Angaben" %} - {% elif wizard.steps.step1 == 5 %} + + {% endif %} {% else %} {% if wizard.steps.step1 == 2 %} diff --git a/austritt/templates/austritt/pdf_template.html b/austritt/templates/austritt/pdf_template.html index c419d41..0e31706 100644 --- a/austritt/templates/austritt/pdf_template.html +++ b/austritt/templates/austritt/pdf_template.html @@ -15,7 +15,7 @@ Handover sheet of work equipment -
+
{{ pdf_data.firstname }} {{ pdf_data.lastname }} diff --git a/austritt/urls.py b/austritt/urls.py index 3175920..fce1d29 100644 --- a/austritt/urls.py +++ b/austritt/urls.py @@ -9,7 +9,9 @@ urlpatterns = [ '4': change_process,}), name='evaform'), - path('form/pdf-preview/', PDFPreviewView.as_view(), name='pdf-preview'), + path('success', success, name='success') ] +# this path is needed, when you wanna do pdf preview +#path('form/pdf-preview/', PDFPreviewView.as_view(), name='pdf-preview'), diff --git a/austritt/views.py b/austritt/views.py index 04d51e3..71156a6 100644 --- a/austritt/views.py +++ b/austritt/views.py @@ -1,13 +1,15 @@ from smtplib import SMTPException import collections from xhtml2pdf import pisa +# for PDF buffer +import io from django.views.generic.edit import CreateView from django.views import View from django.urls import reverse from django.http import HttpResponse, HttpResponseRedirect -from django.core.mail import send_mail, BadHeaderError +from django.core.mail import send_mail, BadHeaderError, EmailMessage from django.template.loader import get_template, render_to_string from formtools.wizard.views import CookieWizardView from django.shortcuts import render @@ -118,12 +120,12 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): context.update({'data': self.beautify_data(self.get_all_cleaned_data()), 'datatable': 1,}) - if (self.steps.step1 == 5): - pdf_data = beautify_data_pdf(self.get_all_cleaned_data()) + #if (self.steps.step1 == 5): + # pdf_data = beautify_data_pdf(self.get_all_cleaned_data()) # storing for PDF class - self.request.session['pdf_data'] = pdf_data - self.request.session.modified = True #forcing session saving - context.update({'pdf_data': pdf_data,}) + # self.request.session['pdf_data'] = pdf_data + # self.request.session.modified = True #forcing session saving + # context.update({'pdf_data': pdf_data,}) return context @@ -146,6 +148,7 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): for form in form_list: form.save() + # send data to departments for dep in MAILS: response = self.send_mail_to_department(dep) @@ -159,7 +162,7 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): return HttpResponseRedirect('success') - def send_mail_to_department(self, department): + def send_mail_to_department(self, department, pdf=None): 'send a mail to the given department with the nececcary notifications' print(f'send mail to department {department}...') @@ -171,13 +174,26 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): # 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'])}) + #generate PDF and store it into buffer + pdf_data=beautify_data_pdf(self.get_all_cleaned_data()) + + #hardcoded imagepath => change for production!!! + context_pdf ={ + 'pdf_data': pdf_data, + 'image_path': '/path/to/your/eva/austritt/static/evapp/logo.png' + } + html_string =render_to_string('austritt/pdf_template.html', context_pdf, request=self.request) + buffer=io.BytesIO() + pisa.CreatePDF(html_string,dest=buffer) + pdf_bytes=buffer.getvalue() + context = {'data': self.beautify_data(newdata), 'contact': contact} firstname = data['firstname'] lastname = data['lastname'] lastday = data['lastdate_employment'] try: mail_template = get_template(f'austritt/department_mail.txt') - if settings.MAILTEST: + if settings.MAILTEST: send_mail( f'EVA: Austritt {firstname} {lastname} {lastday} (MAILTEST)', mail_template.render(context), @@ -185,7 +201,20 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): [EVA_MAIL], fail_silently=False) elif department != "SUBMITTER": - send_mail( + + # comment this back in if you wanna run mail attachments + '''if pdf and department == "OFFICE": + mail= EmailMessage( + subject=f'EVA: Austritt {firstname} {lastname} {lastday} (MAILTEST)', + body=mail_template.render(context), + from_email=EVA_MAIL, + to=[EVA_MAIL], + ) + mail.attach(f'Rueckgabe_Arbeitsmittel_{firstname}_{lastname}.pdf', pdf_bytes, 'application/pdf') + mail.send(fail_silently=False) + else: + ''' + send_mail( f'EVA: Austritt {firstname} {lastname} {lastday}', mail_template.render(context), EVA_MAIL, @@ -249,8 +278,8 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView): return newdata -# Class for rendering PDf -class PDFPreviewView(LoginRequiredMixin, View): +# Class for rendering PDf (only needed for preview, also uncomment path in urls.py when you wanna use it) +'''class PDFPreviewView(LoginRequiredMixin, View): def get(self, request): pdf_data=request.session.get('pdf_data') #getting names for pdf file naming @@ -259,11 +288,11 @@ class PDFPreviewView(LoginRequiredMixin, View): #hardcoded imagepath => change for production!!! context ={ 'pdf_data': pdf_data, - 'image_path': '/home/marike.vossbeck/eva/austritt/static/evapp/logo.png' + 'image_path': '/path/to/your/eva/austritt/static/evapp/logo.png' } html_string =render_to_string('austritt/pdf_template.html', context) response = HttpResponse(content_type='application/pdf') response['Content-Disposition']=f'inline; filename="Rueckgabe_Arbeitsmittel_{firstname}_{lastname}.pdf"' pisa.CreatePDF(html_string, dest=response) return response - +'''