From 9cad741ab2d829f4b737dedb3dd5a732c5f047ae Mon Sep 17 00:00:00 2001 From: Pierre-Narcisi Date: Wed, 8 Jan 2025 15:19:44 +0100 Subject: [PATCH] feat(ca) add dataset detail to acquisiton framework --- .../geonature/core/gn_meta/models/datasets.py | 21 +++++++++++++++++++ backend/geonature/core/gn_meta/routes.py | 16 -------------- backend/geonature/core/gn_meta/schemas.py | 2 ++ .../acquisition_framework_template_pdf.html | 8 +++---- .../metadataModule/af/af-card.component.html | 6 ++++++ 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/backend/geonature/core/gn_meta/models/datasets.py b/backend/geonature/core/gn_meta/models/datasets.py index eacb7785e8..ea199f4204 100644 --- a/backend/geonature/core/gn_meta/models/datasets.py +++ b/backend/geonature/core/gn_meta/models/datasets.py @@ -139,6 +139,27 @@ def user_actors(self): def organism_actors(self): return [actor.organism for actor in self.cor_dataset_actor if actor.organism is not None] + @hybrid_property + def obs_count(self): + from geonature.core.gn_synthese.models import Synthese + + return db.session.scalar( + select(func.count(Synthese.id_synthese)) + .select_from(Synthese) + .where(Synthese.id_dataset == self.id_dataset) + ) + + @hybrid_property + def hab_count(self): + from gn_module_occhab.models import OccurenceHabitat, Station + + return db.session.scalar( + select(func.count(OccurenceHabitat.id_habitat)) + .select_from(OccurenceHabitat) + .where(Station.id_station == OccurenceHabitat.id_station) + .where(Station.id_dataset == self.id_dataset) + ) + def is_deletable(self): return not DB.session.execute(self.synthese_records.exists().select()).scalar() diff --git a/backend/geonature/core/gn_meta/routes.py b/backend/geonature/core/gn_meta/routes.py index 20f6c584cd..ec6052f509 100644 --- a/backend/geonature/core/gn_meta/routes.py +++ b/backend/geonature/core/gn_meta/routes.py @@ -618,22 +618,6 @@ def get_export_pdf_acquisition_frameworks(id_acquisition_framework): dataset_ids = [d.id_dataset for d in af.datasets] nb_data = len(dataset_ids) - # Add count of Synthese observations for each dataset - for dataset in acquisition_framework["datasets"]: - dataset_obs_count_query = ( - select(func.count(Synthese.id_synthese)) - .select_from(Synthese) - .where(Synthese.id_dataset == dataset["id_dataset"]) - ) - dataset_hab_count_query = ( - select(func.count(OccurenceHabitat.id_habitat)) - .select_from(OccurenceHabitat) - .where(Station.id_station == OccurenceHabitat.id_station) - .where(Station.id_dataset == dataset["id_dataset"]) - ) - dataset["count_synthese_observations"] = DB.session.scalar(dataset_obs_count_query) - dataset["count_habitats"] = DB.session.scalar(dataset_hab_count_query) - query = ( select(func.count(Synthese.cd_nom)) .select_from(Synthese) diff --git a/backend/geonature/core/gn_meta/schemas.py b/backend/geonature/core/gn_meta/schemas.py index d4e04bf44f..b34f67aba3 100644 --- a/backend/geonature/core/gn_meta/schemas.py +++ b/backend/geonature/core/gn_meta/schemas.py @@ -67,6 +67,8 @@ class Meta: cor_territories = MA.Nested(NomenclatureSchema, many=True, unknown=EXCLUDE) acquisition_framework = MA.Nested("AcquisitionFrameworkSchema", dump_only=True) sources = MA.Nested(SourceSchema, many=True, dump_only=True) + obs_count = fields.Int(dump_only=True) + hab_count = fields.Int(dump_only=True) @post_dump(pass_many=False, pass_original=True) def module_input(self, item, original, many, **kwargs): diff --git a/backend/geonature/templates/acquisition_framework_template_pdf.html b/backend/geonature/templates/acquisition_framework_template_pdf.html index cb27cf863f..2bf12cee04 100644 --- a/backend/geonature/templates/acquisition_framework_template_pdf.html +++ b/backend/geonature/templates/acquisition_framework_template_pdf.html @@ -299,12 +299,12 @@ {{ dataset['dataset_name'] }} {% endif %}
- {% if dataset['count_synthese_observations']: %} - Nombre d'observations dans la Synthèse : {{ dataset['count_synthese_observations'] }} + {% if dataset['obs_count'] > 0: %} + Nombre d'observations dans la Synthèse : {{ dataset['obs_count'] }} {% endif %}
- {% if dataset['count_habitats']: %} - Nombre d'habitats dans occhab : {{ dataset['count_habitats'] }} + {% if dataset['hab_count'] > 0: %} + Nombre d'habitats dans occhab : {{ dataset['hab_count'] }} {% endif %}

diff --git a/frontend/src/app/metadataModule/af/af-card.component.html b/frontend/src/app/metadataModule/af/af-card.component.html index 8b6aff2f33..1a812625f2 100644 --- a/frontend/src/app/metadataModule/af/af-card.component.html +++ b/frontend/src/app/metadataModule/af/af-card.component.html @@ -421,6 +421,12 @@
Jeux de données associés
{{ dataset.unique_dataset_id }}
+
+ Nombre d'observations dans la Synthèse : {{ dataset['obs_count'] }} +
+
+ Nombre d'habitats dans occhab : {{ dataset['hab_count'] }} +