From 8fe870b5af9c15e2ac249e254ded4c68e62d2189 Mon Sep 17 00:00:00 2001 From: Bernhard Koschicek-Krombholz Date: Wed, 2 Oct 2024 13:30:39 +0200 Subject: [PATCH] semifixed valueTypeID search --- openatlas/api/endpoints/parser.py | 14 +++++++++++-- openatlas/api/resources/search.py | 33 +++++++++++-------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/openatlas/api/endpoints/parser.py b/openatlas/api/endpoints/parser.py index f2c416f6d..33fb16059 100644 --- a/openatlas/api/endpoints/parser.py +++ b/openatlas/api/endpoints/parser.py @@ -99,10 +99,15 @@ def set_search_param(self) -> None: for category, value_list in search.items(): for values in value_list: is_comparable= False + links = [] if check_if_date_search(category): is_comparable = True if category == 'valueTypeID': is_comparable = True + for value in values["values"]: + links.append(Entity.get_links_of_entities( + value[0], + inverse=True)) self.search_param.append({ "search_values": get_search_values( category, @@ -110,9 +115,14 @@ def set_search_param(self) -> None: "logical_operator": values['logicalOperator'], "operator": values['operator'], "category": category, - "is_comparable": is_comparable}) - + "is_comparable": is_comparable, + "value_type_links": + flatten_list_and_remove_duplicates(links)}) + # Todo: fix multiple valueTypeID searches, e.g. + # search={"valueTypeID":[{"operator":"equal","values":[(150412,34)], + # "logicalOperator":"or"}]}&search={"valueTypeID":[{"operator":"equal", + # "values":[(131994,13.5),(131997,3.7)],"logicalOperator":"or"}]} def search_filter(self, entity: Entity) -> bool: for param in self.search_param: if not search_entity(entity, param): diff --git a/openatlas/api/resources/search.py b/openatlas/api/resources/search.py index 4e7798d62..584cf1300 100644 --- a/openatlas/api/resources/search.py +++ b/openatlas/api/resources/search.py @@ -5,7 +5,7 @@ from openatlas.api.resources.util import ( flatten_list_and_remove_duplicates, get_linked_entities_id_api) -from openatlas.models.entity import Entity +from openatlas.models.entity import Entity, Link def get_search_values( @@ -18,10 +18,8 @@ def get_search_values( case "relationToID": values = flatten_list_and_remove_duplicates( [get_linked_entities_id_api(value) for value in values]) - # case "valueTypeID": - # values = flatten_list_and_remove_duplicates( - # [search_for_value_type(value, parameter) for value in - # values]) + case "valueTypeID": + values = [value[1] for value in values] case _: values = [ value.lower() if isinstance(value, str) else value @@ -32,19 +30,13 @@ def get_search_values( -def search_entity(entity: Entity, param: dict[str, Any]) -> bool: +def search_entity(entity: Entity | Link, param: dict[str, Any]) -> bool: entity_values = value_to_be_searched(entity, param) operator_ = param['operator'] search_values = param['search_values'] logical_operator = param['logical_operator'] is_comparable = param['is_comparable'] - # Problem is, that we get not only one value to compare. - # How compare multiple? - if param['category'] == 'valueTypeID': - search_values = [value[1] for value in search_values] - print(search_values) - if not entity_values and (operator_ == 'like' or is_comparable): return False @@ -63,7 +55,8 @@ def search_entity(entity: Entity, param: dict[str, Any]) -> bool: bool_values.append(bool(not any( item in entity_values for item in search_value))) return all(bool_values) - + if entity_values: + print(entity_values) bool_ = False match operator_: case 'equal' if logical_operator == 'or': @@ -107,7 +100,7 @@ def value_to_be_searched( -> list[int | str | float] | Optional[datetime64] | Optional[float]: value: list[int | str | float] | Optional[datetime64]| Optional[float] = [] match param['category']: - case "entityID" | "relationToID" | "valueTypeID": + case "entityID" | "relationToID": value = [entity.id] case "entityName": value = [entity.name.lower()] @@ -134,14 +127,10 @@ def value_to_be_searched( case "endTo": value = entity.end_to case "valueTypeID": - links = Entity.get_links_of_entities( - param['values'][0], - inverse=True) - for link_ in links: - if link_.description: - value = [float(entity.description)] \ - if param['operator'] in ['equal', 'notEqual'] \ - else float(entity.description) + value = [] + for link_ in param['value_type_links']: + if entity.id == link_.domain.id: + value.append(float(link_.description)) case _: value = [] return value