From d468a25caf56c34fba35f7d6fe2519c3d2479507 Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 18 Nov 2019 15:05:44 +0530 Subject: [PATCH 1/3] feat: Handle server and client side primary action in msgprint - Added provision to pass primary action from server side msgprint - msgprint on client side handles server side and client side methods as primary actions --- frappe/__init__.py | 6 +++++- frappe/public/js/frappe/ui/messages.js | 30 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index d3f40d8375..e47a6a624b 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -290,7 +290,7 @@ def log(msg): debug_log.append(as_unicode(msg)) -def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False): +def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False, primary_action=None): """Print a message to the user (via HTTP response). Messages are sent in the `__server_messages` property in the response JSON and shown in a pop-up / modal. @@ -299,6 +299,7 @@ def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, :param title: [optional] Message title. :param raise_exception: [optional] Raise given exception and show message. :param as_table: [optional] If `msg` is a list of lists, render as HTML table. + :param primary_action: [optional] Bind a primary server/client side action. """ from frappe.utils import encode @@ -338,6 +339,9 @@ def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, if alert: out.alert = 1 + if primary_action: + out.primary_action = primary_action + message_log.append(json.dumps(out)) if raise_exception and hasattr(raise_exception, '__name__'): diff --git a/frappe/public/js/frappe/ui/messages.js b/frappe/public/js/frappe/ui/messages.js index f804648834..47dc3095ee 100644 --- a/frappe/public/js/frappe/ui/messages.js +++ b/frappe/public/js/frappe/ui/messages.js @@ -122,8 +122,36 @@ frappe.msgprint = function(msg, title) { // setup and bind an action to the primary button if (data.primary_action) { - frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), + if (data.primary_action.server_action && typeof data.primary_action.server_action === 'string') { + frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), + () => { + frappe.call({ + method: data.primary_action.server_action, + args: { + args: data.primary_action.args + } + }); + }); + } + + if (data.primary_action.client_action && typeof data.primary_action.client_action === 'string') { + let parts = data.primary_action.client_action.split('.'); + let obj = window; + for (let part of parts) { + obj = obj[part]; + } + frappe.msg_dialog.set_primary_action( + __(data.primary_action.label || "Done"), + () => { + if (typeof obj === 'function') { + obj(data.primary_action.args); + } + } + ); + } else { + frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), data.primary_action.action); + } } // class "msgprint" is used in tests From d7d602b134a1da3e8edbc44f199fa27d85d11660 Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 19 Nov 2019 19:54:39 +0530 Subject: [PATCH 2/3] chore: Code cleanup --- frappe/public/js/frappe/ui/messages.js | 36 ++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/frappe/public/js/frappe/ui/messages.js b/frappe/public/js/frappe/ui/messages.js index 47dc3095ee..5c292a76c4 100644 --- a/frappe/public/js/frappe/ui/messages.js +++ b/frappe/public/js/frappe/ui/messages.js @@ -123,15 +123,14 @@ frappe.msgprint = function(msg, title) { // setup and bind an action to the primary button if (data.primary_action) { if (data.primary_action.server_action && typeof data.primary_action.server_action === 'string') { - frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), - () => { - frappe.call({ - method: data.primary_action.server_action, - args: { - args: data.primary_action.args - } - }); + data.primary_action.action = () => { + frappe.call({ + method: data.primary_action.server_action, + args: { + args: data.primary_action.args + } }); + } } if (data.primary_action.client_action && typeof data.primary_action.client_action === 'string') { @@ -140,19 +139,18 @@ frappe.msgprint = function(msg, title) { for (let part of parts) { obj = obj[part]; } - frappe.msg_dialog.set_primary_action( - __(data.primary_action.label || "Done"), - () => { - if (typeof obj === 'function') { - obj(data.primary_action.args); - } + data.primary_action.action = () => { + if (typeof obj === 'function') { + obj(data.primary_action.args); } - ); - } else { - frappe.msg_dialog.set_primary_action(__(data.primary_action.label || "Done"), - data.primary_action.action); + } } - } + + frappe.msg_dialog.set_primary_action( + __(data.primary_action.label || "Done"), + data.primary_action.action + ); + } // class "msgprint" is used in tests frappe.msg_dialog.msg_area = $('
') From 65f20a3ad977502fc12ceb403554a02cec1f444e Mon Sep 17 00:00:00 2001 From: marination Date: Wed, 20 Nov 2019 09:54:26 +0530 Subject: [PATCH 3/3] fix: Codacy --- frappe/public/js/frappe/ui/messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/ui/messages.js b/frappe/public/js/frappe/ui/messages.js index 5c292a76c4..26bee50f18 100644 --- a/frappe/public/js/frappe/ui/messages.js +++ b/frappe/public/js/frappe/ui/messages.js @@ -150,7 +150,7 @@ frappe.msgprint = function(msg, title) { __(data.primary_action.label || "Done"), data.primary_action.action ); - } + } // class "msgprint" is used in tests frappe.msg_dialog.msg_area = $('
')