From 848653f6a6a5878f94176e16f04209e55e45ad7e Mon Sep 17 00:00:00 2001 From: Piotr Banaszkiewicz Date: Sat, 14 Oct 2023 23:52:17 +0200 Subject: [PATCH] [#2530] Move instructor training approaching receiver to BaseAction This is first out of 3 actions related to this complex action. --- .../instructor_training_approaching.py | 77 +++++++++---------- ...nstructor_training_approaching_receiver.py | 15 ++-- ...or_training_approaching_remove_receiver.py | 3 +- ...or_training_approaching_update_receiver.py | 3 +- 4 files changed, 46 insertions(+), 52 deletions(-) diff --git a/amy/emails/actions/instructor_training_approaching.py b/amy/emails/actions/instructor_training_approaching.py index acc4c5229..3d2ea0b12 100644 --- a/amy/emails/actions/instructor_training_approaching.py +++ b/amy/emails/actions/instructor_training_approaching.py @@ -1,3 +1,4 @@ +from datetime import datetime import logging from typing import Any @@ -7,12 +8,13 @@ from django.utils import timezone from typing_extensions import Unpack +from emails.actions.base_action import BaseAction from emails.controller import ( EmailController, EmailControllerMissingRecipientsException, EmailControllerMissingTemplateException, ) -from emails.models import EmailTemplate, ScheduledEmail +from emails.models import ScheduledEmail from emails.signals import ( INSTRUCTOR_TRAINING_APPROACHING_SIGNAL_NAME, Signal, @@ -27,10 +29,8 @@ ) from emails.utils import ( messages_action_cancelled, - messages_action_scheduled, messages_action_updated, messages_missing_recipients, - messages_missing_template, messages_missing_template_link, one_month_before, person_from_request, @@ -107,43 +107,40 @@ def run_instructor_training_approaching_strategy( ) -@receiver(instructor_training_approaching_signal) -@feature_flag_enabled("EMAIL_MODULE") -def instructor_training_approaching_receiver( - sender: Any, **kwargs: Unpack[InstructorTrainingApproachingKwargs] -) -> None: - request = kwargs["request"] - event = kwargs["event"] - event_start_date = kwargs["event_start_date"] - instructors = [ - task.person - for task in Task.objects.filter(event=event, role__name="instructor") - ] - instructor_emails = [ - instructor.email for instructor in instructors if instructor.email - ] - - scheduled_at = one_month_before(event_start_date) - context: InstructorTrainingApproachingContext = { - "event": event, - "instructors": instructors, - } - signal_name = INSTRUCTOR_TRAINING_APPROACHING_SIGNAL_NAME - try: - scheduled_email = EmailController.schedule_email( - signal=signal_name, - context=context, - scheduled_at=scheduled_at, - to_header=instructor_emails, - generic_relation_obj=event, - author=person_from_request(request), - ) - except EmailControllerMissingRecipientsException: - messages_missing_recipients(request, signal_name) - except EmailTemplate.DoesNotExist: - messages_missing_template(request, signal_name) - else: - messages_action_scheduled(request, signal_name, scheduled_email) +class InstructorTrainingApproachingReceiver(BaseAction): + signal = instructor_training_approaching_signal.signal_name + + def get_scheduled_at(self, **kwargs) -> datetime: + event_start_date = kwargs["event_start_date"] + return one_month_before(event_start_date) + + def get_context( + self, **kwargs: Unpack[InstructorTrainingApproachingKwargs] + ) -> InstructorTrainingApproachingContext: + event = kwargs["event"] + instructors = [ + task.person + for task in Task.objects.filter(event=event, role__name="instructor") + ] + return { + "event": event, + "instructors": instructors, + } + + def get_generic_relation_object( + self, context: InstructorTrainingApproachingContext, **kwargs + ) -> Event: + return context["event"] + + def get_recipients( + self, context: InstructorTrainingApproachingContext, **kwargs + ) -> list[str]: + instructors = context["instructors"] + return [instructor.email for instructor in instructors if instructor.email] + + +instructor_training_approaching_receiver = InstructorTrainingApproachingReceiver() +instructor_training_approaching_signal.connect(instructor_training_approaching_receiver) @receiver(instructor_training_approaching_update_signal) diff --git a/amy/emails/tests/actions/test_instructor_training_approaching_receiver.py b/amy/emails/tests/actions/test_instructor_training_approaching_receiver.py index 3452829a5..04f7f52ec 100644 --- a/amy/emails/tests/actions/test_instructor_training_approaching_receiver.py +++ b/amy/emails/tests/actions/test_instructor_training_approaching_receiver.py @@ -50,7 +50,7 @@ def setUp(self) -> None: event=self.event, person=self.instructor2, role=instructor_role ) - @patch("workshops.utils.feature_flags.logger") + @patch("emails.actions.base_action.logger") def test_disabled_when_no_feature_flag(self, mock_logger) -> None: # Arrange request = RequestFactory().get("/") @@ -60,7 +60,7 @@ def test_disabled_when_no_feature_flag(self, mock_logger) -> None: # Assert mock_logger.debug.assert_called_once_with( "EMAIL_MODULE feature flag not set, skipping " - "instructor_training_approaching_receiver" + "instructor_training_approaching" ) def test_receiver_connected_to_signal(self) -> None: @@ -85,7 +85,7 @@ def test_action_triggered(self) -> None: # Act with patch( - "emails.actions.instructor_training_approaching.messages_action_scheduled" + "emails.actions.base_action.messages_action_scheduled" ) as mock_messages_action_scheduled: instructor_training_approaching_signal.send( sender=self.event, @@ -103,7 +103,7 @@ def test_action_triggered(self) -> None: ) @override_settings(FLAGS={"EMAIL_MODULE": [("boolean", True)]}) - @patch("emails.actions.instructor_training_approaching.messages_action_scheduled") + @patch("emails.actions.base_action.messages_action_scheduled") @patch("emails.actions.instructor_training_approaching.one_month_before") def test_email_scheduled( self, @@ -122,8 +122,7 @@ def test_email_scheduled( # Act with patch( - "emails.actions.instructor_training_approaching" - ".EmailController.schedule_email" + "emails.actions.base_action.EmailController.schedule_email" ) as mock_schedule_email: instructor_training_approaching_signal.send( sender=self.event, @@ -143,7 +142,7 @@ def test_email_scheduled( ) @override_settings(FLAGS={"EMAIL_MODULE": [("boolean", True)]}) - @patch("emails.actions.instructor_training_approaching.messages_missing_recipients") + @patch("emails.actions.base_action.messages_missing_recipients") def test_missing_recipients( self, mock_messages_missing_recipients: MagicMock ) -> None: @@ -167,7 +166,7 @@ def test_missing_recipients( mock_messages_missing_recipients.assert_called_once_with(request, signal) @override_settings(FLAGS={"EMAIL_MODULE": [("boolean", True)]}) - @patch("emails.actions.instructor_training_approaching.messages_missing_template") + @patch("emails.actions.base_action.messages_missing_template") def test_missing_template(self, mock_messages_missing_template: MagicMock) -> None: # Arrange request = RequestFactory().get("/") diff --git a/amy/emails/tests/actions/test_instructor_training_approaching_remove_receiver.py b/amy/emails/tests/actions/test_instructor_training_approaching_remove_receiver.py index 90ec31853..589dabb8f 100644 --- a/amy/emails/tests/actions/test_instructor_training_approaching_remove_receiver.py +++ b/amy/emails/tests/actions/test_instructor_training_approaching_remove_receiver.py @@ -283,8 +283,7 @@ def test_integration(self) -> None: request = RequestFactory().get("/") with patch( - "emails.actions.instructor_training_approaching." - "messages_action_scheduled" + "emails.actions.base_action.messages_action_scheduled" ) as mock_action_scheduled: run_instructor_training_approaching_strategy( instructor_training_approaching_strategy(event), diff --git a/amy/emails/tests/actions/test_instructor_training_approaching_update_receiver.py b/amy/emails/tests/actions/test_instructor_training_approaching_update_receiver.py index cae83d7c5..60eee88ff 100644 --- a/amy/emails/tests/actions/test_instructor_training_approaching_update_receiver.py +++ b/amy/emails/tests/actions/test_instructor_training_approaching_update_receiver.py @@ -343,8 +343,7 @@ def test_integration(self) -> None: request = RequestFactory().get("/") with patch( - "emails.actions.instructor_training_approaching." - "messages_action_scheduled" + "emails.actions.base_action.messages_action_scheduled" ) as mock_action_scheduled: run_instructor_training_approaching_strategy( instructor_training_approaching_strategy(event),