From 76ba63002dbfd8bc59a5d470952bf5c5f36bce76 Mon Sep 17 00:00:00 2001 From: Oliver Zander Date: Fri, 17 Oct 2025 12:06:23 +0200 Subject: [PATCH] clean up mail code --- input/{mailer.py => utils/mail/__init__.py} | 12 ++++- .../mail/attachments.py} | 3 ++ input/views.py | 53 +++++++++---------- 3 files changed, 40 insertions(+), 28 deletions(-) rename input/{mailer.py => utils/mail/__init__.py} (92%) rename input/{mail_attachments.py => utils/mail/attachments.py} (99%) diff --git a/input/mailer.py b/input/utils/mail/__init__.py similarity index 92% rename from input/mailer.py rename to input/utils/mail/__init__.py index d3df345..db3a53c 100644 --- a/input/mailer.py +++ b/input/utils/mail/__init__.py @@ -2,7 +2,17 @@ from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import get_template from django.utils.html import strip_tags -from .models import TYPE_CHOICES + +from input.models import TYPE_CHOICES + +from .attachments import collect_attachment_paths, attach_files + +__all__ = [ + 'collect_attachment_paths', + 'attach_files', + 'send_decision_mail', + 'send_staff_decision_mail', +] def _type_labels(choice: str): diff --git a/input/mail_attachments.py b/input/utils/mail/attachments.py similarity index 99% rename from input/mail_attachments.py rename to input/utils/mail/attachments.py index 46e7f26..0916f36 100644 --- a/input/mail_attachments.py +++ b/input/utils/mail/attachments.py @@ -4,11 +4,14 @@ import time import urllib.request import urllib.parse import mimetypes + from pathlib import Path from typing import Iterable, List, Tuple + from django.conf import settings from django.core.mail import EmailMultiAlternatives + def _ensure_cache_dir() -> Path: """ Ensure that the cache directory for attachments exists. diff --git a/input/views.py b/input/views.py index 7525a00..2c6292d 100755 --- a/input/views.py +++ b/input/views.py @@ -12,7 +12,8 @@ from django.contrib.auth.decorators import login_required from django.views.generic import TemplateView 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 input.utils.mail import collect_attachment_paths, attach_files from .forms import ( BaseApplicationForm, @@ -263,42 +264,40 @@ class ApplicationView(FormView): def send_mail(self, obj, data): # Prepare minimal mail context and send mails + type_label_html = self.type_info.label + type_label_plain = strip_tags(type_label_html) + data['pk'] = obj.pk data['url_prefix'] = settings.EMAIL_URL_PREFIX - data['type_label'] = self.type_info.label + data['type_label'] = type_label_html + context = {'data': data} - try: - # Mail to applicant - txt1 = get_template('input/ifg_volunteer_mail.txt').render(context) - html1 = get_template('input/ifg_volunteer_mail.html').render(context) - msg1 = EmailMultiAlternatives( - 'Deine Förderanfrage bei Wikimedia Deutschland', txt1, settings.IF_EMAIL, [data['email']] - ) - msg1.attach_alternative(html1, 'text/html') - applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code) - attach_files(msg1, applicant_files) - msg1.send() - type_label_html = self.type_info.label - type_label_plain = strip_tags(type_label_html) - # Mail to IF - txt2 = get_template('input/if_mail.txt').render(context) - html2 = get_template('input/if_mail.html').render(context) - applicant_name = self.get_recipient_name(obj, data) - msg2 = EmailMultiAlternatives( - f'Anfrage {type_label_plain} von {applicant_name}', txt2, settings.IF_EMAIL, [settings.IF_EMAIL] - ) - msg2.attach_alternative(html2, 'text/html') - staff_files = collect_attachment_paths(kind='staff', choice=self.type_code) - attach_files(msg2, staff_files) - msg2.send() + applicant_name = self.get_recipient_name(obj, data) + applicant_subject = 'Deine Förderanfrage bei Wikimedia Deutschland' + staff_subject = f'Anfrage {type_label_plain} von {applicant_name}' + + try: + self.send_email('applicant', 'ifg_volunteer_mail', applicant_subject, data['email'], context) + self.send_email('staff', 'if_mail', staff_subject, settings.IF_EMAIL, context) except BadHeaderError: obj.delete() return HttpResponse('Invalid header found. Data not saved!') except SMTPException: obj.delete() - return HttpResponse('Error in sending mails (probably wrong adress?). Data not saved!') + return HttpResponse('Error in sending mails (probably wrong address?). Data not saved!') + + def send_email(self, kind, template_name, subject, recipient, context): + plain = get_template(f'input/{template_name}.txt').render(context) + html = get_template(f'input/{template_name}.html').render(context) + email = EmailMultiAlternatives(subject, plain, settings.IF_EMAIL, [recipient]) + applicant_files = collect_attachment_paths(kind=kind, choice=self.type_code) + + email.attach_alternative(html, 'text/html') + attach_files(email, applicant_files) + + return email.send() @staticmethod def get_recipient_name(obj, data):