Skip to content

Commit

Permalink
[#2530] Move instructor training approaching receiver to BaseAction
Browse files Browse the repository at this point in the history
This is first out of 3 actions related to this complex action.
  • Loading branch information
pbanaszkiewicz committed Oct 14, 2023
1 parent fc97123 commit 848653f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 52 deletions.
77 changes: 37 additions & 40 deletions amy/emails/actions/instructor_training_approaching.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import logging
from typing import Any

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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("/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 848653f

Please sign in to comment.