From 4678d517ae6b166174a7f2d23c832d455df2de0c Mon Sep 17 00:00:00 2001 From: SUBHANKAR DHAR Date: Tue, 16 Jun 2026 15:32:38 +0530 Subject: [PATCH] fix: remove frappe.db.commit() from OnSite WIP server script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Frappe v16's server script sandbox does not allow explicit commits — the framework manages the transaction automatically after the event handler runs. Calling frappe.db.commit() inside a DocType Event script throws AttributeError: module has no attribute 'commit'. Also adds fix_server_script_commit.py to patch already-deployed instances. Co-Authored-By: Claude Sonnet 4.6 --- fix_server_script_commit.py | 61 +++++++++++++++++++++++++++++++++++++ setup_furnitex.py | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 fix_server_script_commit.py diff --git a/fix_server_script_commit.py b/fix_server_script_commit.py new file mode 100644 index 00000000..48569f2a --- /dev/null +++ b/fix_server_script_commit.py @@ -0,0 +1,61 @@ +""" +fix_server_script_commit.py +Removes frappe.db.commit() from the OnSite WIP warehouse server script. +In DocType Event server scripts, Frappe manages the transaction automatically — +calling frappe.db.commit() inside the script throws AttributeError in the sandbox. +Run via: bench --site frontend execute frappe.fix_server_script_commit.run +""" + +import frappe + +SCRIPT_NAME = "Furnitex - Auto Create OnSite WIP Warehouse" + +FIXED_SCRIPT = """\ +# Auto-fires after a new Project is saved +# Creates "{Project Name} - OnSite WIP" warehouse automatically + +project_name = doc.project_name or doc.name +company = doc.company or "Furnitex" +abbr = frappe.db.get_value("Company", company, "abbr") or "F" +wh_short_name = project_name + " - OnSite WIP" +warehouse_name = wh_short_name + " - " + abbr + +root_wh = frappe.db.get_value( + "Warehouse", + {"company": company, "is_group": 1}, + "name" +) or ("All Warehouses - " + abbr) + +if not frappe.db.exists("Warehouse", warehouse_name): + wh = frappe.get_doc({ + "doctype": "Warehouse", + "warehouse_name": wh_short_name, + "parent_warehouse": root_wh, + "company": company, + "is_group": 0, + }) + wh.flags.ignore_permissions = True + wh.insert() + # NOTE: no frappe.db.commit() here — Frappe handles the transaction + frappe.msgprint( + "OnSite WIP Warehouse created: " + warehouse_name + "", + alert=True, indicator="green" + ) +""" + + +def run(): + frappe.set_user("Administrator") + + if not frappe.db.exists("Server Script", SCRIPT_NAME): + print(f" [WARN] Server Script '{SCRIPT_NAME}' not found — nothing to fix.") + return + + doc = frappe.get_doc("Server Script", SCRIPT_NAME) + doc.script = FIXED_SCRIPT + doc.flags.ignore_permissions = True + doc.save() + frappe.db.commit() + + print(f" [OK] Removed frappe.db.commit() from: {SCRIPT_NAME}") + print(" Warehouse auto-creation will now work without AttributeError.") diff --git a/setup_furnitex.py b/setup_furnitex.py index 4deb941a..0e487b1a 100644 --- a/setup_furnitex.py +++ b/setup_furnitex.py @@ -552,7 +552,7 @@ if not frappe.db.exists("Warehouse", warehouse_name): }) wh.flags.ignore_permissions = True wh.insert() - frappe.db.commit() + # No frappe.db.commit() — Frappe manages the transaction in server scripts frappe.msgprint( "OnSite WIP Warehouse created: " + warehouse_name + "", alert=True, indicator="green"