forked from beba/foerderbarometer
88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
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
|
||
|
||
|
||
def _type_labels(choice: str):
|
||
"""
|
||
Resolve the human-readable type label.
|
||
Returns (HTML label, plain text label).
|
||
"""
|
||
html = TYPE_CHOICES.get(choice, choice)
|
||
plain = strip_tags(str(html))
|
||
return html, plain
|
||
|
||
|
||
def _decision_context(obj, choice_code: str) -> dict:
|
||
"""
|
||
Build a minimal, consistent context for decision mails (applicant & staff).
|
||
Also exposes the full project object as 'project' for template access.
|
||
"""
|
||
type_html, type_plain = _type_labels(choice_code)
|
||
realname = getattr(obj, 'realname', '') or getattr(obj, 'email', '')
|
||
return {
|
||
'data': {
|
||
'realname': realname,
|
||
'typestring': type_html,
|
||
'typestring_plain': type_plain,
|
||
'name': getattr(obj, 'name', None),
|
||
},
|
||
'project': obj,
|
||
}
|
||
|
||
|
||
def send_decision_mail(obj, choice_code: str, granted: bool) -> None:
|
||
"""
|
||
Send a decision email to the applicant after manual approval/denial.
|
||
Uses: input/approval_granted.(txt|html) or input/approval_denied.(txt|html)
|
||
"""
|
||
recipient = getattr(obj, 'email', None)
|
||
if not recipient:
|
||
return # no recipient -> skip
|
||
|
||
ctx = _decision_context(obj, choice_code)
|
||
base = 'input/approval_granted' if granted else 'input/approval_denied'
|
||
|
||
project_name = getattr(obj, 'name', None) or '(ohne Projektnamen)'
|
||
decision_word = 'bewilligt' if granted else 'abgelehnt'
|
||
subject = f'Deine Förderanfrage „{project_name}“ – {decision_word}'
|
||
|
||
txt = get_template(f'{base}.txt').render(ctx)
|
||
html = get_template(f'{base}.html').render(ctx)
|
||
|
||
msg = EmailMultiAlternatives(
|
||
subject,
|
||
txt,
|
||
settings.IF_EMAIL,
|
||
[recipient],
|
||
)
|
||
msg.attach_alternative(html, 'text/html')
|
||
msg.send()
|
||
|
||
|
||
def send_staff_decision_mail(obj, choice_code: str, granted: bool) -> None:
|
||
"""
|
||
Send a decision email to the internal team (staff) after approval/denial.
|
||
Uses: input/approval_granted_staff.(txt|html) or input/approval_denied_staff.(txt|html)
|
||
"""
|
||
ctx = _decision_context(obj, choice_code)
|
||
base = 'input/approval_granted_staff' if granted else 'input/approval_denied_staff'
|
||
|
||
project_name = getattr(obj, 'name', None) or '(ohne Projektnamen)'
|
||
decision_word = 'bewilligt' if granted else 'abgelehnt'
|
||
subject = f'Entscheidung: {project_name} ({decision_word})'
|
||
|
||
txt = get_template(f'{base}.txt').render(ctx)
|
||
html = get_template(f'{base}.html').render(ctx)
|
||
|
||
msg = EmailMultiAlternatives(
|
||
subject,
|
||
txt,
|
||
settings.IF_EMAIL,
|
||
[settings.IF_EMAIL],
|
||
)
|
||
msg.attach_alternative(html, 'text/html')
|
||
msg.send()
|