From 0c7e476ff3f8bdc3f00312b080c61f5c2391b8dc Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 22 Mar 2016 15:26:47 +0530 Subject: [PATCH] [enhancement] standard replies now support jinja templating, fixes frappe/erpnext#4787 --- frappe/core/doctype/docfield/docfield.json | 4 +- .../standard_reply/standard_reply.json | 59 ++++++++++++++++++- .../doctype/standard_reply/standard_reply.py | 13 +++- .../doctype/letter_head/letter_head.json | 8 ++- .../public/js/frappe/views/communication.js | 34 +++++------ 5 files changed, 94 insertions(+), 24 deletions(-) diff --git a/frappe/core/doctype/docfield/docfield.json b/frappe/core/doctype/docfield/docfield.json index 9d81c082c0..819b5e40ee 100644 --- a/frappe/core/doctype/docfield/docfield.json +++ b/frappe/core/doctype/docfield/docfield.json @@ -437,7 +437,7 @@ "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 0, - "label": "Depends On", + "label": "Display Depends On", "length": 255, "no_copy": 0, "oldfieldname": "depends_on", @@ -1012,7 +1012,7 @@ "issingle": 0, "istable": 1, "max_attachments": 0, - "modified": "2016-02-22 09:08:47.112186", + "modified": "2016-03-22 15:18:41.237995", "modified_by": "Administrator", "module": "Core", "name": "DocField", diff --git a/frappe/email/doctype/standard_reply/standard_reply.json b/frappe/email/doctype/standard_reply/standard_reply.json index dbe3a5f776..a7486dba12 100644 --- a/frappe/email/doctype/standard_reply/standard_reply.json +++ b/frappe/email/doctype/standard_reply/standard_reply.json @@ -17,6 +17,7 @@ "fieldtype": "Data", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Subject", @@ -24,6 +25,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -39,6 +41,7 @@ "fieldtype": "Text Editor", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Response", @@ -46,6 +49,7 @@ "no_copy": 0, "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 1, @@ -62,6 +66,7 @@ "fieldtype": "Link", "hidden": 1, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 0, "label": "Owner", @@ -70,6 +75,57 @@ "options": "User", "permlevel": 0, "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "section_break_4", + "fieldtype": "Section Break", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_list_view": 0, + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, + { + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "fieldname": "standard_reply_help", + "fieldtype": "HTML", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_list_view": 0, + "label": "Standard Reply Help", + "length": 0, + "no_copy": 0, + "options": "

Standard Reply Example

\n\n
Order Overdue\n\nTransaction {{ doc.name }} has exceeded Due Date. Please take necessary action.\n\nDetails\n\n- Customer: {{ doc.customer }}\n- Amount: {{ doc.grand_total }}\n
\n\n

How to get fieldnames

\n\n

The fieldnames you can use in your standard reply are the fields in the document from which you are sending the email. You can find out the fields of any documents via Setup > Customize Form View and selecting the document type (e.g. Sales Invoice)

\n\n

Templating

\n\n

Templates are compiled using the Jinja Templating Langauge. To learn more about Jinja, read this documentation.

\n", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, "read_only": 0, "report_hide": 0, "reqd": 0, @@ -81,13 +137,14 @@ "hide_heading": 0, "hide_toolbar": 0, "icon": "icon-comment", + "idx": 0, "in_create": 0, "in_dialog": 0, "is_submittable": 0, "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2015-11-16 06:29:57.875243", + "modified": "2016-03-22 15:25:43.935671", "modified_by": "Administrator", "module": "Email", "name": "Standard Reply", diff --git a/frappe/email/doctype/standard_reply/standard_reply.py b/frappe/email/doctype/standard_reply/standard_reply.py index 4e73bb3c7a..308f45bb9b 100644 --- a/frappe/email/doctype/standard_reply/standard_reply.py +++ b/frappe/email/doctype/standard_reply/standard_reply.py @@ -2,8 +2,17 @@ # For license information, please see license.txt from __future__ import unicode_literals -import frappe +import frappe, json from frappe.model.document import Document class StandardReply(Document): - pass \ No newline at end of file + pass + +@frappe.whitelist() +def get_standard_reply(template_name, doc): + '''Returns the processed HTML of a standard reply with the given doc''' + if isinstance(doc, basestring): + doc = json.loads(doc) + + standard_reply = frappe.get_doc("Standard Reply", template_name) + return frappe.render_template(standard_reply.response, doc) \ No newline at end of file diff --git a/frappe/print/doctype/letter_head/letter_head.json b/frappe/print/doctype/letter_head/letter_head.json index 5835626d2f..432cc20067 100644 --- a/frappe/print/doctype/letter_head/letter_head.json +++ b/frappe/print/doctype/letter_head/letter_head.json @@ -16,6 +16,7 @@ "fieldtype": "Data", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Letter Head Name", @@ -42,6 +43,7 @@ "fieldtype": "Check", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Disabled", @@ -69,6 +71,7 @@ "fieldtype": "Check", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Is Default", @@ -96,6 +99,7 @@ "fieldtype": "Text Editor", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 1, "label": "Content", @@ -117,10 +121,12 @@ "allow_on_submit": 0, "bold": 0, "collapsible": 0, + "depends_on": "letter_head_name", "fieldname": "footer", "fieldtype": "Text Editor", "hidden": 0, "ignore_user_permissions": 0, + "ignore_xss_filter": 0, "in_filter": 0, "in_list_view": 0, "label": "Footer", @@ -148,7 +154,7 @@ "issingle": 0, "istable": 0, "max_attachments": 3, - "modified": "2016-01-28 02:03:23.139628", + "modified": "2016-03-22 15:18:09.648844", "modified_by": "Administrator", "module": "Print", "name": "Letter Head", diff --git a/frappe/public/js/frappe/views/communication.js b/frappe/public/js/frappe/views/communication.js index 027d9e39e7..63d6c04cc2 100644 --- a/frappe/public/js/frappe/views/communication.js +++ b/frappe/public/js/frappe/views/communication.js @@ -139,7 +139,8 @@ frappe.views.CommunicationComposer = Class.extend({ var me = this; this.dialog.get_input("standard_reply").on("change", function() { var standard_reply = $(this).val(); - var prepend_reply = function() { + + var prepend_reply = function(reply_html) { if(me.reply_added===standard_reply) { return; } @@ -149,30 +150,27 @@ frappe.views.CommunicationComposer = Class.extend({ parts = content.split(''); if(parts.length===2) { - content = [parts[0], frappe.standard_replies[standard_reply], - "
", parts[1]]; + content = [reply_html, "
", parts[1]]; } else { - content = [frappe.standard_replies[standard_reply], - "
", content]; + content = [reply_html, "
", content]; } content_field.set_input(content.join('')); me.reply_added = standard_reply; } - if(frappe.standard_replies[standard_reply]) { - prepend_reply(); - } else { - $.ajax({ - url:"/api/resource/Standard Reply/" + standard_reply, - statusCode: { - 200: function(data) { - frappe.standard_replies[standard_reply] = data.data.response; - prepend_reply(); - } - } - }); - } + + frappe.call({ + method: 'frappe.email.doctype.standard_reply.standard_reply.get_standard_reply', + args: { + template_name: standard_reply, + doc: me.frm.doc + }, + callback: function(r) { + prepend_reply(r.message); + } + }); + }); },