From a812c5750cdb158a22868bc9c9928f17a34a1986 Mon Sep 17 00:00:00 2001 From: pkfec Date: Mon, 30 Dec 2024 14:25:40 -0500 Subject: [PATCH] validate ao_is_pending boolean type --- webservices/filters.py | 28 ++++++++++++++++++---------- webservices/resources/legal.py | 20 ++++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/webservices/filters.py b/webservices/filters.py index 05f555c0f..a036b330d 100644 --- a/webservices/filters.py +++ b/webservices/filters.py @@ -185,17 +185,25 @@ def get_cycle(kwargs): return kwargs['cycle'] -ao_requestor_type_valid_filter_values = ['1', '2', '3', '4', '5', '6', '7', '8', '9', - '10', '11', '12', '13', '14', '15', '16'] +def validate_multiselect_filter(filter, valid_values): + valid_results = [] + # Validate each value in filter + for value in filter: + if isinstance(value, str) and ' ' not in value: + if value in valid_values: + valid_results.append(value) + return valid_results -def validate_ao_requestor_type(ao_requestor_type): - valid_values = [] - # Validate each value in ao_requestor_type - for value in ao_requestor_type: - if isinstance(value, str) and ' ' not in value: - if value in ao_requestor_type_valid_filter_values: - valid_values.append(value) +def validate_boolean_dropdown(value): + # If value is None or an empty string, treat it as False + if value is None or value == "": + return False + + # If the value is a boolean, return it as is + if isinstance(value, bool): + return value - return valid_values + # Return False if the value is not a boolean, None, or empty string + return False diff --git a/webservices/resources/legal.py b/webservices/resources/legal.py index 31b2f86bd..840555635 100644 --- a/webservices/resources/legal.py +++ b/webservices/resources/legal.py @@ -26,7 +26,7 @@ logger = logging.getLogger(__name__) # To debug, uncomment the line below: -# logger.setLevel(logging.DEBUG) +logger.setLevel(logging.DEBUG) es_client = create_es_client() @@ -604,8 +604,12 @@ def apply_ao_specific_query_params(query, **kwargs): if check_filter_exists(kwargs, "ao_name"): must_clauses.append(Q("match", name=" ".join(kwargs.get("ao_name")))) - if kwargs.get("ao_is_pending") is not None: - must_clauses.append(Q("term", is_pending=kwargs.get("ao_is_pending"))) + # if kwargs.get("ao_is_pending") is not None: + # must_clauses.append(Q("term", is_pending=kwargs.get("ao_is_pending"))) + + ao_pending_boolean = kwargs.get("ao_is_pending") + if filters.validate_boolean_dropdown(ao_pending_boolean): + must_clauses.append(Q("term", is_pending=ao_pending_boolean)) if kwargs.get("ao_status"): must_clauses.append(Q("match", status=kwargs.get("ao_status"))) @@ -688,9 +692,12 @@ def apply_ao_specific_query_params(query, **kwargs): # Get 'ao_requestor_type' from kwargs ao_requestor_type = kwargs.get("ao_requestor_type", []) + ao_requestor_type_valid_values = ['1', '2', '3', '4', '5', '6', '7', '8', '9', + '10', '11', '12', '13', '14', '15', '16'] - # Validate 'ao_requestor_type' - valid_requestor_types = filters.validate_ao_requestor_type(ao_requestor_type) + # Validate 'ao_requestor_type filter' + valid_requestor_types = filters.validate_multiselect_filter( + ao_requestor_type, ao_requestor_type_valid_values) # Always include valid values in the query construction if valid_requestor_types: @@ -788,7 +795,8 @@ def execute_query(query): def check_filter_exists(kwargs, filter): if kwargs.get(filter): for val in kwargs.get(filter): - if len(val) > 0: + # if len(val) > 0: + if (len(val) > 0) and ('' not in val): return True return False else: