changes
This commit is contained in:
53
app/basic.py
53
app/basic.py
@@ -89,8 +89,18 @@ def execute_data_query(name, params):
|
||||
if Model is None:
|
||||
return None
|
||||
|
||||
def parse_param_to_dict(param):
|
||||
if isinstance(param, dict):
|
||||
return param
|
||||
if isinstance(param, str):
|
||||
try:
|
||||
return ast.literal_eval(param)
|
||||
except (ValueError, SyntaxError):
|
||||
return {}
|
||||
return {}
|
||||
|
||||
# Lấy các tham số từ dict `params`
|
||||
filter_str = params.get('filter')
|
||||
filter_param = params.get('filter')
|
||||
values = params.get('values')
|
||||
values = values if values==None else values.split(',')
|
||||
summary = params.get('summary')
|
||||
@@ -99,40 +109,47 @@ def execute_data_query(name, params):
|
||||
sort = params.get('sort')
|
||||
sort = None if sort==None else sort.split(',')
|
||||
distinct_values = params.get('distinct_values')
|
||||
filter_or = params.get('filter_or')
|
||||
exclude = params.get('exclude')
|
||||
filter_or_param = params.get('filter_or')
|
||||
exclude_param = params.get('exclude')
|
||||
calculation = params.get('calculation')
|
||||
final_filter = params.get('final_filter')
|
||||
final_exclude = params.get('final_exclude')
|
||||
final_filter_param = params.get('final_filter')
|
||||
final_exclude_param = params.get('final_exclude')
|
||||
|
||||
# Xây dựng filter_list
|
||||
filter_list = Q()
|
||||
if filter_or != None:
|
||||
for key, value in ast.literal_eval(filter_or).items():
|
||||
filter_or_dict = parse_param_to_dict(filter_or_param)
|
||||
if filter_or_dict:
|
||||
for key, value in filter_or_dict.items():
|
||||
filter_list.add(Q(**{key: value}), Q.OR)
|
||||
|
||||
if filter_str != None:
|
||||
for key, value in ast.literal_eval(filter_str).items():
|
||||
if isinstance(value, dict) == True:
|
||||
if value['type'] == 'F':
|
||||
filter_list.add(Q(**{key: F(value['field'])}), Q.AND)
|
||||
filter_dict = parse_param_to_dict(filter_param)
|
||||
if filter_dict:
|
||||
for key, value in filter_dict.items():
|
||||
if isinstance(value, dict) and value.get('type') == 'F':
|
||||
filter_list.add(Q(**{key: F(value['field'])}), Q.AND)
|
||||
else:
|
||||
filter_list.add(Q(**{key: value}), Q.AND)
|
||||
|
||||
# Thực thi query
|
||||
rows = Model.objects.all() if len(filter_list) == 0 else Model.objects.filter(filter_list)
|
||||
if exclude != None:
|
||||
|
||||
exclude_dict = parse_param_to_dict(exclude_param)
|
||||
if exclude_dict:
|
||||
exclude_list = Q()
|
||||
for key, value in ast.literal_eval(exclude).items():
|
||||
if isinstance(value, dict) == True:
|
||||
if value['type'] == 'F':
|
||||
exclude_list.add(Q(**{key: F(value['field'])}), Q.AND)
|
||||
for key, value in exclude_dict.items():
|
||||
if isinstance(value, dict) and value.get('type') == 'F':
|
||||
exclude_list.add(Q(**{key: F(value['field'])}), Q.AND)
|
||||
else:
|
||||
exclude_list.add(Q(**{key: value}), Q.AND)
|
||||
rows = rows.exclude(exclude_list)
|
||||
|
||||
rows, need_serializer = base_query(rows, values, summary, distinct_values)
|
||||
rows = final_result(rows, calculation, final_filter, final_exclude, sort)
|
||||
|
||||
# We need to parse final_filter and final_exclude here as they are applied in final_result
|
||||
final_filter_dict = parse_param_to_dict(final_filter_param)
|
||||
final_exclude_dict = parse_param_to_dict(final_exclude_param)
|
||||
|
||||
rows = final_result(rows, calculation, final_filter_dict, final_exclude_dict, sort)
|
||||
|
||||
# Initialize total_rows and full_data
|
||||
total_rows = 0
|
||||
|
||||
Reference in New Issue
Block a user