From fdfe5cbf9379e85f4c15fd4859e0a3cf8a571aa1 Mon Sep 17 00:00:00 2001 From: Daizy Modi Date: Thu, 17 Nov 2022 19:14:10 +0530 Subject: [PATCH] fix: use `get_cached_value` to avoid db call with `db.exists` --- erpnext/accounts/doctype/account/account.py | 14 +++++++++----- .../report/general_ledger/general_ledger.py | 8 +++++--- erpnext/accounts/utils.py | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py index 8beb01b2cf..17c96b6106 100644 --- a/erpnext/accounts/doctype/account/account.py +++ b/erpnext/accounts/doctype/account/account.py @@ -114,8 +114,9 @@ class Account(NestedSet): def validate_root_details(self): # does not exists parent - if frappe.db.exists("Account", self.name): - if not frappe.get_cached_value("Account", self.name, "parent_account"): + account_values = frappe.get_cached_value("Account", self.name, ["parent_account"], as_dict=1) + if account_values: + if not account_values.parent_account: throw(_("Root cannot be edited."), RootNotEditable) if not self.parent_account and not self.is_group: @@ -439,10 +440,13 @@ def update_account_number(name, account_name, account_number=None, from_descenda @frappe.whitelist() def merge_account(old, new, is_group, root_type, company): # Validate properties before merging - if not frappe.db.exists("Account", new): + account_data = frappe.get_cached_value( + "Account", new, ["is_group", "root_type", "company", "parent_account"], as_dict=True + ) + if not account_data: throw(_("Account {0} does not exist").format(new)) - val = list(frappe.get_cached_value("Account", new, ["is_group", "root_type", "company"])) + val = list(account_data.values())[:3] # is_group, root_type, company if val != [cint(is_group), root_type, company]: throw( @@ -451,7 +455,7 @@ def merge_account(old, new, is_group, root_type, company): ) ) - if is_group and frappe.get_cached_value("Account", new, "parent_account") == old: + if is_group and account_data.parent_account == old: frappe.db.set_value( "Account", new, "parent_account", frappe.get_cached_value("Account", old, "parent_account") ) diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py index 615db29267..2dfb5d4627 100644 --- a/erpnext/accounts/report/general_ledger/general_ledger.py +++ b/erpnext/accounts/report/general_ledger/general_ledger.py @@ -286,9 +286,11 @@ def get_accounts_with_children(accounts): all_accounts = [] for d in accounts: - if frappe.db.exists("Account", d): - lft, rgt = frappe.get_cached_value("Account", d, ["lft", "rgt"]) - children = frappe.get_all("Account", filters={"lft": [">=", lft], "rgt": ["<=", rgt]}) + account_data = frappe.get_cached_value("Account", d, ["lft", "rgt"], as_dict=1) + if account_data: + children = frappe.get_all( + "Account", filters={"lft": [">=", account_data.lft], "rgt": ["<=", account_data.rgt]} + ) all_accounts += [c.name for c in children] else: frappe.throw(_("Account: {0} does not exist").format(d)) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 4aed7cec11..41702d65b4 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -975,7 +975,7 @@ def get_account_balances(accounts, company): def create_payment_gateway_account(gateway, payment_channel="Email"): from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account - company = frappe.get_cached_value("Global Defaults", None, "default_company") + company = frappe.get_cached_value("Global Defaults", "Global Defaults", "default_company") if not company: return