Skip to content

Commit

Permalink
feat(notification): specify message type (html, md, txt)
Browse files Browse the repository at this point in the history
(cherry picked from commit 90b076a)
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
  • Loading branch information
blaggacao authored and akhilnarang committed Apr 2, 2024
1 parent a35e9ba commit e9a8a14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
13 changes: 12 additions & 1 deletion frappe/email/doctype/notification/notification.json
Expand Up @@ -36,6 +36,7 @@
"send_to_all_assignees",
"recipients",
"message_sb",
"message_type",
"message",
"message_examples",
"view_properties",
Expand Down Expand Up @@ -277,15 +278,24 @@
"fieldname": "send_to_all_assignees",
"fieldtype": "Check",
"label": "Send To All Assignees"
},
{
"default": "Markdown",
"depends_on": "is_standard",
"fieldname": "message_type",
"fieldtype": "Select",
"label": "Message Type",
"options": "Markdown\nHTML\nPlain Text"
}
],
"icon": "fa fa-envelope",
"index_web_pages_for_search": 1,
"links": [],
"modified": "2021-05-04 11:17:11.882314",
"modified": "2023-11-17 08:48:25.616203",
"modified_by": "Administrator",
"module": "Email",
"name": "Notification",
"naming_rule": "Set by user",
"owner": "Administrator",
"permissions": [
{
Expand All @@ -301,6 +311,7 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "subject",
"track_changes": 1
}
55 changes: 25 additions & 30 deletions frappe/email/doctype/notification/notification.py
Expand Up @@ -16,6 +16,8 @@
from frappe.utils.jinja import validate_template
from frappe.utils.safe_exec import get_safe_globals

FORMATS = {"HTML": ".html", "Markdown": ".md", "Plain Text": ".txt"}


class Notification(Document):
# begin: auto-generated types
Expand Down Expand Up @@ -50,6 +52,7 @@ class Notification(Document):
]
is_standard: DF.Check
message: DF.Code | None
message_type: DF.Literal["Markdown", "HTML", "Plain Text"]
method: DF.Data | None
module: DF.Link | None
print_format: DF.Link | None
Expand Down Expand Up @@ -94,19 +97,11 @@ def validate(self):
def on_update(self):
frappe.cache.hdel("notifications", self.document_type)
path = export_module_json(self, self.is_standard, self.module)
if path:
formats = [".md", ".html", ".txt"]

for ext in formats:
file_path = path + ext
if os.path.exists(file_path):
with open(file_path, "w") as f:
f.write(self.message)
break

else:
with open(path + ".md", "w") as f:
f.write(self.message)
if path and self.message:
extension = FORMATS.get(self.message_type, ".md")
file_path = path + extension
with open(file_path, "w") as f:
f.write(self.message)

# py
if not os.path.exists(path + ".py"):
Expand Down Expand Up @@ -412,23 +407,23 @@ def get_attachment(self, doc):
def get_template(self, md_as_html=False):
module = get_doc_module(self.module, self.doctype, self.name)

def load_template(extn):
template = ""
template_path = os.path.join(os.path.dirname(module.__file__), frappe.scrub(self.name) + extn)
if os.path.exists(template_path):
with open(template_path) as f:
template = f.read()
return template

formats = [".html", ".md", ".txt"]

for format in formats:
template = load_template(format)
if template:
if format == ".md" and md_as_html:
return frappe.utils.md_to_html(template)
else:
return template
path = os.path.join(os.path.dirname(module.__file__), frappe.scrub(self.name))
extension = FORMATS.get(self.message_type, ".md")
file_path = path + extension

template = ""

if os.path.exists(file_path):
with open(file_path) as f:
template = f.read()

if not template:
return

if extension == ".md":
return frappe.utils.md_to_html(template)

return template

def load_standard_properties(self, context):
"""load templates and run get_context"""
Expand Down

0 comments on commit e9a8a14

Please sign in to comment.