From 307718520ba5651139a9385c6de76b55d55d2e65 Mon Sep 17 00:00:00 2001 From: vasileios Date: Wed, 3 Apr 2024 16:57:19 +0200 Subject: [PATCH] [#3725] Started with failed registrations --- src/openforms/emails/tasks.py | 85 ++++++++++++++--- .../emails/templates/emails/admin_digest.html | 24 ++++- src/openforms/emails/tests/test_tasks.py | 93 ++++++++++++++++++- 3 files changed, 180 insertions(+), 22 deletions(-) diff --git a/src/openforms/emails/tasks.py b/src/openforms/emails/tasks.py index 248e50ad53..983780772a 100644 --- a/src/openforms/emails/tasks.py +++ b/src/openforms/emails/tasks.py @@ -2,10 +2,12 @@ from django.conf import settings from django.template.loader import render_to_string +from django.urls import reverse from django.utils import timezone from django.utils.translation import gettext_lazy as _ from django_yubin.models import Message +from furl import furl from openforms.celery import app from openforms.config.models import GlobalConfiguration @@ -20,33 +22,86 @@ 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, ) + + +def collect_failed_emails(desired_period) -> list[dict[str, str] | None]: + logs = TimelineLogProxy.objects.filter( + timestamp__gt=desired_period, + extra_data__status=Message.STATUS_FAILED, + extra_data__include_in_daily_digest=True, + ).distinct("content_type", "extra_data__status", "extra_data__event") + + if not logs: + return + failed_emails = [ + { + "submission_uuid": log.content_object.uuid, + "event": log.extra_data["event"], + } + for log in logs + ] + + return failed_emails + + +def collect_registrations_failures(desired_period) -> list[dict[str, str] | None]: + logs = TimelineLogProxy.objects.filter( + timestamp__gt=desired_period, + extra_data__log_event="registration_failure", + extra_data__include_in_daily_digest=True, + ).order_by("timestamp") + + if not logs: + return + + connected_forms = {} + for log in logs: + form = log.content_object.form + + if form.uuid in connected_forms: + connected_forms[form.uuid]["failed_submissions_counter"] += 1 + connected_forms[form.uuid]["last_failure_at"] = log.timestamp + else: + connected_forms[form.uuid] = { + "form_name": form.name, + "failed_submissions_counter": 1, + "initial_failure_at": log.timestamp, + "last_failure_at": log.timestamp, + "admin_link": build_filtered_submission_admin_url( + str(form.uuid), "failed", "24hAgo" + ), + } + + failed_registrations = [form_detail for form_detail in connected_forms.values()] + + return failed_registrations + + +def build_filtered_submission_admin_url( + form_uuid: str, state: str, time_from: str +) -> str: + query_params = {"form": form_uuid, "state": state, "from": time_from} + submissions_admin_url = furl(reverse("admin:submissions_submission_changelist")) + return submissions_admin_url.add(query_params).url diff --git a/src/openforms/emails/templates/emails/admin_digest.html b/src/openforms/emails/templates/emails/admin_digest.html index a644f27753..ae3d66efa2 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..1fa2081767 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