Merge pull request #11313 from rmehta/fix-api-get-value

fix(minor): use get_list in client.get_value for tighter checks
This commit is contained in:
mergify[bot] 2020-08-19 10:07:22 +00:00 committed by GitHub
commit 4ebfff8e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -75,17 +75,23 @@ def get_value(doctype, fieldname, filters=None, as_dict=True, debug=False, paren
filters = get_safe_filters(filters)
try:
fieldname = json.loads(fieldname)
fields = json.loads(fieldname)
except (TypeError, ValueError):
# name passed, not json
pass
fields = [fieldname]
# check whether the used filters were really parseable and usable
# and did not just result in an empty string or dict
if not filters:
filters = None
return frappe.db.get_value(doctype, filters, fieldname, as_dict=as_dict, debug=debug)
value = frappe.get_list(doctype, filters=filters, fields=fields, debug=debug, limit=1)
if as_dict:
value = value[0] if value else {}
else:
value = value[0].fieldname
return value
@frappe.whitelist()
def get_single_value(doctype, field):

View file

@ -4,7 +4,7 @@ from __future__ import unicode_literals
import unittest, frappe, os
from frappe.core.doctype.user.user import generate_keys
from frappe.frappeclient import FrappeClient
from frappe.frappeclient import FrappeClient, FrappeException
from frappe.utils.data import get_url
import requests
@ -56,6 +56,21 @@ class TestAPI(unittest.TestCase):
doc = server.get_doc("Note", "get_this")
self.assertTrue(doc)
def test_get_value(self):
server = FrappeClient(get_url(), "Administrator", "admin", verify=False)
frappe.db.sql("delete from `tabNote` where title = 'get_value'")
frappe.db.commit()
test_content = "test get value"
server.insert_many([
{"doctype": "Note", "public": True, "title": "get_value", "content": test_content},
])
self.assertEqual(server.get_value("Note", "content", {"title": "get_value"}).get('content'), test_content)
self.assertRaises(FrappeException, server.get_value, "Note", "(select (password) from(__Auth) order by name desc limit 1)", {"title": "get_value"})
def test_update_doc(self):
server = FrappeClient(get_url(), "Administrator", "admin", verify=False)
frappe.db.sql("delete from `tabNote` where title in ('Sing','sing')")