Skip to content

Commit 6b317b7

Browse files
Merge pull request #14 from TeskaLabs/Refactoring/Review-300123
Refactoring/review 300123
2 parents 220709b + c00b8c7 commit 6b317b7

File tree

16 files changed

+344
-73
lines changed

16 files changed

+344
-73
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ https://teskalabs.github.io/asab-iris/
6060

6161
**Templates used for emailing must be stored under**
6262
```
63-
/Templates/Mails/
63+
/Templates/Email/
6464
```
6565

6666
**Templates used by Slack must be stored under**
6767
```
6868
/Templates/Slack/
6969
```
7070

71-
**Templates used for other porpose must be stored under**
71+
**Templates used for other purpose must be stored under**
7272
```
7373
/Templates/General/
7474
```

asabiris/app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .output.slack import SlackOutputService
1616

1717
# orchestrators.
18-
from .orchestration.sendmail import SendMailOrchestrator
18+
from .orchestration.sendemail import SendEmailOrchestrator
1919
from .orchestration.render import RenderReportOrchestrator
2020

2121
from .handlers.kafkahandler import KafkaHandler
@@ -69,7 +69,7 @@ def __init__(self, args=None):
6969
self.SlackOutputService = SlackOutputService(self)
7070

7171
# Orchestrators
72-
self.SendMailOrchestrator = SendMailOrchestrator(self)
72+
self.SendEmailOrchestrator = SendEmailOrchestrator(self)
7373
self.RenderReportOrchestrator = RenderReportOrchestrator(self)
7474
self.SendSlackOrchestrator = SendSlackOrchestrator(self)
7575

asabiris/handlers/kafkahandler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def dispatch(self, msg):
8989
L.warning("Message type '{}' not implemented.".format(msg_type))
9090

9191
async def send_email(self, json_data):
92-
await self.App.SendMailOrchestrator.send_mail(
92+
await self.App.SendEmailOrchestrator.send_email(
9393
email_to=json_data["to"],
9494
body_template=json_data["body"]["template"],
9595
email_cc=json_data.get("cc", []), # Optional

asabiris/handlers/webhandler.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, app):
2626

2727
web_app = app.WebContainer.WebApp
2828
web_app.router.add_put(r"/send_email", self.send_email)
29-
web_app.router.add_put(r"/send_mail", self.send_email) # This one is for backward compatiblity
29+
web_app.router.add_put(r"/send_mail", self.send_email) # This one is for backward compatibility
3030
web_app.router.add_put(r"/render", self.render)
3131
web_app.router.add_put(r"/send_slack", self.send_alert)
3232

@@ -55,24 +55,24 @@ async def send_email(self, request, *, json_data):
5555
"subject": "Lufthansa Hiest",
5656
"from": "Jimmy.Conway@Goodfellas.com",
5757
"body": {
58-
"template": "test.md",
58+
"template": "/Templates/Emails/test.md",
5959
"params": {
6060
"Name": "Toddy Siciro"
6161
}
6262
},
6363
"attachments": [
6464
{
65-
"template": "test.md",
65+
"template": "/Templates/Emails/hello.html",
6666
"params": {
6767
"Name": "Michael Corleone"
6868
},
6969
"format": "pdf",
70-
"filename": "Made.pdf"
70+
"filename": "Alert.pdf"
7171
}]
7272
}
7373
7474
```
75-
Attached will be retrieved from request.conent when rendering the email is not required.
75+
Attached will be retrieved from request.content when rendering the email is not required.
7676
7777
Example of the email body template:
7878
```
@@ -93,7 +93,7 @@ async def send_email(self, request, *, json_data):
9393
"""
9494

9595
try:
96-
await self.App.SendMailOrchestrator.send_mail(
96+
await self.App.SendEmailOrchestrator.send_email(
9797
email_to=json_data["to"],
9898
body_template=json_data["body"]["template"],
9999
email_cc=json_data.get("cc", []), # Optional
@@ -103,7 +103,6 @@ async def send_email(self, request, *, json_data):
103103
body_params=json_data["body"].get("params", {}), # Optional
104104
attachments=json_data.get("attachments", []), # Optional
105105
)
106-
107106
except KeyError as e:
108107
raise aiohttp.web.HTTPNotFound(text="{}".format(e))
109108

@@ -128,7 +127,7 @@ async def send_alert(self, request, *, json_data):
128127
{
129128
"type": "slack",
130129
"body": {
131-
"template": "test.md",
130+
"template": "/Templates/Slack/alert.md",
132131
"params": {
133132
"Name": "Toddy Siciro"
134133
}
@@ -154,7 +153,7 @@ async def render(self, request):
154153
This endpoint renders request body into template based on the format specified.
155154
Example:
156155
```
157-
localhost:8080/render?format=pdf&template=test.md
156+
localhost:8080/render?format=pdf&template=/Templates/General/test.md
158157
159158
format: pdf/html
160159
@@ -176,11 +175,12 @@ async def render(self, request):
176175
template_data = await request.json()
177176

178177
# Render a body
179-
html = await self.render(template, template_data)
178+
html = await self.App.RenderReportOrchestrator.render(template, template_data)
179+
180180
# get pdf from html if present.
181181
if fmt == 'pdf':
182182
content_type = "application/pdf"
183-
pdf = self.App.HtmlToPdfService.format(html)
183+
pdf = self.App.PdfFormatterService.format(html)
184184
elif fmt == 'html':
185185
content_type = "text/html"
186186
else:

asabiris/orchestration/render.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class RenderReportOrchestrator(object):
1515
def __init__(self, app):
1616
# formatters
1717
self.JinjaService = app.get_service("JinjaService")
18-
self.HtmlToPdfService = app.get_service("HtmlToPdfService")
1918
self.MarkdownToHTMLService = app.get_service("MarkdownToHTMLService")
2019

2120
async def render(self, template, params):
@@ -26,7 +25,9 @@ async def render(self, template, params):
2625
# - primarily use absolute path - starts with "/"
2726
# - if absolute path is used, check it start with "/Templates"
2827
# - if it is not absolute path, it is file name - assume it's a file in Templates folder
29-
assert template.startswith("/Templates"), "Template must be stored in /Templates directory"
28+
# templates must be stores in /Templates/General
29+
if not template.startswith("/Templates/General"):
30+
raise ValueError("Template must be stored in /Templates/General directory")
3031

3132
html = await self.JinjaService.format(template, params)
3233
_, extension = os.path.splitext(template)

asabiris/orchestration/sendmail.py asabiris/orchestration/sendemail.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414

1515

16-
class SendMailOrchestrator(object):
16+
class SendEmailOrchestrator(object):
1717

1818
def __init__(self, app):
1919

@@ -25,7 +25,7 @@ def __init__(self, app):
2525
# output
2626
self.SmtpService = app.get_service("SmtpService")
2727

28-
async def send_mail(
28+
async def send_email(
2929
self, *,
3030
email_to,
3131
email_from=None,
@@ -67,14 +67,11 @@ async def send_mail(
6767
# If content-type is application/octet-stream we assume there is additional attachments in request else we raise bad-request error.
6868
template = a.get('template', None)
6969

70-
# - primarily use absolute path - starts with "/"
71-
# - if absolute path is used, check it start with "/Templates"
72-
# - if it is not absolute path, it is file name - assume it's a file in Templates folder
73-
7470
if template is not None:
7571
params = a.get('params', {})
76-
77-
assert template.startswith("/Templates"), "Template must be stored in /Templates directory"
72+
# templates must be stores in /Templates/Emails
73+
if not template.startswith("/Templates/Email"):
74+
raise ValueError("Template must be stored in /Templates/Email directory")
7875

7976
# get file-name of the attachment
8077
file_name = self.get_file_name(a)
@@ -83,10 +80,10 @@ async def send_mail(
8380
# get pdf from html if present.
8481
fmt = a.get('format', 'html')
8582
if fmt == 'pdf':
86-
result = self.HtmlToPdfService.format(jinja_output)
83+
result = self.HtmlToPdfService.format(jinja_output).read()
8784
content_type = "application/pdf"
8885
elif fmt == 'html':
89-
result = jinja_output
86+
result = jinja_output.encode("utf-8")
9087
content_type = "text/html"
9188
else:
9289
raise ValueError("Invalid/unknown format '{}'".format(fmt))
@@ -124,7 +121,9 @@ async def render(self, template, params):
124121
125122
jinja_output will be used for extracting subject.
126123
"""
127-
assert template.startswith("/Templates"), "Template must be stored in /Templates directory"
124+
# templates must be stores in /Templates/Emails
125+
if not template.startswith("/Templates/Email"):
126+
raise ValueError("Template must be stored in /Templates/Email directory")
128127

129128
try:
130129
jinja_output = await self.JinjaService.format(template, params)

asabiris/orchestration/sendslack.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ async def send_to_slack(self, msg):
3333
# - primarily use absolute path - starts with "/"
3434
# - if absolute path is used, check it start with "/Templates"
3535
# - if it is not absolute path, it is file name - assume it's a file in Templates folder
36-
assert body['template'].startswith("/Templates"), "Template must be stored in /Templates directory"
36+
37+
# templates must be stores in /Templates/Slack
38+
if not body['template'].startswith("/Templates/Slack"):
39+
raise ValueError("Template must be stored in /Templates/Slack directory")
3740

3841
body["params"] = body.get("params", {})
3942
output = await self.JinjaService.format(body["template"], body["params"])

library/Templates/Email/hello.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="EN">
3+
<title>Sample subject</title>
4+
<body>
5+
<p>Hello,</p>
6+
<p style="color: green">I'm very simple message by {{name}}!</p>
7+
<p>Your,<br/>ASAB Iris</p>
8+
</body>
9+
</html>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SUBJECT: Hello from ASAB Iris
22
Hello,
33

4-
I'm very simple message!
4+
I'm very simple message by {{name}}!
55

66
Your,
77
ASAB Iris

library/Templates/hello.html library/Templates/General/hello.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="EN">
33
<body>
44
<p>Hello,</p>
5-
<p style="color: green">I'm very simple message!</p>
5+
<p style="color: green">I'm very simple message by {{name}}!</p>
66
<p>Your,<br/>ASAB Iris</p>
77
</body>
88
</html>

library/Templates/General/hello.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SUBJECT: Hello from ASAB Iris
2+
Hello,
3+
4+
I'm very simple message by {{name}}!
5+
6+
Your,
7+
ASAB Iris

library/alert.md library/Templates/Slack/alert.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Ooops. Something wrong happened.
2-
1+
This is a sample message.
32

43
{{ message }}
54

library/alert.html

-13
This file was deleted.

library/sample.html

-10
This file was deleted.

0 commit comments

Comments
 (0)