Skip to content

Commit

Permalink
validate ao_is_pending boolean type
Browse files Browse the repository at this point in the history
  • Loading branch information
pkfec committed Jan 8, 2025
1 parent 2b3abbd commit a812c57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
28 changes: 18 additions & 10 deletions webservices/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 14 additions & 6 deletions webservices/resources/legal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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")))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a812c57

Please sign in to comment.