From 31af09f010bc3a0c0f263e8747bd453fb3d55740 Mon Sep 17 00:00:00 2001 From: Bernhard Koschicek-Krombholz Date: Tue, 10 Dec 2024 15:42:34 +0100 Subject: [PATCH] wip --- openatlas/api/endpoints/endpoint.py | 29 +++++++++++++++++------- openatlas/api/formats/csv.py | 35 ++++++++--------------------- openatlas/models/entity.py | 7 ++++++ 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/openatlas/api/endpoints/endpoint.py b/openatlas/api/endpoints/endpoint.py index 3c2f6f4f1..b1174e8ba 100644 --- a/openatlas/api/endpoints/endpoint.py +++ b/openatlas/api/endpoints/endpoint.py @@ -11,15 +11,14 @@ from openatlas import app from openatlas.api.endpoints.parser import Parser from openatlas.api.formats.csv import ( - build_dataframe, build_dataframe_network, build_link_dataframe) + build_dataframe_with_relations, build_dataframe, build_link_dataframe) from openatlas.api.formats.loud import get_loud_entities from openatlas.api.resources.resolve_endpoints import ( download, parse_loud_context) from openatlas.api.resources.templates import ( geojson_collection_template, geojson_pagination, linked_place_pagination, linked_places_template, loud_pagination, loud_template) -from openatlas.api.resources.util import ( - get_linked_entities_api, get_location_link) +from openatlas.api.resources.util import get_location_link from openatlas.models.entity import Entity, Link @@ -84,7 +83,15 @@ def resolve_entities(self) -> Response | dict[str, Any]: if self.parser.search: self.entities = [ e for e in self.entities if self.parser.search_filter(e)] - self.remove_duplicate_entities() + before = len(self.entities) + # self.remove_duplicate_entities() + self.entities = set(self.entities) + after = len(self.entities) + if before != after: + print(before) + print(after) + self.entities = list(self.entities) + if self.parser.count == 'true': return jsonify(len(self.entities)) self.sort_entities() @@ -132,7 +139,7 @@ def filter_by_type(self) -> list[Entity]: def export_entities_csv(self) -> Response: frames = [ - build_dataframe(e, relations=True) + build_dataframe_with_relations(e) for e in self.entities_with_links.values()] return Response( pd.DataFrame(data=frames).to_csv(), @@ -177,7 +184,7 @@ def get_entities_grouped_by_class(self) -> dict[str, Any]: sorted(self.entities, key=lambda entity: entity.class_.name), key=lambda entity: entity.class_.name): grouped_entities[class_] = \ - [build_dataframe_network(entity) for entity in entities_] + [build_dataframe(entity) for entity in entities_] return grouped_entities def link_parser_check(self, inverse: bool = False) -> list[Link]: @@ -202,8 +209,14 @@ def sort_entities(self) -> None: def remove_duplicate_entities(self) -> None: seen: set[int] = set() seen_add = seen.add # Faster than always call seen.add() - self.entities = \ - [e for e in self.entities if not (e.id in seen or seen_add(e.id))] + entities = [] + for e in self.entities: + if e.id not in seen: + seen_add(e.id) + entities.append(e) + self.entities = entities + # self.entities = \ + # [e for e in self.entities if not (e.id in seen or seen_add(e.id))] def get_entities_formatted(self) -> None: entities = [] diff --git a/openatlas/api/formats/csv.py b/openatlas/api/formats/csv.py index d7dc0260e..1798981a1 100644 --- a/openatlas/api/formats/csv.py +++ b/openatlas/api/formats/csv.py @@ -11,36 +11,20 @@ from openatlas.models.gis import Gis -def build_dataframe( - entity_dict: dict[str, Any], - relations: bool = False) -> dict[str, Any]: +def build_dataframe_with_relations( + entity_dict: dict[str, Any]) -> dict[str, Any]: entity = entity_dict['entity'] - geom = get_csv_geom_entry(entity) - data = { - 'id': str(entity.id), - 'name': entity.name, - 'description': entity.description, - 'begin_from': entity.begin_from, - 'begin_to': entity.begin_to, - 'begin_comment': entity.begin_comment, - 'end_from': entity.end_from, - 'end_to': entity.end_to, - 'end_comment': entity.end_comment, - 'cidoc_class': entity.cidoc_class.name, - 'system_class': entity.class_.name, - 'geom_type': geom['type'], - 'coordinates': geom['coordinates']} - if relations: - for key, value in get_csv_links(entity_dict).items(): - data[key] = ' | '.join(list(map(str, value))) - for key, value in get_csv_types(entity_dict).items(): - data[key] = ' | '.join(list(map(str, value))) + data = build_dataframe(entity) + for key, value in get_csv_links(entity_dict).items(): + data[key] = ' | '.join(list(map(str, value))) + for key, value in get_csv_types(entity_dict).items(): + data[key] = ' | '.join(list(map(str, value))) return data -def build_dataframe_network(entity: Entity) -> dict[str, Any]: +def build_dataframe(entity: Entity) -> dict[str, Any]: geom = get_csv_geom_entry(entity) - data = { + return { 'id': str(entity.id), 'name': entity.name, 'description': entity.description, @@ -54,7 +38,6 @@ def build_dataframe_network(entity: Entity) -> dict[str, Any]: 'system_class': entity.class_.name, 'geom_type': geom['type'], 'coordinates': geom['coordinates']} - return data def build_link_dataframe(link: Link) -> dict[str, Any]: diff --git a/openatlas/models/entity.py b/openatlas/models/entity.py index 64d923b31..a5eb31371 100644 --- a/openatlas/models/entity.py +++ b/openatlas/models/entity.py @@ -82,6 +82,12 @@ def __init__(self, data: dict[str, Any]) -> None: self.creator = g.file_info[self.id]['creator'] self.license_holder = g.file_info[self.id]['license_holder'] + def __eq__(self, other): + return self.id == other.id + + def __hash__(self): + return hash(('id', self.id)) + def get_linked_entity( self, code: str, @@ -586,6 +592,7 @@ def __init__( self.last = format_date_part(self.end_to, 'year') \ if self.end_to else self.last + def update(self) -> None: db_link.update({ 'id': self.id,