Skip to content

Commit

Permalink
feat(notifications): add preview
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed May 21, 2024
1 parent 1b025c7 commit 50f98e5
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
11 changes: 11 additions & 0 deletions frappe/email/doctype/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ frappe.ui.form.on("Notification", {
document_type: function (frm) {
frappe.notification.setup_fieldname_select(frm);
},
preview_document: (frm) => {
frappe.call({
method: "generate_preview",
doc: frm.doc,
callback: (r) => {
frm.refresh_field("meets_condition");
frm.refresh_field("preview_rendered_message");
frm.refresh_field("preview_rendered_subject");
},
});
},
view_properties: function (frm) {
frappe.route_options = { doc_type: frm.doc.document_type };
frappe.set_route("Form", "Customize Form");
Expand Down
56 changes: 53 additions & 3 deletions frappe/email/doctype/notification/notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@
"view_properties",
"column_break_25",
"attach_print",
"print_format"
"print_format",
"preview_tab",
"section_break_dpmd",
"preview_document",
"column_break_jpfc",
"meets_condition",
"section_break_sdof",
"preview_rendered_subject",
"preview_rendered_message"
],
"fields": [
{
Expand Down Expand Up @@ -286,12 +294,54 @@
"fieldtype": "Select",
"label": "Message Type",
"options": "Markdown\nHTML\nPlain Text"
},
{
"fieldname": "section_break_dpmd",
"fieldtype": "Section Break"
},
{
"fieldname": "preview_document",
"fieldtype": "Dynamic Link",
"label": "Select Document",
"options": "document_type"
},
{
"fieldname": "column_break_jpfc",
"fieldtype": "Column Break"
},
{
"fieldname": "meets_condition",
"fieldtype": "Data",
"is_virtual": 1,
"label": "Meets Condition?"
},
{
"fieldname": "preview_tab",
"fieldtype": "Tab Break",
"label": "Preview"
},
{
"fieldname": "section_break_sdof",
"fieldtype": "Section Break"
},
{
"fieldname": "preview_rendered_message",
"fieldtype": "Code",
"is_virtual": 1,
"label": "Rendered Message"
},
{
"depends_on": "eval: in_list(['Email', 'Slack', 'System Notification'], doc.channel)",
"fieldname": "preview_rendered_subject",
"fieldtype": "Data",
"is_virtual": 1,
"label": "Rendered Subject"
}
],
"icon": "fa fa-envelope",
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-03-23 16:03:31.519921",
"modified": "2024-05-21 13:58:45.419683",
"modified_by": "Administrator",
"module": "Email",
"name": "Notification",
Expand All @@ -314,4 +364,4 @@
"states": [],
"title_field": "subject",
"track_changes": 1
}
}
66 changes: 64 additions & 2 deletions frappe/email/doctype/notification/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ class Notification(Document):
"Custom",
]
is_standard: DF.Check
meets_condition: DF.Data | None
message: DF.Code | None
message_type: DF.Literal["Markdown", "HTML", "Plain Text"]
method: DF.Data | None
module: DF.Link | None
preview_document: DF.DynamicLink | None
preview_rendered_message: DF.Code | None
preview_rendered_subject: DF.Data | None
print_format: DF.Link | None
property_value: DF.Data | None
recipients: DF.Table[NotificationRecipient]
send_system_notification: DF.Check
send_to_all_assignees: DF.Check
sender: DF.Link | None
sender_email: DF.Data | None
set_property_after_alert: DF.Literal[None]
set_property_after_alert: DF.LiteralNone
slack_webhook_url: DF.Link | None
subject: DF.Data | None
value_changed: DF.Literal[None]
value_changed: DF.LiteralNone
# end: auto-generated types

def onload(self):
Expand All @@ -77,6 +81,64 @@ def autoname(self):
if not self.name:
self.name = self.subject

@property
def meets_condition(self):
if not self.condition:
return _("Yes")

if not (self.preview_document and self.document_type):
return _("Select a document to check if it meets conditions.")

try:
doc = frappe.get_cached_doc(self.document_type, self.preview_document)
met_condition = frappe.safe_eval(self.condition, eval_locals=get_context(doc))
except Exception as e:
return _("Failed to evaluate conditions: {}").format(e)
return _("Yes") if met_condition else _("No")

@property
def preview_rendered_message(self):
if not (self.preview_document and self.document_type):
return _("Select a document to preview message")

try:
doc = frappe.get_cached_doc(self.document_type, self.preview_document)
context = get_context(doc)
context.update({"alert": self, "comments": None})
if doc.get("_comments"):
context["comments"] = json.loads(doc.get("_comments"))
msg = frappe.render_template(self.message, context)
if self.channel == "SMS":
return frappe.utils.strip_html_tags(msg)
return msg
except Exception as e:
return _("Failed to render message: {}").format(e)

@property
def preview_rendered_subject(self):
if not (self.preview_document and self.document_type):
return _("Select a document to preview subject")

try:
doc = frappe.get_cached_doc(self.document_type, self.preview_document)
context = get_context(doc)
context.update({"alert": self, "comments": None})
if doc.get("_comments"):
context["comments"] = json.loads(doc.get("_comments"))
if not self.subject:
return _("No subject")
if "{" in self.subject:
return frappe.render_template(self.subject, context)
return self.subject
except Exception as e:
return _("Failed to render subject: {}").format(e)

@frappe.whitelist()
def generate_preview(self):
# This function doesn't need to do anything specific as virtual fields
# get evaluated automatically.
pass

def validate(self):
if self.channel in ("Email", "Slack", "System Notification"):
validate_template(self.subject)
Expand Down

0 comments on commit 50f98e5

Please sign in to comment.