File tree Expand file tree Collapse file tree 5 files changed +49
-7
lines changed
Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ #pylint: disable=too-few-public-methods
2+ import abc
3+ import smtplib
4+ from allocation import config
5+
6+
7+ class AbstractNotifications (abc .ABC ):
8+
9+ @abc .abstractmethod
10+ def send (self , destination , message ):
11+ raise NotImplementedError
12+
13+
14+ DEFAULT_HOST = config .get_email_host_and_port ()['host' ]
15+ DEFAULT_PORT = config .get_email_host_and_port ()['port' ]
16+
17+
18+
19+ class EmailNotifications (AbstractNotifications ):
20+
21+ def __init__ (self , smtp_host = DEFAULT_HOST , port = DEFAULT_PORT ):
22+ self .server = smtplib .SMTP (smtp_host , port = port )
23+ self .server .noop ()
24+
25+ def send (self , destination , message ):
26+ msg = f'Subject: allocation service notification\n { message } '
27+ self .server .sendmail (
28+ from_addr = 'allocations@example.com' ,
29+ to_addrs = [destination ],
30+ msg = msg
31+ )
Original file line number Diff line number Diff line change 11import inspect
22from typing import Callable
3- from allocation .adapters import email , orm , redis_eventpublisher
3+ from allocation .adapters import orm , redis_eventpublisher
4+ from allocation .adapters .notifications import (
5+ AbstractNotifications , EmailNotifications
6+ )
47from allocation .service_layer import handlers , messagebus , unit_of_work
58
69
710def bootstrap (
811 start_orm : bool = True ,
912 uow : unit_of_work .AbstractUnitOfWork = unit_of_work .SqlAlchemyUnitOfWork (),
10- send_mail : Callable = email . send ,
13+ notifications : AbstractNotifications = None ,
1114 publish : Callable = redis_eventpublisher .publish ,
1215) -> messagebus .MessageBus :
1316
17+ if notifications is None :
18+ notifications = EmailNotifications ()
19+
1420 if start_orm :
1521 orm .start_mappers ()
1622
17- dependencies = {'uow' : uow , 'send_mail ' : send_mail , 'publish' : publish }
23+ dependencies = {'uow' : uow , 'notifications ' : notifications , 'publish' : publish }
1824 injected_event_handlers = {
1925 event_type : [
2026 inject_dependencies (handler , dependencies )
Original file line number Diff line number Diff line change @@ -19,3 +19,8 @@ def get_redis_host_and_port():
1919 port = 63791 if host == 'localhost' else 6379
2020 return dict (host = host , port = port )
2121
22+ def get_email_host_and_port ():
23+ host = os .environ .get ('EMAIL_HOST' , 'localhost' )
24+ port = 11025 if host == 'localhost' else 1025
25+ http_port = 18025 if host == 'localhost' else 8025
26+ return dict (host = host , port = port , http_port = http_port )
Original file line number Diff line number Diff line change 55from allocation .domain import commands , events , model
66from allocation .domain .model import OrderLine
77if TYPE_CHECKING :
8+ from allocation .adapters import notifications
89 from . import unit_of_work
910
1011
1112class InvalidSku (Exception ):
1213 pass
1314
1415
16+
1517def add_batch (
1618 cmd : commands .CreateBatch , uow : unit_of_work .AbstractUnitOfWork
1719):
@@ -56,9 +58,9 @@ def change_batch_quantity(
5658#pylint: disable=unused-argument
5759
5860def send_out_of_stock_notification (
59- event : events .OutOfStock , send_mail : Callable ,
61+ event : events .OutOfStock , notifications : notifications . AbstractNotifications ,
6062):
61- send_mail (
63+ notifications . send (
6264 'stock@made.com' ,
6365 f'Out of stock for { event .sku } ' ,
6466 )
You can’t perform that action at this time.
0 commit comments