From 05ad109bcb880ddf075d185fb3594bda46c9a4f4 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 09:56:44 +0530 Subject: [PATCH 1/8] cast singles fieldtype --- frappe/database.py | 35 +++++++++++++++++++++++++++++++---- frappe/model/base_document.py | 23 ++--------------------- frappe/utils/__init__.py | 22 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/frappe/database.py b/frappe/database.py index 939a82fea7..f0b55591c1 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -12,7 +12,7 @@ import frappe.defaults import frappe.async import re import frappe.model.meta -from frappe.utils import now, get_datetime, cstr +from frappe.utils import now, get_datetime, cstr, cast_fieldtype from frappe import _ from frappe.model.utils.link_count import flush_local_link_count from frappe.utils.background_jobs import execute_job, get_queue @@ -544,6 +544,7 @@ class Database: from tabSingles where field in (%s) and doctype=%s""" \ % (', '.join(['%s'] * len(fields)), '%s'), tuple(fields) + (doctype,), as_dict=False, debug=debug) + r = self._cast_singles_result(doctype, result) if as_dict: if r: @@ -556,7 +557,26 @@ class Database: else: return r and [[i[1] for i in r]] or [] - def get_singles_dict(self, doctype): + def _cast_singles_result(self, doctype, result): + batch = [ ] + + for field, value in result: + docfields = self.sql(""" + SELECT fieldname, fieldtype + FROM `tabDocField` + WHERE parent = %s + AND fieldname = %s + """, (doctype, field)) + + if docfields: + field, type_ = docfields[0] + value = cast_fieldtype(type_, value) + + batch.append(tuple([field, value])) + + return batch + + def get_singles_dict(self, doctype, debug = False): """Get Single DocType as dict. :param doctype: DocType of the single object whose value is requested @@ -566,9 +586,16 @@ class Database: # Get coulmn and value of the single doctype Accounts Settings account_settings = frappe.db.get_singles_dict("Accounts Settings") """ + result = self.sql(""" + SELECT field, value + FROM `tabSingles` + WHERE doctype = %s + """, doctype) + result = self._cast_singles_result(doctype, result) - return frappe._dict(self.sql("""select field, value from - tabSingles where doctype=%s""", doctype)) + dict_ = frappe._dict(result) + + return dict_ def get_all(self, *args, **kwargs): return frappe.get_all(*args, **kwargs) diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index f743b881be..36675e1f31 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -7,7 +7,7 @@ import datetime import frappe, sys from frappe import _ from frappe.utils import (cint, flt, now, cstr, strip_html, getdate, get_datetime, to_timedelta, - sanitize_html, sanitize_email) + sanitize_html, sanitize_email, cast_fieldtype) from frappe.model import default_fields from frappe.model.naming import set_new_name from frappe.model.utils.link_count import notify_link_count @@ -759,26 +759,7 @@ class BaseDocument(object): return self.cast(val, df) def cast(self, val, df): - if df.fieldtype in ("Currency", "Float", "Percent"): - val = flt(val) - - elif df.fieldtype in ("Int", "Check"): - val = cint(val) - - elif df.fieldtype in ("Data", "Text", "Small Text", "Long Text", - "Text Editor", "Select", "Link", "Dynamic Link"): - val = cstr(val) - - elif df.fieldtype == "Date": - val = getdate(val) - - elif df.fieldtype == "Datetime": - val = get_datetime(val) - - elif df.fieldtype == "Time": - val = to_timedelta(val) - - return val + return cast_fieldtype(df.fieldtype, value) def _extract_images_from_text_editor(self): from frappe.utils.file_manager import extract_images_from_doc diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index 9c384e00d5..b7258c5330 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -556,3 +556,25 @@ def parse_json(val): if isinstance(val, string_types): return json.loads(val) return val + +def cast_fieldtype(fieldtype, value): + if fieldtype in ("Currency", "Float", "Percent"): + value = flt(value) + + elif fieldtype in ("Int", "Check"): + value = cint(value) + + elif fieldtype in ("Data", "Text", "Small Text", "Long Text", + "Text Editor", "Select", "Link", "Dynamic Link"): + value = cstr(value) + + elif fieldtype == "Date": + value = getdate(value) + + elif fieldtype == "Datetime": + value = get_datetime(value) + + elif fieldtype == "Time": + value = to_timedelta(value) + + return value \ No newline at end of file From 8b5d24f89a9a9b1a62326af49a808b3fd39e1eec Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 10:21:00 +0530 Subject: [PATCH 2/8] fix minor variable call --- frappe/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/database.py b/frappe/database.py index f0b55591c1..5ff4d2f004 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -544,7 +544,7 @@ class Database: from tabSingles where field in (%s) and doctype=%s""" \ % (', '.join(['%s'] * len(fields)), '%s'), tuple(fields) + (doctype,), as_dict=False, debug=debug) - r = self._cast_singles_result(doctype, result) + r = self._cast_singles_result(doctype, r) if as_dict: if r: From 6340b5c4bf78c02d4c03533da6bfe72c0d4f8ac1 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 10:36:41 +0530 Subject: [PATCH 3/8] val to value --- frappe/model/base_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index 36675e1f31..2d2e9947f4 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -758,7 +758,7 @@ class BaseDocument(object): return self.cast(val, df) - def cast(self, val, df): + def cast(self, value, df): return cast_fieldtype(df.fieldtype, value) def _extract_images_from_text_editor(self): From 3d0064332c633aa192840e90b79c2fd721aa2e66 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 11:02:53 +0530 Subject: [PATCH 4/8] get_meta alternative --- frappe/database.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/frappe/database.py b/frappe/database.py index 5ff4d2f004..96e8f3f645 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -561,16 +561,9 @@ class Database: batch = [ ] for field, value in result: - docfields = self.sql(""" - SELECT fieldname, fieldtype - FROM `tabDocField` - WHERE parent = %s - AND fieldname = %s - """, (doctype, field)) - - if docfields: - field, type_ = docfields[0] - value = cast_fieldtype(type_, value) + df = frappe.get_meta(doctype).get_field(field) + if df: + value = cast_fieldtype(df.fieldtype, value) batch.append(tuple([field, value])) From 062c7fa283e8e1f694e79476778febff9ad8d62e Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 11:06:48 +0530 Subject: [PATCH 5/8] fixed codacy --- frappe/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index b7258c5330..ea100cb863 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -558,7 +558,7 @@ def parse_json(val): return val def cast_fieldtype(fieldtype, value): - if fieldtype in ("Currency", "Float", "Percent"): + if fieldtype in ("Currency", "Float", "Percent"): value = flt(value) elif fieldtype in ("Int", "Check"): From d4afe75e7ed9a09d24f4904cd1bbf845e63141d3 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 11:14:52 +0530 Subject: [PATCH 6/8] return immutable --- frappe/database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/database.py b/frappe/database.py index 96e8f3f645..4aa98df665 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -567,7 +567,7 @@ class Database: batch.append(tuple([field, value])) - return batch + return tuple(batch) def get_singles_dict(self, doctype, debug = False): """Get Single DocType as dict. From 5a3682a5867db5bfa924f623b88ba956677c6c57 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 11:27:51 +0530 Subject: [PATCH 7/8] fix codacy --- frappe/database.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frappe/database.py b/frappe/database.py index 4aa98df665..fe827aad39 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -544,7 +544,7 @@ class Database: from tabSingles where field in (%s) and doctype=%s""" \ % (', '.join(['%s'] * len(fields)), '%s'), tuple(fields) + (doctype,), as_dict=False, debug=debug) - r = self._cast_singles_result(doctype, r) + r = self._cast_result(doctype, r) if as_dict: if r: @@ -557,7 +557,7 @@ class Database: else: return r and [[i[1] for i in r]] or [] - def _cast_singles_result(self, doctype, result): + def _cast_result(doctype, result): batch = [ ] for field, value in result: @@ -584,7 +584,7 @@ class Database: FROM `tabSingles` WHERE doctype = %s """, doctype) - result = self._cast_singles_result(doctype, result) + result = self._cast_result(doctype, result) dict_ = frappe._dict(result) From 608b3972ef645b513efabb9eccb44f4271a85514 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Thu, 29 Mar 2018 11:41:46 +0530 Subject: [PATCH 8/8] move helper outside class definition --- frappe/database.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/frappe/database.py b/frappe/database.py index fe827aad39..6a119cff4f 100644 --- a/frappe/database.py +++ b/frappe/database.py @@ -35,6 +35,19 @@ from pymysql.constants import ER, FIELD_TYPE from pymysql.converters import conversions import pymysql +# Helpers +def _cast_result(doctype, result): + batch = [ ] + + for field, value in result: + df = frappe.get_meta(doctype).get_field(field) + if df: + value = cast_fieldtype(df.fieldtype, value) + + batch.append(tuple([field, value])) + + return tuple(batch) + class Database: """ Open a database connection with the given parmeters, if use_default is True, use the @@ -544,7 +557,7 @@ class Database: from tabSingles where field in (%s) and doctype=%s""" \ % (', '.join(['%s'] * len(fields)), '%s'), tuple(fields) + (doctype,), as_dict=False, debug=debug) - r = self._cast_result(doctype, r) + r = _cast_result(doctype, r) if as_dict: if r: @@ -557,18 +570,6 @@ class Database: else: return r and [[i[1] for i in r]] or [] - def _cast_result(doctype, result): - batch = [ ] - - for field, value in result: - df = frappe.get_meta(doctype).get_field(field) - if df: - value = cast_fieldtype(df.fieldtype, value) - - batch.append(tuple([field, value])) - - return tuple(batch) - def get_singles_dict(self, doctype, debug = False): """Get Single DocType as dict. @@ -584,7 +585,7 @@ class Database: FROM `tabSingles` WHERE doctype = %s """, doctype) - result = self._cast_result(doctype, result) + result = _cast_result(doctype, result) dict_ = frappe._dict(result)