fix: use get_cached_value to avoid db call with db.exists

This commit is contained in:
Daizy Modi 2022-11-17 19:14:10 +05:30
parent 678a4c33da
commit fdfe5cbf93
3 changed files with 15 additions and 9 deletions

View File

@ -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")
)

View File

@ -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))

View File

@ -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