From 097eb0a4b577bfd29203d209a663b5b49e41e97f Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 26 Nov 2015 13:07:32 +0530 Subject: [PATCH] [enhancement] ability to add comments in email alerts --- .../doctype/email_alert/email_alert.json | 24 +++++++++++++++++-- .../email/doctype/email_alert/email_alert.py | 16 +++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/frappe/email/doctype/email_alert/email_alert.json b/frappe/email/doctype/email_alert/email_alert.json index 8e5d0d0281..b329c4ccae 100644 --- a/frappe/email/doctype/email_alert/email_alert.json +++ b/frappe/email/doctype/email_alert/email_alert.json @@ -25,6 +25,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -47,6 +48,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -70,6 +72,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -93,6 +96,7 @@ "options": "DocType", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -116,6 +120,7 @@ "options": "\nNew\nSave\nSubmit\nCancel\nDays After\nDays Before\nValue Change", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -140,6 +145,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -165,6 +171,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -189,6 +196,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -213,6 +221,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -234,6 +243,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -256,6 +266,7 @@ "options": "

Condition Examples:

\n
doc.status==\"Open\"\ndoc.due_date==nowdate()\ndoc.total > 40000\n
\n

Hints:

\n
    \n
  1. To check for an event every day, select \"Date Change\" in Event
  2. \n
  3. To send an alert if a particular value changes, select \"Value Change\"
  4. \n
", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -278,6 +289,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -301,6 +313,7 @@ "options": "Email Alert Recipient", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -323,6 +336,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -345,6 +359,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -368,6 +383,7 @@ "permlevel": 0, "precision": "", "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -388,9 +404,10 @@ "label": "Message Examples", "length": 0, "no_copy": 0, - "options": "
Message Example
\n\n
\n

Order Overdue

\n\n

Transaction {{ doc.name }} has exceeded Due Date. Please take necessary action.

\n\n

Details

\n\n\n
", + "options": "
Message Example
\n\n
\n<h3>Order Overdue</h3>\n\n<p>Transaction {{ doc.name }} has exceeded Due Date. Please take necessary action.</p>\n\n<!-- show last comment -->\n{% if comments %}\nLast comment: {{ comments[-1].comment }} by {{ comments[-1].by }}\n{% endif %}\n\n<h4>Details</h4>\n\n<ul>\n<li>Customer: {{ doc.customer }}\n<li>Amount: {{ doc.total_amount }}\n</ul>\n
", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -414,6 +431,7 @@ "permlevel": 0, "precision": "", "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -425,13 +443,15 @@ "hide_heading": 0, "hide_toolbar": 0, "icon": "icon-envelope", + "idx": 0, "in_create": 0, "in_dialog": 0, "is_submittable": 0, "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2015-11-16 06:29:46.014034", + "menu_index": 0, + "modified": "2015-11-26 02:14:59.637519", "modified_by": "Administrator", "module": "Email", "name": "Email Alert", diff --git a/frappe/email/doctype/email_alert/email_alert.py b/frappe/email/doctype/email_alert/email_alert.py index 2366652de3..5da08920c8 100644 --- a/frappe/email/doctype/email_alert/email_alert.py +++ b/frappe/email/doctype/email_alert/email_alert.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import frappe +import json from frappe import _ from frappe.model.document import Document from frappe.utils import validate_email_add, nowdate @@ -95,10 +96,21 @@ def evaluate_alert(doc, alert, event): return subject = alert.subject + + if event != "Value Change" and not doc.is_new(): + # reload the doc for the latest values & comments, + # except for validate type event. + doc = frappe.get_doc(doc.doctype, doc.name) + + context = {"doc": doc, "alert": alert, "comments": None} + + if doc.get("_comments"): + context["comments"] = json.loads(doc.get("_comments")) + if "{" in subject: - subject = frappe.render_template(alert.subject, {"doc": doc, "alert": alert}) + subject = frappe.render_template(alert.subject, context) frappe.sendmail(recipients=recipients, subject=subject, - message= frappe.render_template(alert.message, {"doc": doc, "alert":alert}), + message= frappe.render_template(alert.message, context), bulk=True, reference_doctype = doc.doctype, reference_name = doc.name, attachments = [frappe.attach_print(doc.doctype, doc.name)] if alert.attach_print else None)