More idiomatic initialization of notification_sender

This commit is contained in:
richard-dds 2019-05-13 12:06:33 -04:00
parent aaa9d47ccf
commit c03b69b351
3 changed files with 15 additions and 7 deletions

View File

@ -29,6 +29,7 @@ from atst.utils import mailer
from atst.utils.form_cache import FormCache from atst.utils.form_cache import FormCache
from atst.utils.json import CustomJSONEncoder from atst.utils.json import CustomJSONEncoder
from atst.queue import queue from atst.queue import queue
from atst.utils.notification_sender import NotificationSender
from logging.config import dictConfig from logging.config import dictConfig
from atst.utils.logging import JsonFormatter, RequestContextFilter from atst.utils.logging import JsonFormatter, RequestContextFilter
@ -63,6 +64,7 @@ def make_app(config):
make_csp_provider(app) make_csp_provider(app)
make_crl_validator(app) make_crl_validator(app)
make_mailer(app) make_mailer(app)
make_notification_sender(app)
queue.init_app(app) queue.init_app(app)
db.init_app(app) db.init_app(app)
@ -247,6 +249,10 @@ def make_mailer(app):
app.mailer = mailer.Mailer(mailer_connection, sender) app.mailer = mailer.Mailer(mailer_connection, sender)
def make_notification_sender(app):
app.notification_sender = NotificationSender(queue, app.logger)
def apply_json_logger(): def apply_json_logger():
dictConfig( dictConfig(
{ {

View File

@ -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.database import db
from atst.models import NotificationRecipient from atst.models import NotificationRecipient
@ -6,11 +8,14 @@ from atst.models import NotificationRecipient
class NotificationSender(object): class NotificationSender(object):
EMAIL_SUBJECT = "ATST notification" EMAIL_SUBJECT = "ATST notification"
def __init__(self, queue: ATSTQueue): def __init__(self, queue: ATSTQueue, logger: Logger):
self.queue = queue self.queue = queue
self.logger = logger
def send(self, body, type_=None): def send(self, body, type_=None):
recipients = self._get_recipients(type_) 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) self.queue.send_mail(recipients, self.EMAIL_SUBJECT, body)
def _get_recipients(self, type_): def _get_recipients(self, type_):
@ -18,6 +23,3 @@ class NotificationSender(object):
recipient.email recipient.email
for recipient in db.session.query(NotificationRecipient).all() for recipient in db.session.query(NotificationRecipient).all()
] ]
notification_sender = NotificationSender(queue)

View File

@ -11,8 +11,8 @@ def mock_queue(queue):
@pytest.fixture @pytest.fixture
def notification_sender(mock_queue): def notification_sender(mock_queue, mock_logger):
return NotificationSender(mock_queue) return NotificationSender(mock_queue, mock_logger)
def test_can_send_notification(mock_queue, notification_sender): def test_can_send_notification(mock_queue, notification_sender):