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 %}