From e979a9d9fb6489d6123318fede270f9748e0ed90 Mon Sep 17 00:00:00 2001 From: vasileios Date: Thu, 4 Apr 2024 12:14:48 +0200 Subject: [PATCH 1/5] [#3725] Added failed registrations to the email digest --- src/openforms/emails/tasks.py | 28 ++---- .../emails/templates/emails/admin_digest.html | 24 ++++- src/openforms/emails/tests/test_tasks.py | 97 ++++++++++++++++++- src/openforms/emails/utils.py | 22 +++++ src/openforms/logging/logevent.py | 7 +- src/openforms/registrations/tasks.py | 8 +- src/openforms/registrations/utils.py | 33 +++++++ src/openforms/submissions/admin.py | 19 ++++ src/openforms/submissions/utils.py | 11 +++ 9 files changed, 218 insertions(+), 31 deletions(-) diff --git a/src/openforms/emails/tasks.py b/src/openforms/emails/tasks.py index 248e50ad53..85f832e560 100644 --- a/src/openforms/emails/tasks.py +++ b/src/openforms/emails/tasks.py @@ -5,13 +5,11 @@ from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from django_yubin.models import Message - from openforms.celery import app from openforms.config.models import GlobalConfiguration -from openforms.logging.models import TimelineLogProxy +from openforms.registrations.utils import collect_registrations_failures -from .utils import send_mail_html +from .utils import collect_failed_emails, send_mail_html @app.task @@ -20,32 +18,24 @@ def send_email_digest() -> None: if not (recipients := config.recipients_email_digest): return - period_start = timezone.now() - timedelta(days=1) + desired_period = timezone.now() - timedelta(days=1) - logs = TimelineLogProxy.objects.filter( - timestamp__gt=period_start, - extra_data__status=Message.STATUS_FAILED, - extra_data__include_in_daily_digest=True, - ).distinct("content_type", "extra_data__status", "extra_data__event") + failed_emails = collect_failed_emails(desired_period) + failed_registrations = collect_registrations_failures(desired_period) - if not logs: + if not (failed_emails or failed_registrations): return content = render_to_string( "emails/admin_digest.html", { - "logs": [ - { - "submission_uuid": log.content_object.uuid, - "event": log.extra_data["event"], - } - for log in logs - ], + "failed_emails": failed_emails, + "failed_registrations": failed_registrations, }, ) send_mail_html( - _("[Open Forms] Daily summary of failed emails"), + _("[Open Forms] Daily summary of failed procedures"), content, settings.DEFAULT_FROM_EMAIL, recipients, diff --git a/src/openforms/emails/templates/emails/admin_digest.html b/src/openforms/emails/templates/emails/admin_digest.html index a644f27753..1f0b865606 100644 --- a/src/openforms/emails/templates/emails/admin_digest.html +++ b/src/openforms/emails/templates/emails/admin_digest.html @@ -1,10 +1,24 @@ {% load static i18n %}

- {% blocktranslate %}Here is a summary of the emails that failed to send yesterday:{% endblocktranslate %} + {% blocktranslate %}Here is a summary of the failed procedures in the past 24 hours:{% endblocktranslate %}

- +{% if failed_emails %} +
{% trans "Emails" %}
+ +{% endif %} + +{% if failed_registrations %} +
{% trans "Registrations" %}
+ +{% endif %} diff --git a/src/openforms/emails/tests/test_tasks.py b/src/openforms/emails/tests/test_tasks.py index ab5b277b8f..48c4e1336c 100644 --- a/src/openforms/emails/tests/test_tasks.py +++ b/src/openforms/emails/tests/test_tasks.py @@ -9,7 +9,9 @@ from freezegun import freeze_time from openforms.config.models import GlobalConfiguration +from openforms.forms.tests.factories import FormFactory from openforms.logging.tests.factories import TimelineLogProxyFactory +from openforms.submissions.constants import RegistrationStatuses from openforms.submissions.tests.factories import SubmissionFactory from ..tasks import send_email_digest @@ -72,8 +74,9 @@ def test_create_digest_email(self): expected_content = dedent( f"""

- Here is a summary of the emails that failed to send yesterday: + Here is a summary of the failed procedures in the past 24 hours:

+
Emails