WMDE
/
eva
forked from tohe/eva
7
1
Fork 0

no more pdf preview, but hopefully email attachment works now

This commit is contained in:
Marike Voßbeck 2026-03-18 14:56:41 +01:00
parent 47888b0c23
commit 1fb7bd9385
4 changed files with 49 additions and 18 deletions

View File

@ -51,15 +51,15 @@
{% elif wizard.steps.step1 == 4 %} {% elif wizard.steps.step1 == 4 %}
{% translate "Sonstige Angaben" %} {% translate "Sonstige Angaben" %}
<!-- Delete when in production!! --> <!-- Delete when in production!! -->
{% elif wizard.steps.step1 == 5 %} <!--{% elif wizard.steps.step1 == 5 %}
{% translate "PDF Abgabe Arbeitsmittel" %} {% translate "PDF Abgabe Arbeitsmittel" %}
{% if pdf_data %} {% if pdf_data %}
{% include 'austritt/pdf_template.html' %} {% include 'austritt/pdf_template.html' %}
<a href="{% url 'pdf-preview' %}" target="_blank"> <a href="{% url 'pdf-preview' %}" target="_blank">
<button type="button">Als PDF speichern</button> <button type="button">Als PDF speichern</button>
</a> </a>
{% endif %} {% endif %}-->
{% endif %} {% endif %}
{% else %} {% else %}
{% if wizard.steps.step1 == 2 %} {% if wizard.steps.step1 == 2 %}

View File

@ -15,7 +15,7 @@
<strong style="color: blue;">Handover sheet of work equipment</strong> <strong style="color: blue;">Handover sheet of work equipment</strong>
</td> </td>
<td style="width: 35%; text-align: right; vertical-align: bottom;font-size:large;"> <td style="width: 35%; text-align: right; vertical-align: bottom;font-size:large;">
<img src="{{ image_path}}" style="display: block; margin-left: auto; width: 100px; height: 100px;"/><br/> <img src="{{ image_path }}" style="display: block; margin-left: auto; width: 100px; height: 100px;"/><br/>
<strong>{{ pdf_data.firstname }} {{ pdf_data.lastname }}</strong> <strong>{{ pdf_data.firstname }} {{ pdf_data.lastname }}</strong>
</td> </td>
</tr> </tr>

View File

@ -9,7 +9,9 @@ urlpatterns = [
'4': change_process,}), '4': change_process,}),
name='evaform'), name='evaform'),
path('form/pdf-preview/', PDFPreviewView.as_view(), name='pdf-preview'),
path('success', success, name='success') 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'),

View File

@ -1,13 +1,15 @@
from smtplib import SMTPException from smtplib import SMTPException
import collections import collections
from xhtml2pdf import pisa from xhtml2pdf import pisa
# for PDF buffer
import io
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.views import View from django.views import View
from django.urls import reverse from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect 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 django.template.loader import get_template, render_to_string
from formtools.wizard.views import CookieWizardView from formtools.wizard.views import CookieWizardView
from django.shortcuts import render from django.shortcuts import render
@ -118,12 +120,12 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
context.update({'data': self.beautify_data(self.get_all_cleaned_data()), context.update({'data': self.beautify_data(self.get_all_cleaned_data()),
'datatable': 1,}) 'datatable': 1,})
if (self.steps.step1 == 5): #if (self.steps.step1 == 5):
pdf_data = beautify_data_pdf(self.get_all_cleaned_data()) # pdf_data = beautify_data_pdf(self.get_all_cleaned_data())
# storing for PDF class # storing for PDF class
self.request.session['pdf_data'] = pdf_data # self.request.session['pdf_data'] = pdf_data
self.request.session.modified = True #forcing session saving # self.request.session.modified = True #forcing session saving
context.update({'pdf_data': pdf_data,}) # context.update({'pdf_data': pdf_data,})
return context return context
@ -146,6 +148,7 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
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:
response = self.send_mail_to_department(dep) response = self.send_mail_to_department(dep)
@ -159,7 +162,7 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
return HttpResponseRedirect('success') 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' '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}...')
@ -171,13 +174,26 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
# only the relevant data should be in the context # 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'])}) 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} context = {'data': self.beautify_data(newdata), 'contact': contact}
firstname = data['firstname'] firstname = data['firstname']
lastname = data['lastname'] lastname = data['lastname']
lastday = data['lastdate_employment'] lastday = data['lastdate_employment']
try: try:
mail_template = get_template(f'austritt/department_mail.txt') mail_template = get_template(f'austritt/department_mail.txt')
if settings.MAILTEST: if settings.MAILTEST:
send_mail( send_mail(
f'EVA: Austritt {firstname} {lastname} {lastday} (MAILTEST)', f'EVA: Austritt {firstname} {lastname} {lastday} (MAILTEST)',
mail_template.render(context), mail_template.render(context),
@ -185,7 +201,20 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
[EVA_MAIL], [EVA_MAIL],
fail_silently=False) fail_silently=False)
elif department != "SUBMITTER": 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}', f'EVA: Austritt {firstname} {lastname} {lastday}',
mail_template.render(context), mail_template.render(context),
EVA_MAIL, EVA_MAIL,
@ -249,8 +278,8 @@ class EvaFormView(LoginRequiredMixin, CookieWizardView):
return newdata return newdata
# Class for rendering PDf # Class for rendering PDf (only needed for preview, also uncomment path in urls.py when you wanna use it)
class PDFPreviewView(LoginRequiredMixin, View): '''class PDFPreviewView(LoginRequiredMixin, View):
def get(self, request): def get(self, request):
pdf_data=request.session.get('pdf_data') pdf_data=request.session.get('pdf_data')
#getting names for pdf file naming #getting names for pdf file naming
@ -259,11 +288,11 @@ class PDFPreviewView(LoginRequiredMixin, View):
#hardcoded imagepath => change for production!!! #hardcoded imagepath => change for production!!!
context ={ context ={
'pdf_data': pdf_data, '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) html_string =render_to_string('austritt/pdf_template.html', context)
response = HttpResponse(content_type='application/pdf') response = HttpResponse(content_type='application/pdf')
response['Content-Disposition']=f'inline; filename="Rueckgabe_Arbeitsmittel_{firstname}_{lastname}.pdf"' response['Content-Disposition']=f'inline; filename="Rueckgabe_Arbeitsmittel_{firstname}_{lastname}.pdf"'
pisa.CreatePDF(html_string, dest=response) pisa.CreatePDF(html_string, dest=response)
return response return response
'''