diff --git a/gregui/mailutils.py b/gregui/mailutils.py index 30187aaf73fcec8090921f83e5b95a2b36a8eb48..19795179783c9b54b8b60652045159895fa2618b 100644 --- a/gregui/mailutils.py +++ b/gregui/mailutils.py @@ -1,6 +1,6 @@ from django.conf import settings -from django.core.mail import send_mail from django.template.loader import render_to_string +from django_q.tasks import async_task def registration_template(institution, sponsor) -> str: @@ -17,19 +17,25 @@ def confirmation_template(guest) -> str: return render_to_string("sponsor_confirmation.txt", keywords) -def send_registration_mail(mail_to, sponsor) -> int: - return send_mail( - subject="Subject", - message=registration_template(settings.INSTANCE_NAME, sponsor), - from_email=None, - recipient_list=[mail_to], +def send_registration_mail(mail_to, sponsor) -> str: + return async_task( + "django.core.mail.send_mail", + **{ + "subject": "Subject", + "message": registration_template(settings.INSTANCE_NAME, sponsor), + "from_email": None, + "recipient_list": [mail_to], + } ) -def send_confirmation_mail(mail_to, guest) -> int: - return send_mail( - subject="Subject", - message=confirmation_template(guest), - from_email=None, - recipient_list=[mail_to], +def send_confirmation_mail(mail_to, guest) -> str: + return async_task( + "django.core.mail.send_mail", + **{ + "subject": "Subject", + "message": confirmation_template(guest), + "from_email": None, + "recipient_list": [mail_to], + } ) diff --git a/gregui/tests/test_mailutils.py b/gregui/tests/test_mailutils.py index 25391f5d12898a832f6da139a94f77fb47f46e1a..03f30f3de2d4604859dbf5e1945e47a0830faf4e 100644 --- a/gregui/tests/test_mailutils.py +++ b/gregui/tests/test_mailutils.py @@ -1,4 +1,5 @@ from django.core import mail +from django_q.tasks import result import pytest from gregui import mailutils @@ -33,7 +34,8 @@ Your guest, Foo Bar, has completed their registration, please confirm the guest @pytest.mark.django_db def test_registration_mail(): mail.outbox = [] - assert mailutils.send_registration_mail("test@example.no", "Foo") == 1 + task_id = mailutils.send_registration_mail("test@example.no", "Foo") + assert result(task_id) == 1 assert len(mail.outbox) == 1 assert mail.outbox[0].to == ["test@example.no"] @@ -41,6 +43,7 @@ def test_registration_mail(): @pytest.mark.django_db def test_confirmation_mail(): mail.outbox = [] - assert mailutils.send_confirmation_mail("test@example.no", "Foo") == 1 + task_id = mailutils.send_confirmation_mail("test@example.no", "Foo") + assert result(task_id) == 1 assert len(mail.outbox) == 1 assert mail.outbox[0].to == ["test@example.no"]