from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import get_template from input.models import Project from .attachments import collect_and_attach __all__ = [ 'build_email', 'send_email', 'collect_and_attach', 'send_applicant_decision_mail', 'send_staff_decision_mail', 'send_decision_mails', ] def build_email(template_name: str, context: dict, subject: str, *recipients: str, **kwargs): body = get_template(f'mails/{template_name}.txt').render(context) html = get_template(f'mails/{template_name}.html').render(context) kwargs.setdefault('from_email', settings.IF_EMAIL) kwargs['subject'] = subject kwargs['body'] = body kwargs['to'] = recipients email = EmailMultiAlternatives(**kwargs) email.attach_alternative(html, 'text/html') return email def send_email(template_name: str, context: dict, subject: str, *recipients: str, fail_silently=False, **kwargs): return build_email(template_name, context, subject, *recipients, **kwargs).send(fail_silently) def get_decision_mail_context(obj: Project): """ Build a minimal, consistent context for decision mails (applicant & staff). """ return { 'project': obj, 'data': { 'realname': obj.realname or obj.email, 'name': obj.name, }, } def send_base_decision_mail(obj: Project, scope: str, subject: str, recipient: str): context = get_decision_mail_context(obj) decision = 'granted' if obj.granted else 'denied' decision_label = 'bewilligt' if obj.granted else 'abgelehnt' subject = subject.format(name=obj.name, decision=decision_label) return send_email(f'approval_{scope}_{decision}', context, subject, recipient) def send_applicant_decision_mail(obj: Project): """ Send a decision email to the applicant after manual approval/denial. """ if recipient := obj.email: return send_base_decision_mail(obj, 'applicant', 'Deine Förderanfrage „{name}“ – {decision}', recipient) return 0 def send_staff_decision_mail(obj: Project): """ Send a decision email to the internal team (staff) after approval/denial. """ return send_base_decision_mail(obj, 'staff', 'Entscheidung: {name} ({decision})', settings.IF_EMAIL) def send_decision_mails(obj: Project): return send_applicant_decision_mail(obj) + send_staff_decision_mail(obj)