Newer
Older
from typing import Optional, Union
from django.conf import settings
from django.template.context import Context
from django_q.tasks import async_task
from greg.models import InvitationLink
from gregui.models import EmailTemplate
logger = logging.getLogger(__name__)
template: EmailTemplate, context: dict[str, str], mail_to: str
) -> dict[str, Union[str, list[str]]]:
"""Combine input to a dict ready for use as arguments ti django's send_mail"""
return {
"subject": template.get_subject(context),
"message": template.get_body(context),
"from_email": template.from_email or None,
"recipient_list": [mail_to],
}
def make_registration_url(token: str) -> str:
return "{base}/invite#{token}".format(
base=settings.BASE_URL,
token=token,
)
def make_guest_profile_url(person_id: Union[str, int]) -> str:
return "{base}/sponsor/guest/{person_id}".format(
base=settings.BASE_URL,
person_id=person_id,
)
institution: str, sponsor: str, mail_to: str, token: str
) -> dict[str, Union[str, list[str]]]:
"""
Prepare email for registration
Produces a complete set of arguments ready for use with django.core.mail.send_mail
when sending a registration email to the guest.
"""
template = EmailTemplate.objects.get(
template_key=EmailTemplate.EmailType.GUEST_REGISTRATION
)
registration_link = make_registration_url(token)
context = Context(
{
"institution": institution,
"sponsor": sponsor,
"registration_link": registration_link,
return prepare_arguments(template, context, mail_to)
def confirmation_template(
guest_name: str, mail_to: str, guest_person_id: Union[str, int]
) -> dict[str, Union[str, list[str]]]:
"""
Prepare email for confirmation
Produces a complete set of arguments ready for use with django.core.mail.send_mail
when sending a confirmation email to the sponsor.
"""
template = EmailTemplate.objects.get(
template_key=EmailTemplate.EmailType.SPONSOR_CONFIRMATION
confirmation_link = make_guest_profile_url(guest_person_id)
context = Context({"guest": guest_name, "confirmation_link": confirmation_link})
return prepare_arguments(template, context, mail_to)
def reminder_template(mail_to: str, num_roles: int) -> dict[str, Union[str, list[str]]]:
template = EmailTemplate.objects.get(
template_key=EmailTemplate.EmailType.ROLE_END_REMINDER
)
context = Context({"num_roles": num_roles})
return prepare_arguments(template, context, mail_to)
def send_registration_mail(mail_to: str, sponsor: str, token: str) -> str:
arguments = registration_template(settings.INSTANCE_NAME, sponsor, mail_to, token)
return async_task("django.core.mail.send_mail", **arguments)
def send_confirmation_mail(
mail_to: str, guest_name: str, guest_person_id: Union[int, str]
) -> str:
arguments = confirmation_template(
guest_name=guest_name, mail_to=mail_to, guest_person_id=guest_person_id
)
return async_task("django.core.mail.send_mail", **arguments)
def send_role_ending_mail(mail_to: str, num_roles: int) -> str:
arguments = reminder_template(mail_to, num_roles)
return async_task("django.core.mail.send_mail", **arguments)
def send_invite_mail(link: InvitationLink) -> Optional[str]:
email_address = link.invitation.role.person.private_email
if not email_address:
"No e-mail address found for invitation link with ID: {%s}", link.id
mail_to=email_address.value,
sponsor=f"{sponsor.first_name} {sponsor.last_name}",
token=link.uuid,
def send_confirmation_mail_from_link(link: InvitationLink) -> Optional[str]:
email_address = link.invitation.role.sponsor.work_email
guest = link.invitation.role.person
guest_name = f"{guest.first_name} {guest.last_name}"
return send_confirmation_mail(
mail_to=email_address, guest_name=guest_name, guest_person_id=guest.id
)