fix: get currency name from DB only if options are set and value is truthy (#16382)

* fix: call `frappe.db.exists` only if `options` are set and value is truthy

* fix: sider issue

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>

* fix: use `get_value` instead of `exists`

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>

* test: ensure currency formatting works without currency set in df options or param

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
This commit is contained in:
Sagar Vora 2022-03-23 19:28:01 +05:30 committed by GitHub
parent a9b8fe8197
commit 4bb5ea609c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -963,10 +963,13 @@ class BaseDocument(object):
from frappe.model.meta import get_default_df
df = get_default_df(fieldname)
if df.fieldtype == "Currency" and not currency:
currency = self.get(df.get("options"))
if not frappe.db.exists('Currency', currency, cache=True):
currency = None
if (
df.fieldtype == "Currency"
and not currency
and (currency_field := df.get("options"))
and (currency_value := self.get(currency_field))
):
currency = frappe.db.get_value('Currency', currency_value, cache=True)
val = self.get(fieldname)

View file

@ -246,7 +246,7 @@ class TestDocument(unittest.TestCase):
'fields': [
{'label': 'Currency', 'fieldname': 'currency', 'reqd': 1, 'fieldtype': 'Currency'},
]
}).insert()
}).insert(ignore_if_duplicate=True)
frappe.delete_doc_if_exists("Currency", "INR", 1)
@ -262,6 +262,10 @@ class TestDocument(unittest.TestCase):
})
self.assertEqual(d.get_formatted('currency', currency='INR', format="#,###.##"), '₹ 100,000.00')
# should work even if options aren't set in df
# and currency param is not passed
self.assertIn("0", d.get_formatted("currency"))
def test_limit_for_get(self):
doc = frappe.get_doc("DocType", "DocType")
# assuming DocType has more than 3 Data fields