diff --git a/atst/app.py b/atst/app.py index ef5ff09c..f4c5be4d 100644 --- a/atst/app.py +++ b/atst/app.py @@ -29,6 +29,7 @@ from atst.utils import mailer from atst.utils.form_cache import FormCache from atst.utils.json import CustomJSONEncoder from atst.queue import queue +from atst.utils.notification_sender import NotificationSender from logging.config import dictConfig from atst.utils.logging import JsonFormatter, RequestContextFilter @@ -63,6 +64,7 @@ def make_app(config): make_csp_provider(app) make_crl_validator(app) make_mailer(app) + make_notification_sender(app) queue.init_app(app) db.init_app(app) @@ -247,6 +249,10 @@ def make_mailer(app): app.mailer = mailer.Mailer(mailer_connection, sender) +def make_notification_sender(app): + app.notification_sender = NotificationSender(queue, app.logger) + + def apply_json_logger(): dictConfig( { diff --git a/atst/utils/notification_sender.py b/atst/utils/notification_sender.py index 4b47f472..881106fa 100644 --- a/atst/utils/notification_sender.py +++ b/atst/utils/notification_sender.py @@ -1,4 +1,6 @@ -from atst.queue import ATSTQueue, queue +from logging import Logger + +from atst.queue import ATSTQueue from atst.database import db from atst.models import NotificationRecipient @@ -6,11 +8,14 @@ from atst.models import NotificationRecipient class NotificationSender(object): EMAIL_SUBJECT = "ATST notification" - def __init__(self, queue: ATSTQueue): + def __init__(self, queue: ATSTQueue, logger: Logger): self.queue = queue + self.logger = logger def send(self, body, type_=None): recipients = self._get_recipients(type_) + self.logger.info( + "Sending a notification to these recipients: {}\n\n{}".format(recipients, body)) self.queue.send_mail(recipients, self.EMAIL_SUBJECT, body) def _get_recipients(self, type_): @@ -18,6 +23,3 @@ class NotificationSender(object): recipient.email for recipient in db.session.query(NotificationRecipient).all() ] - - -notification_sender = NotificationSender(queue) diff --git a/tests/utils/test_notification_sender.py b/tests/utils/test_notification_sender.py index d0f7150a..0e8f3c23 100644 --- a/tests/utils/test_notification_sender.py +++ b/tests/utils/test_notification_sender.py @@ -11,8 +11,8 @@ def mock_queue(queue): @pytest.fixture -def notification_sender(mock_queue): - return NotificationSender(mock_queue) +def notification_sender(mock_queue, mock_logger): + return NotificationSender(mock_queue, mock_logger) def test_can_send_notification(mock_queue, notification_sender):