From 27602494b31cd5c9c3fe7e7bae8542fa6796fdaf Mon Sep 17 00:00:00 2001 From: Piotr Banaszkiewicz Date: Tue, 7 Nov 2023 23:07:45 +0100 Subject: [PATCH] [#2540] Inform users about issues with email strategy --- amy/trainings/views.py | 74 ++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/amy/trainings/views.py b/amy/trainings/views.py index 2ca09737a..9d4ffd043 100644 --- a/amy/trainings/views.py +++ b/amy/trainings/views.py @@ -4,6 +4,7 @@ from django.shortcuts import redirect, render from django.urls import reverse_lazy +from emails.actions.instructor_training_approaching import EmailStrategyException from emails.actions.instructor_training_completed_not_badged import ( instructor_training_completed_not_badged_strategy, run_instructor_training_completed_not_badged_strategy, @@ -82,12 +83,18 @@ def form_valid(self, form): person = form.cleaned_data["trainee"] event = form.cleaned_data["event"] result = super().form_valid(form) - run_instructor_training_completed_not_badged_strategy( - instructor_training_completed_not_badged_strategy(person), - request=self.request, - person=person, - training_completed_date=event.end if event else None, - ) + try: + run_instructor_training_completed_not_badged_strategy( + instructor_training_completed_not_badged_strategy(person), + request=self.request, + person=person, + training_completed_date=event.end if event else None, + ) + except EmailStrategyException as exc: + messages.error( + self.request, + f"Error when running instructor training completed strategy. {exc}", + ) return result @@ -99,12 +106,18 @@ class TrainingProgressUpdate(RedirectSupportMixin, OnlyForAdminsMixin, AMYUpdate def form_valid(self, form): person = form.cleaned_data["trainee"] event = form.cleaned_data["event"] - run_instructor_training_completed_not_badged_strategy( - instructor_training_completed_not_badged_strategy(person), - request=self.request, - person=person, - training_completed_date=event.end if event else None, - ) + try: + run_instructor_training_completed_not_badged_strategy( + instructor_training_completed_not_badged_strategy(person), + request=self.request, + person=person, + training_completed_date=event.end if event else None, + ) + except EmailStrategyException as exc: + messages.error( + self.request, + f"Error when running instructor training completed strategy. {exc}", + ) return super().form_valid(form) @@ -121,12 +134,18 @@ def before_delete(self, *args, **kwargs): def after_delete(self, *args, **kwargs): person = self._person event = self._event - run_instructor_training_completed_not_badged_strategy( - instructor_training_completed_not_badged_strategy(person), - request=self.request, - person=person, - training_completed_date=event.end if event else None, - ) + try: + run_instructor_training_completed_not_badged_strategy( + instructor_training_completed_not_badged_strategy(person), + request=self.request, + person=person, + training_completed_date=event.end if event else None, + ) + except EmailStrategyException as exc: + messages.error( + self.request, + f"Error when running instructor training completed strategy. {exc}", + ) def all_trainees_queryset(): @@ -188,12 +207,19 @@ def all_trainees(request): progress.full_clean() progress.save() - run_instructor_training_completed_not_badged_strategy( - instructor_training_completed_not_badged_strategy(trainee), - request=request, - person=trainee, - training_completed_date=event.end if event else None, - ) + try: + run_instructor_training_completed_not_badged_strategy( + instructor_training_completed_not_badged_strategy(trainee), + request=request, + person=trainee, + training_completed_date=event.end if event else None, + ) + except EmailStrategyException as exc: + messages.error( + request, + "Error when running instructor training completed strategy." + f" {exc}", + ) except ValidationError as e: unique_constraint_message = (