From a9c4894ccfef40f404ca62c253f13ec7f790a1b0 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:16:27 +0100 Subject: [PATCH 1/6] fix: task_id parameter for publish_progress --- frappe/realtime.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/frappe/realtime.py b/frappe/realtime.py index b795df3782..fe62571c05 100644 --- a/frappe/realtime.py +++ b/frappe/realtime.py @@ -9,13 +9,16 @@ import frappe from frappe.utils.data import cstr -def publish_progress(percent, title=None, doctype=None, docname=None, description=None): +def publish_progress( + percent, title=None, doctype=None, docname=None, description=None, task_id=None +): publish_realtime( "progress", {"percent": percent, "title": title, "description": description}, user=None if doctype and docname else frappe.session.user, doctype=doctype, docname=docname, + task_id=task_id, ) @@ -41,8 +44,11 @@ def publish_realtime( if message is None: message = {} + if not task_id and hasattr(frappe.local, "task_id"): + task_id = frappe.local.task_id + if event is None: - event = "task_progress" if frappe.local.task_id else "global" + event = "task_progress" if task_id else "global" elif event == "msgprint" and not user: user = frappe.session.user elif event == "list_update": @@ -51,9 +57,6 @@ def publish_realtime( elif event == "docinfo_update": room = get_doc_room(doctype, docname) - if not task_id and hasattr(frappe.local, "task_id"): - task_id = frappe.local.task_id - if not room: if task_id: after_commit = False From 105c4a20fb4d5e2546dc8ea1fc22eb72a96c5295 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:19:56 +0100 Subject: [PATCH 2/6] refactor: better variable names --- frappe/desk/doctype/bulk_update/bulk_update.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frappe/desk/doctype/bulk_update/bulk_update.py b/frappe/desk/doctype/bulk_update/bulk_update.py index 27ffb4ffb8..8a8583ab7a 100644 --- a/frappe/desk/doctype/bulk_update/bulk_update.py +++ b/frappe/desk/doctype/bulk_update/bulk_update.py @@ -74,8 +74,8 @@ def _bulk_action(doctype, docnames, action, data): failed = [] - for i, d in enumerate(docnames, 1): - doc = frappe.get_doc(doctype, d) + for idx, docname in enumerate(docnames, 1): + doc = frappe.get_doc(doctype, docname) try: message = "" if action == "submit" and doc.docstatus.is_draft(): @@ -93,12 +93,12 @@ def _bulk_action(doctype, docnames, action, data): doc.save() message = _("Updating {0}").format(doctype) else: - failed.append(d) + failed.append(docname) frappe.db.commit() - show_progress(docnames, message, i, d) + show_progress(docnames, message, idx, docname) except Exception: - failed.append(d) + failed.append(docname) frappe.db.rollback() return failed From 09395420b855ecfa8f77138ea03303a35248a6f3 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:30:13 +0100 Subject: [PATCH 3/6] feat: task_id for submit_cancel_or_update_docs --- .../desk/doctype/bulk_update/bulk_update.py | 16 ++++++++--- .../public/js/frappe/list/bulk_operations.js | 28 +++++++++---------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/frappe/desk/doctype/bulk_update/bulk_update.py b/frappe/desk/doctype/bulk_update/bulk_update.py index 8a8583ab7a..9ec7e44ae8 100644 --- a/frappe/desk/doctype/bulk_update/bulk_update.py +++ b/frappe/desk/doctype/bulk_update/bulk_update.py @@ -24,6 +24,7 @@ class BulkUpdate(Document): limit: DF.Int update_value: DF.SmallText # end: auto-generated types + @frappe.whitelist() def bulk_update(self): self.check_permission("write") @@ -45,12 +46,12 @@ class BulkUpdate(Document): @frappe.whitelist() -def submit_cancel_or_update_docs(doctype, docnames, action="submit", data=None): +def submit_cancel_or_update_docs(doctype, docnames, action="submit", data=None, task_id=None): if isinstance(docnames, str): docnames = frappe.parse_json(docnames) if len(docnames) < 20: - return _bulk_action(doctype, docnames, action, data) + return _bulk_action(doctype, docnames, action, data, task_id) elif len(docnames) <= 500: frappe.msgprint(_("Bulk operation is enqueued in background."), alert=True) frappe.enqueue( @@ -59,6 +60,7 @@ def submit_cancel_or_update_docs(doctype, docnames, action="submit", data=None): docnames=docnames, action=action, data=data, + task_id=task_id, queue="short", timeout=1000, ) @@ -68,11 +70,12 @@ def submit_cancel_or_update_docs(doctype, docnames, action="submit", data=None): ) -def _bulk_action(doctype, docnames, action, data): +def _bulk_action(doctype, docnames, action, data, task_id=None): if data: data = frappe.parse_json(data) failed = [] + num_documents = len(docnames) for idx, docname in enumerate(docnames, 1): doc = frappe.get_doc(doctype, docname) @@ -95,7 +98,12 @@ def _bulk_action(doctype, docnames, action, data): else: failed.append(docname) frappe.db.commit() - show_progress(docnames, message, idx, docname) + frappe.publish_progress( + percent=idx / num_documents * 100, + title=message, + description=docname, + task_id=task_id, + ) except Exception: failed.append(docname) diff --git a/frappe/public/js/frappe/list/bulk_operations.js b/frappe/public/js/frappe/list/bulk_operations.js index a0271967b4..863c0abb90 100644 --- a/frappe/public/js/frappe/list/bulk_operations.js +++ b/frappe/public/js/frappe/list/bulk_operations.js @@ -209,28 +209,28 @@ export default class BulkOperations { submit_or_cancel(docnames, action = "submit", done = null) { action = action.toLowerCase(); - frappe - .call({ - method: "frappe.desk.doctype.bulk_update.bulk_update.submit_cancel_or_update_docs", - args: { - doctype: this.doctype, - action: action, - docnames: docnames, - }, + const task_id = Math.random().toString(36).slice(-5); + frappe.realtime.task_subscribe(task_id); + return frappe + .xcall("frappe.desk.doctype.bulk_update.bulk_update.submit_cancel_or_update_docs", { + doctype: this.doctype, + action: action, + docnames: docnames, + task_id: task_id, }) - .then((r) => { - let failed = r.message; - if (!failed) failed = []; - - if (failed.length && !r._server_messages) { + .then((failed) => { + if (failed?.length) { frappe.throw( __("Cannot {0} {1}", [action, failed.map((f) => f.bold()).join(", ")]) ); } - if (failed.length < docnames.length) { + if (failed?.length < docnames.length) { frappe.utils.play_sound(action); if (done) done(); } + }) + .finally(() => { + frappe.realtime.task_unsubscribe(task_id); }); } From 9ada76df6c4ed7ba0d5545f2ff95f69f11ef9739 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:30:43 +0100 Subject: [PATCH 4/6] chore: deprecate old show_progress --- frappe/desk/doctype/bulk_update/bulk_update.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frappe/desk/doctype/bulk_update/bulk_update.py b/frappe/desk/doctype/bulk_update/bulk_update.py index 9ec7e44ae8..a0f5a45326 100644 --- a/frappe/desk/doctype/bulk_update/bulk_update.py +++ b/frappe/desk/doctype/bulk_update/bulk_update.py @@ -6,6 +6,7 @@ from frappe import _ from frappe.core.doctype.submission_queue.submission_queue import queue_submission from frappe.model.document import Document from frappe.utils import cint +from frappe.utils.deprecations import deprecated from frappe.utils.scheduler import is_scheduler_inactive @@ -112,6 +113,7 @@ def _bulk_action(doctype, docnames, action, data, task_id=None): return failed +@deprecated def show_progress(docnames, message, i, description): n = len(docnames) frappe.publish_progress(float(i) * 100 / n, title=message, description=description) From 54a83892bbb3d2d7cb546729678ee3e4465f283a Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:37:02 +0100 Subject: [PATCH 5/6] fix: better error message --- frappe/public/js/frappe/list/bulk_operations.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frappe/public/js/frappe/list/bulk_operations.js b/frappe/public/js/frappe/list/bulk_operations.js index 863c0abb90..41fed778db 100644 --- a/frappe/public/js/frappe/list/bulk_operations.js +++ b/frappe/public/js/frappe/list/bulk_operations.js @@ -220,9 +220,17 @@ export default class BulkOperations { }) .then((failed) => { if (failed?.length) { - frappe.throw( - __("Cannot {0} {1}", [action, failed.map((f) => f.bold()).join(", ")]) - ); + const comma_separated_records = frappe.utils.comma_and(failed); + switch (action) { + case "submit": + frappe.throw(__("Cannot submit {0}.", [comma_separated_records])); + break; + case "cancel": + frappe.throw(__("Cannot cancel {0}.", [comma_separated_records])); + break; + default: + frappe.throw(__("Cannot {0} {1}.", [action, comma_separated_records])); + } } if (failed?.length < docnames.length) { frappe.utils.play_sound(action); From 68c0e6f85f36be71928593a2fbaf756115f22462 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:39:47 +0100 Subject: [PATCH 6/6] refactor: better parameter name --- frappe/public/js/frappe/list/bulk_operations.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frappe/public/js/frappe/list/bulk_operations.js b/frappe/public/js/frappe/list/bulk_operations.js index 41fed778db..22cf51166d 100644 --- a/frappe/public/js/frappe/list/bulk_operations.js +++ b/frappe/public/js/frappe/list/bulk_operations.js @@ -218,9 +218,9 @@ export default class BulkOperations { docnames: docnames, task_id: task_id, }) - .then((failed) => { - if (failed?.length) { - const comma_separated_records = frappe.utils.comma_and(failed); + .then((failed_docnames) => { + if (failed_docnames?.length) { + const comma_separated_records = frappe.utils.comma_and(failed_docnames); switch (action) { case "submit": frappe.throw(__("Cannot submit {0}.", [comma_separated_records])); @@ -232,7 +232,7 @@ export default class BulkOperations { frappe.throw(__("Cannot {0} {1}.", [action, comma_separated_records])); } } - if (failed?.length < docnames.length) { + if (failed_docnames?.length < docnames.length) { frappe.utils.play_sound(action); if (done) done(); }