Enhance ApplicationView to send applicant/staff emails with attachments and improved subjects

This commit is contained in:
Roman 2025-10-07 19:46:25 +02:00 committed by Oliver Zander
parent 1ad4970cbc
commit f98894b250
1 changed files with 19 additions and 3 deletions

View File

@ -11,6 +11,8 @@ from django.conf import settings
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from django.utils.html import strip_tags
from input.mail_attachments import collect_attachment_paths, attach_files
from .forms import ( from .forms import (
BaseApplicationForm, BaseApplicationForm,
@ -252,18 +254,32 @@ class ApplicationView(FormView):
txt1 = get_template('input/ifg_volunteer_mail.txt').render(context) txt1 = get_template('input/ifg_volunteer_mail.txt').render(context)
html1 = get_template('input/ifg_volunteer_mail.html').render(context) html1 = get_template('input/ifg_volunteer_mail.html').render(context)
msg1 = EmailMultiAlternatives( msg1 = EmailMultiAlternatives(
'Formular ausgefüllt', txt1, settings.IF_EMAIL, [data['email']] 'Deine Förderanfrage bei Wikimedia Deutschland', txt1, settings.IF_EMAIL, [data['email']]
) )
msg1.attach_alternative(html1, 'text/html') msg1.attach_alternative(html1, 'text/html')
applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code)
attach_files(msg1, applicant_files)
msg1.send() msg1.send()
type_label_html = TYPE_CHOICES.get(self.type_code, self.type_code)
type_label = strip_tags(str(type_label_html))
# Mail to IF # Mail to IF
txt2 = get_template('input/if_mail.txt').render(context) txt2 = get_template('input/if_mail.txt').render(context)
html2 = get_template('input/if_mail.html').render(context) html2 = get_template('input/if_mail.html').render(context)
applicant_name = (
getattr(modell, 'username', None)
or data.get('username')
or getattr(modell, 'realname', None)
or data.get('realname')
or getattr(modell, 'email', None)
or data.get('email')
or 'Unbekannt'
)
msg2 = EmailMultiAlternatives( msg2 = EmailMultiAlternatives(
'Formular ausgefüllt', txt2, settings.IF_EMAIL, [settings.IF_EMAIL] f'Anfrage {type_label} von {applicant_name}', txt2, settings.IF_EMAIL, [settings.IF_EMAIL]
) )
msg2.attach_alternative(html2, 'text/html') msg2.attach_alternative(html2, 'text/html')
staff_files = collect_attachment_paths(kind='staff', choice=self.type_code)
attach_files(msg2, staff_files)
msg2.send() msg2.send()
except BadHeaderError: except BadHeaderError: