Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Dec 10, 2024
1 parent 31af09f commit 3037380
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
35 changes: 12 additions & 23 deletions openatlas/api/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def __init__(
single: bool = False) -> None:
self.entities = entities if isinstance(entities, list) else [entities]
self.parser = Parser(parser)
self.pagination = None
self.pagination: dict[str, Any] = {}
self.single = single
self.entities_with_links: dict[int, dict[str, Any]] = {}
self.formated_entities = []
self.formated_entities: list[dict[str, Any]] = []

def get_links_for_entities(self) -> None:
if not self.entities:
return
for entity in self.entities:
self.entities_with_links[entity.id] = {
'entity': entity,
Expand Down Expand Up @@ -83,28 +85,19 @@ 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)]
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)

self.remove_duplicate_entities()
if self.parser.count == 'true':
return jsonify(len(self.entities))

self.sort_entities()
self.get_pagination()
self.reduce_entities_to_limit()
if self.entities:
self.get_links_for_entities()
self.get_links_for_entities()
if self.parser.export == 'csv':
return self.export_entities_csv()
if self.parser.export == 'csvNetwork':
return self.export_csv_for_network_analysis()
if self.entities:
self.get_entities_formatted()
self.get_entities_formatted()

if self.parser.format in app.config['RDF_FORMATS']: # pragma: no cover
return Response(
Expand Down Expand Up @@ -209,16 +202,12 @@ 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()
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))]
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:
if not self.entities:
return
entities = []
match self.parser.format:
case 'geojson':
Expand Down
6 changes: 0 additions & 6 deletions openatlas/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ 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,
Expand Down

0 comments on commit 3037380

Please sign in to comment.