Merge pull request #5309 from achillesrasquinha/cast-singles

cast singles fieldtype
This commit is contained in:
Achilles Rasquinha 2018-03-29 13:22:22 +05:30 committed by GitHub
commit 6fcd9ad18e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 26 deletions

View file

@ -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
@ -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,6 +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 = _cast_result(doctype, r)
if as_dict:
if r:
@ -556,7 +570,7 @@ class Database:
else:
return r and [[i[1] for i in r]] or []
def get_singles_dict(self, doctype):
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 +580,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 = _cast_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)

View file

@ -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
@ -758,27 +758,8 @@ 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
def cast(self, value, df):
return cast_fieldtype(df.fieldtype, value)
def _extract_images_from_text_editor(self):
from frappe.utils.file_manager import extract_images_from_doc

View file

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