Skip to content

Commit

Permalink
fix: non-html notifications from files
Browse files Browse the repository at this point in the history
(cherry picked from commit fe8cc7a)
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
  • Loading branch information
blaggacao authored and akhilnarang committed Apr 2, 2024
1 parent f5a17f7 commit a35e9ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
31 changes: 22 additions & 9 deletions frappe/email/doctype/notification/notification.py
Expand Up @@ -12,7 +12,7 @@
from frappe.integrations.doctype.slack_webhook_url.slack_webhook_url import send_slack_message
from frappe.model.document import Document
from frappe.modules.utils import export_module_json, get_doc_module
from frappe.utils import add_to_date, cast, is_html, nowdate, validate_email_address
from frappe.utils import add_to_date, cast, nowdate, validate_email_address
from frappe.utils.jinja import validate_template
from frappe.utils.safe_exec import get_safe_globals

Expand Down Expand Up @@ -95,8 +95,16 @@ def on_update(self):
frappe.cache.hdel("notifications", self.document_type)
path = export_module_json(self, self.is_standard, self.module)
if path:
# js
if not os.path.exists(path + ".md") and not os.path.exists(path + ".html"):
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)

Expand Down Expand Up @@ -401,7 +409,7 @@ def get_attachment(self, doc):
}
]

def get_template(self):
def get_template(self, md_as_html=False):
module = get_doc_module(self.module, self.doctype, self.name)

def load_template(extn):
Expand All @@ -412,7 +420,15 @@ def load_template(extn):
template = f.read()
return template

return load_template(".html") or load_template(".md")
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

def load_standard_properties(self, context):
"""load templates and run get_context"""
Expand All @@ -423,10 +439,7 @@ def load_standard_properties(self, context):
if out:
context.update(out)

self.message = self.get_template()

if not is_html(self.message):
self.message = frappe.utils.md_to_html(self.message)
self.message = self.get_template(md_as_html=True)

def on_trash(self):
frappe.cache.hdel("notifications", self.document_type)
Expand Down
2 changes: 1 addition & 1 deletion frappe/utils/jinja.py
Expand Up @@ -107,7 +107,7 @@ def guess_is_path(template):
# if its single line and ends with a html, then its probably a path
if "\n" not in template and "." in template:
extn = template.rsplit(".")[-1]
if extn in ("html", "css", "scss", "py", "md", "json", "js", "xml"):
if extn in ("html", "css", "scss", "py", "md", "json", "js", "xml", "txt"):
return True

return False
Expand Down

0 comments on commit a35e9ba

Please sign in to comment.