fix: Replace raw query with ORM

This commit is contained in:
Deepesh Garg
2020-11-25 13:42:16 +05:30
parent 509e40a584
commit 08d562a5bb

View File

@@ -499,16 +499,17 @@ def get_filtered_dimensions(doctype, txt, searchfield, start, page_len, filters)
from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import get_dimension_filter_map from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import get_dimension_filter_map
dimension_filters = get_dimension_filter_map() dimension_filters = get_dimension_filter_map()
dimension_filters = dimension_filters.get((filters.get('dimension'),filters.get('account'))) dimension_filters = dimension_filters.get((filters.get('dimension'),filters.get('account')))
group_condition = '' query_filters = []
company_condition = ''
meta = frappe.get_meta(doctype) meta = frappe.get_meta(doctype)
if meta.is_tree: if meta.is_tree:
group_condition = 'and is_group = 0 ' query_filters.append(['is_group', '=', 0])
if meta.has_field('company'): if meta.has_field('company'):
company_condition = 'and company = %s ' % (frappe.db.escape(filters.get('company'))) query_filters.append(['company', '=', filters.get('company')])
if txt:
query_filters.append([searchfield, 'LIKE', "%%%s%%" % txt])
if dimension_filters: if dimension_filters:
if dimension_filters['allow_or_restrict'] == 'Allow': if dimension_filters['allow_or_restrict'] == 'Allow':
@@ -521,25 +522,12 @@ def get_filtered_dimensions(doctype, txt, searchfield, start, page_len, filters)
else: else:
dimensions = tuple(dimension_filters['allowed_dimensions']) dimensions = tuple(dimension_filters['allowed_dimensions'])
result = frappe.db.sql("""SELECT name from `tab{doctype}` where query_filters.append(['name', query_selector, dimensions])
name {query_selector} {restricted}
{group_condition} {company_condition}
and {key} LIKE %(txt)s""".format(
doctype=doctype, query_selector=query_selector, restricted=dimensions,
group_condition = group_condition,
company_condition = company_condition,
key=searchfield), {
'txt': '%' + txt + '%'
})
return result output = frappe.get_all(doctype, filters=query_filters)
else: result = [d.name for d in output]
return frappe.db.sql("""
SELECT name from `tab{doctype}` where return [(d,) for d in set(result)]
{key} LIKE %(txt)s {group_condition} {company_condition}"""
.format(doctype=doctype, key=searchfield,
group_condition=group_condition, company_condition=company_condition),
{ 'txt': '%' + txt + '%'})
@frappe.whitelist() @frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs @frappe.validate_and_sanitize_search_inputs