diff --git a/openatlas/__init__.py b/openatlas/__init__.py index 80a995c1e..060ade179 100644 --- a/openatlas/__init__.py +++ b/openatlas/__init__.py @@ -60,7 +60,13 @@ def before_request() -> None: g.cidoc_classes = CidocClass.get_all(session['language']) g.properties = CidocProperty.get_all(session['language']) g.classes = OpenatlasClass.get_all() - g.types = Type.get_all() + with_count = False + if request.path.startswith('/type') or \ + request.path.startswith('/api/type_tree/') or ( + request.path.startswith('/entity/') and + request.path.split('/entity/')[1].isdigit()): + with_count = True + g.types = Type.get_all(with_count) g.reference_systems = ReferenceSystem.get_all() g.view_class_mapping = view_class_mapping g.class_view_mapping = OpenatlasClass.get_class_view_mapping() diff --git a/openatlas/api/endpoints/type.py b/openatlas/api/endpoints/type.py index 7a0260a2f..68e222263 100644 --- a/openatlas/api/endpoints/type.py +++ b/openatlas/api/endpoints/type.py @@ -89,8 +89,9 @@ def get() -> Union[tuple[Resource, int], Response]: @staticmethod def get_type_tree() -> dict[int, Any]: - return {id_: GetTypeTree.serialize_to_json(type_) - for id_, type_ in Type.get_all().items()} + return { + id_: GetTypeTree.serialize_to_json(type_) for id_, type_ + in g.types.items()} @staticmethod def serialize_to_json(type_: Type) -> dict[str, Any]: diff --git a/openatlas/database/type.py b/openatlas/database/type.py index 8f3a1c142..f32e2a5eb 100644 --- a/openatlas/database/type.py +++ b/openatlas/database/type.py @@ -6,9 +6,8 @@ class Type: @staticmethod - def get_types() -> list[dict[str, Any]]: - g.cursor.execute( - """ + def get_types(with_count: bool) -> list[dict[str, Any]]: + sql = f""" SELECT e.id, e.name, @@ -18,8 +17,9 @@ def get_types() -> list[dict[str, Any]]: e.created, e.modified, es.id AS super_id, - COUNT(l2.id) AS count, - COUNT(l3.id) AS count_property, + {'COUNT(l2.id)' if with_count else '0'} AS count, + {'COUNT(l3.id)' if with_count else '0'} AS count_property, + COALESCE(to_char(e.begin_from, 'yyyy-mm-dd hh24:mi:ss BC'), '') AS begin_from, COALESCE(to_char(e.begin_to, 'yyyy-mm-dd hh24:mi:ss BC'), '') @@ -36,17 +36,21 @@ def get_types() -> list[dict[str, Any]]: LEFT JOIN model.link l ON e.id = l.domain_id AND l.property_code IN ('P127', 'P89') LEFT JOIN model.entity es ON l.range_id = es.id - - -- Get count - LEFT JOIN model.link l2 ON e.id = l2.range_id - AND l2.property_code IN ('P2', 'P89') - LEFT JOIN model.link l3 ON e.id = l3.type_id - + """ + if with_count: + sql += """ + -- Get count + LEFT JOIN model.link l2 ON e.id = l2.range_id + AND l2.property_code IN ('P2', 'P89') + LEFT JOIN model.link l3 ON e.id = l3.type_id + """ + sql += """ WHERE e.openatlas_class_name IN ('administrative_unit', 'type', 'type_tools') GROUP BY e.id, es.id ORDER BY e.name; - """) + """ + g.cursor.execute(sql) return [dict(row) for row in g.cursor.fetchall()] @staticmethod diff --git a/openatlas/models/type.py b/openatlas/models/type.py index c8cb024f7..c3ae58777 100644 --- a/openatlas/models/type.py +++ b/openatlas/models/type.py @@ -117,9 +117,9 @@ def move_entities(self, new_type_id: int, checkbox_values: str) -> None: Db.remove_entity_type(self.id, delete_ids) @staticmethod - def get_all() -> dict[int, Type]: + def get_all(with_count: bool) -> dict[int, Type]: types = {} - for row in Db.get_types(): + for row in Db.get_types(with_count): type_ = Type(row) types[type_.id] = type_ type_.count = row['count'] or row['count_property'] diff --git a/tests/test_api.py b/tests/test_api.py index ed2e473b6..cebb0cc55 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -408,22 +408,20 @@ def test_api(self) -> None: # ---Type Endpoints--- for rv in [ self.app.get(url_for('api_03.type_overview')), - self.app.get( - url_for('api_03.type_overview', download=True))]: + self.app.get(url_for('api_03.type_overview', download=True))]: rv = rv.get_json()['place'][0]['children'][0] assert bool(rv['label'] == 'Austria') for rv in [ self.app.get(url_for('api_03.type_by_view_class')), self.app.get( - url_for('api_03.type_by_view_class', download=True))]: + url_for('api_03.type_by_view_class', download=True))]: rv = rv.get_json()['place'][2]['children'][0] assert bool(rv['label'] == 'Boundary Mark') for rv in [ self.app.get(url_for('api_03.type_tree')), - self.app.get( - url_for('api_03.type_tree', download=True))]: + self.app.get(url_for('api_03.type_tree', download=True))]: assert bool(rv.get_json()['typeTree']) rv = self.app.get(url_for('api_03.type_tree', count=True)) assert rv.get_json() > 0