File tree Expand file tree Collapse file tree 5 files changed +48
-7
lines changed
Expand file tree Collapse file tree 5 files changed +48
-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+ @abc .abstractmethod
9+ def send (self , destination , message ):
10+ raise NotImplementedError
11+
12+
13+ DEFAULT_HOST = config .get_email_host_and_port ()["host" ]
14+ DEFAULT_PORT = config .get_email_host_and_port ()["port" ]
15+
16+
17+ class EmailNotifications (AbstractNotifications ):
18+ def __init__ (self , smtp_host = DEFAULT_HOST , port = DEFAULT_PORT ):
19+ self .server = smtplib .SMTP (smtp_host , port = port )
20+ self .server .noop ()
21+
22+ def send (self , destination , message ):
23+ msg = f"Subject: allocation service notification\n { message } "
24+ self .server .sendmail (
25+ from_addr = "allocations@example.com" ,
26+ to_addrs = [destination ],
27+ msg = msg ,
28+ )
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 ,
6+ EmailNotifications ,
7+ )
48from allocation .service_layer import handlers , messagebus , unit_of_work
59
610
711def bootstrap (
812 start_orm : bool = True ,
913 uow : unit_of_work .AbstractUnitOfWork = unit_of_work .SqlAlchemyUnitOfWork (),
10- send_mail : Callable = email . send ,
14+ notifications : AbstractNotifications = None ,
1115 publish : Callable = redis_eventpublisher .publish ,
1216) -> messagebus .MessageBus :
1317
18+ if notifications is None :
19+ notifications = EmailNotifications ()
20+
1421 if start_orm :
1522 orm .start_mappers ()
1623
17- dependencies = {"uow" : uow , "send_mail " : send_mail , "publish" : publish }
24+ dependencies = {"uow" : uow , "notifications " : notifications , "publish" : publish }
1825 injected_event_handlers = {
1926 event_type : [
2027 inject_dependencies (handler , dependencies )
Original file line number Diff line number Diff line change @@ -19,3 +19,10 @@ def get_redis_host_and_port():
1919 host = os .environ .get ("REDIS_HOST" , "localhost" )
2020 port = 63791 if host == "localhost" else 6379
2121 return dict (host = host , port = port )
22+
23+
24+ def get_email_host_and_port ():
25+ host = os .environ .get ("EMAIL_HOST" , "localhost" )
26+ port = 11025 if host == "localhost" else 1025
27+ http_port = 18025 if host == "localhost" else 8025
28+ return dict (host = host , port = port , http_port = http_port )
Original file line number Diff line number Diff line change 66from allocation .domain .model import OrderLine
77
88if TYPE_CHECKING :
9+ from allocation .adapters import notifications
910 from . import unit_of_work
1011
1112
@@ -61,9 +62,9 @@ def change_batch_quantity(
6162
6263def send_out_of_stock_notification (
6364 event : events .OutOfStock ,
64- send_mail : Callable ,
65+ notifications : notifications . AbstractNotifications ,
6566):
66- send_mail (
67+ notifications . send (
6768 "stock@made.com" ,
6869 f"Out of stock for { event .sku } " ,
6970 )
You can’t perform that action at this time.
0 commit comments