def custom_conditions_query_dict()

in services/ui_backend_service/api/utils.py [0:0]


def custom_conditions_query_dict(query: MultiDict, allowed_keys: List[str] = []):
    conditions = []
    values = []

    for key, val in query.items():
        if key.startswith("_"):
            continue

        deconstruct = key.split(":", 1)
        if len(deconstruct) > 1:
            field = deconstruct[0]
            operator = deconstruct[1]
        else:
            field = key
            operator = "eq"

        if allowed_keys is not None and field not in allowed_keys:
            continue

        if operator not in operators_to_sql:
            continue

        vals = val.split(",")

        conditions.append(
            "({})".format(" OR ".join(
                map(lambda v: operators_to_sql["is" if v == "null" else operator].format(field), vals)
            ))
        )
        values += map(
            lambda v: None if v == "null" else operators_to_sql_values[operator].format(v), vals)

    return conditions, values