Undo replace of frappe.db.sql with frappe.get_list (#14074)

This commit is contained in:
Suraj Shetty 2018-05-16 13:53:31 +05:30 committed by Faris Ansari
parent 265005d30d
commit fbb6b3da5f
6 changed files with 72 additions and 75 deletions

View File

@ -681,23 +681,29 @@ def get_companies():
def get_children(doctype, parent, company, is_root=False): def get_children(doctype, parent, company, is_root=False):
from erpnext.accounts.report.financial_statements import sort_accounts from erpnext.accounts.report.financial_statements import sort_accounts
parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_') fieldname = frappe.db.escape(doctype.lower().replace(' ','_'))
fields = [ doctype = frappe.db.escape(doctype)
'name as value',
'is_group as expandable' # root
]
filters = [['docstatus', '<', 2]]
if is_root: if is_root:
fields += ['root_type', 'report_type', 'account_currency'] if doctype == 'Account' else [] fields = ", root_type, report_type, account_currency" if doctype=="Account" else ""
filters.append([parent_fieldname, '=', '']) acc = frappe.db.sql(""" select
filters.append(['company', '=', company]) name as value, is_group as expandable {fields}
from `tab{doctype}`
where ifnull(`parent_{fieldname}`,'') = ''
and `company` = %s and docstatus<2
order by name""".format(fields=fields, fieldname = fieldname, doctype=doctype),
company, as_dict=1)
else: else:
fields += ['account_currency'] if doctype == 'Account' else [] # other
fields += [parent_fieldname + ' as parent'] fields = ", account_currency" if doctype=="Account" else ""
acc = frappe.db.sql("""select
name as value, is_group as expandable, parent_{fieldname} as parent {fields}
acc = frappe.get_list(doctype, fields=fields, filters=filters) from `tab{doctype}`
where ifnull(`parent_{fieldname}`,'') = %s
and docstatus<2
order by name""".format(fields=fields, fieldname=fieldname, doctype=doctype),
parent, as_dict=1)
if doctype == 'Account': if doctype == 'Account':
sort_accounts(acc, is_root, key="value") sort_accounts(acc, is_root, key="value")

View File

@ -169,10 +169,11 @@ def get_children(doctype, parent, is_root=False):
if is_root: if is_root:
parent = '' parent = ''
land_units = frappe.get_list(doctype, land_units = frappe.db.sql("""select name as value,
fields = ['name as value', 'is_group as expandable'], is_group as expandable
filters= [['parent_land_unit', '=', parent]], from `tabLand Unit`
order_by='name') where ifnull(`parent_land_unit`,'') = %s
order by name""", (parent), as_dict=1)
# return nodes # return nodes
return land_units return land_units

View File

@ -318,26 +318,26 @@ def get_employee_emails(employee_list):
@frappe.whitelist() @frappe.whitelist()
def get_children(doctype, parent=None, company=None, is_root=False, is_tree=False): def get_children(doctype, parent=None, company=None, is_root=False, is_tree=False):
filters = [['company', '=', company]] condition = ''
fields = ['name as value', 'employee_name as title']
if is_root: if is_root:
parent = '' parent = ""
if parent and company and parent!=company: if parent and company and parent!=company:
filters.append(['reports_to', '=', parent]) condition = ' and reports_to = "{0}"'.format(frappe.db.escape(parent))
else: else:
filters.append(['reports_to', '=', '']) condition = ' and ifnull(reports_to, "")=""'
employees = frappe.get_list(doctype, fields=fields, employee = frappe.db.sql("""
filters=filters, order_by='name') select
name as value, employee_name as title,
exists(select name from `tabEmployee` where reports_to=emp.name) as expandable
from
`tabEmployee` emp
where company='{company}' {condition} order by name"""
.format(company=company, condition=condition), as_dict=1)
for employee in employees: # return employee
is_expandable = frappe.get_all(doctype, filters=[ return employee
['reports_to', '=', employee.get('value')]
])
employee.expandable = 1 if is_expandable else 0
return employees
def on_doctype_update(): def on_doctype_update():

View File

@ -624,28 +624,18 @@ def get_children(doctype, parent=None, is_root=False, **filters):
return return
if frappe.form_dict.parent: if frappe.form_dict.parent:
bom_items = frappe.get_list('BOM Item', return frappe.db.sql("""select
fields=['item_code', 'bom_no as value', 'stock_qty'], bom_item.item_code,
filters=[['parent', '=', frappe.form_dict.parent]], bom_item.bom_no as value,
order_by='idx') bom_item.stock_qty,
if(ifnull(bom_item.bom_no, "")!="", 1, 0) as expandable,
item_names = tuple(d.get('item_code') for d in bom_items) item.image,
item.description
items = frappe.get_list('Item', from `tabBOM Item` bom_item, tabItem item
fields=['image', 'description', 'name'], where bom_item.parent=%s
filters=[['name', 'in', item_names]]) # to get only required item dicts and bom_item.item_code = item.name
order by bom_item.idx
for bom_item in bom_items: """, frappe.form_dict.parent, as_dict=True)
# extend bom_item dict with respective item dict
bom_item.update(
# returns an item dict from items list which matches with item_code
(item for item in items if item.get('name')
== bom_item.get('item_code')).next()
)
bom_item.expandable = 0 if bom_item.value in ('', None) else 1
return bom_items
def get_boms_in_bottom_up_order(bom_no=None): def get_boms_in_bottom_up_order(bom_no=None):
def _get_parent(bom_no): def _get_parent(bom_no):

View File

@ -186,25 +186,27 @@ def set_tasks_as_overdue():
@frappe.whitelist() @frappe.whitelist()
def get_children(doctype, parent, task=None, project=None, is_root=False): def get_children(doctype, parent, task=None, project=None, is_root=False):
conditions = ''
filters = [['docstatus', '<', '2']]
if task: if task:
filters.append(['parent_task', '=', task]) # via filters
conditions += ' and parent_task = "{0}"'.format(frappe.db.escape(task))
elif parent and not is_root: elif parent and not is_root:
# via expand child # via expand child
filters.append(['parent_task', '=', parent]) conditions += ' and parent_task = "{0}"'.format(frappe.db.escape(parent))
else: else:
filters.append(['parent_task', '=', '']) conditions += ' and ifnull(parent_task, "")=""'
if project: if project:
filters.append(['project', '=', project]) conditions += ' and project = "{0}"'.format(frappe.db.escape(project))
tasks = frappe.get_list(doctype, fields=[ tasks = frappe.db.sql("""select name as value,
'name as value', subject as title,
'subject as title', is_group as expandable
'is_group as expandable' from `tabTask`
], filters=filters, order_by='name') where docstatus < 2
{conditions}
order by name""".format(conditions=conditions), as_dict=1)
# return tasks # return tasks
return tasks return tasks

View File

@ -144,19 +144,17 @@ def get_children(doctype, parent=None, company=None, is_root=False):
if is_root: if is_root:
parent = "" parent = ""
fields = ['name as value', 'is_group as expandable'] warehouses = frappe.db.sql("""select name as value,
filters = [ is_group as expandable
['docstatus', '<', '2'], from `tabWarehouse`
['parent_warehouse', '=', parent], where docstatus < 2
['company', 'in', (company, None,'')] and ifnull(`parent_warehouse`,'') = %s
] and (`company` = %s or company is null or company = '')
order by name""", (parent, company), as_dict=1)
warehouses = frappe.get_list(doctype, fields=fields, filters=filters, order_by='name')
# return warehouses # return warehouses
for wh in warehouses: for wh in warehouses:
wh["balance"] = get_stock_value_on(warehouse=wh.value) wh["balance"] = get_stock_value_on(warehouse=wh.value)
return warehouses return warehouses
@frappe.whitelist() @frappe.whitelist()