Skip to content

Instantly share code, notes, and snippets.

@netchampfaris
Created March 3, 2019 17:55
Show Gist options
  • Save netchampfaris/d79186c908e309771e301eaf79375afe to your computer and use it in GitHub Desktop.
Save netchampfaris/d79186c908e309771e301eaf79375afe to your computer and use it in GitHub Desktop.
def nested_filters():
sql_string = parse_nested_filters('Item',
['and', [
['has_variant', '=', 1],
['show_in_website', '=', 0],
['or', [
['variant_of', '=', 'test'],
['item_group', '=', 'product'],
['and', [
['item_code', '>', 'hello'],
['item_code', '<', 'test']
]]
]]
]]
)
print(sql_string)
def parse_nested_filters(doctype, nested_filters, operator=None):
filter_string = ''
if operator:
simple_filters = [f for f in nested_filters if len(f) > 2]
nested_filters = [f for f in nested_filters if len(f) == 2]
simple_conditions = get_conditions(doctype, simple_filters)
nested_conditions = [parse_nested_filters(doctype, f) for f in nested_filters]
filter_string += '(' + (' ' + operator + ' ').join(simple_conditions + nested_conditions) + ')'
return filter_string
for val in nested_filters:
if isinstance(val, frappe.string_types):
operator = val
else:
filter_string = parse_nested_filters(doctype, val, operator)
return filter_string
def get_conditions(filter_list):
from frappe.model.db_query import DatabaseQuery
conditions = []
DatabaseQuery('Item').build_filter_conditions(filter_list, conditions)
return conditions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment