Skip to content

Commit

Permalink
Merge branch 'develop' into feat-import/support-constant
Browse files Browse the repository at this point in the history
  • Loading branch information
20cents authored Dec 3, 2024
2 parents 79472e6 + 42032c9 commit 925249c
Show file tree
Hide file tree
Showing 72 changed files with 1,746 additions and 635 deletions.
2 changes: 1 addition & 1 deletion backend/dependencies/UsersHub
98 changes: 83 additions & 15 deletions backend/geonature/core/gn_monitoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from geoalchemy2 import Geometry
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.sql import select, func


Expand All @@ -16,12 +16,13 @@
from utils_flask_sqla.serializers import serializable
from utils_flask_sqla_geo.serializers import geoserializable

from pypnnomenclature.models import TNomenclatures
from geonature.core.gn_commons.models import TModules
from geonature.core.gn_meta.models import TDatasets
from geonature.utils.env import DB


corVisitObserver = DB.Table(
cor_visit_observer = DB.Table(
"cor_visit_observer",
DB.Column(
"id_base_visit",
Expand All @@ -39,7 +40,7 @@
)


corSiteModule = DB.Table(
cor_site_module = DB.Table(
"cor_site_module",
DB.Column(
"id_base_site",
Expand All @@ -56,7 +57,7 @@
schema="gn_monitoring",
)

corSiteArea = DB.Table(
cor_site_area = DB.Table(
"cor_site_area",
DB.Column(
"id_base_site",
Expand All @@ -68,6 +69,58 @@
schema="gn_monitoring",
)

cor_module_type = DB.Table(
"cor_module_type",
DB.Column(
"id_module",
DB.Integer,
DB.ForeignKey("gn_commons.t_modules.id_module"),
primary_key=True,
),
DB.Column(
"id_type_site",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature_type_site"),
primary_key=True,
),
schema="gn_monitoring",
)

cor_site_type = DB.Table(
"cor_site_type",
DB.Column(
"id_base_site",
DB.Integer,
DB.ForeignKey("gn_monitoring.t_base_sites.id_base_site"),
primary_key=True,
),
DB.Column(
"id_type_site",
DB.Integer,
DB.ForeignKey("gn_monitoring.bib_type_site.id_nomenclature_type_site"),
primary_key=True,
),
schema="gn_monitoring",
)


@serializable
class BibTypeSite(DB.Model):
__tablename__ = "bib_type_site"
__table_args__ = {"schema": "gn_monitoring"}

id_nomenclature_type_site = DB.Column(
DB.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"),
nullable=False,
primary_key=True,
)
config = DB.Column(JSONB)
nomenclature = DB.relationship(
TNomenclatures, uselist=False, backref=DB.backref("bib_type_site", uselist=False)
)

sites = DB.relationship("TBaseSites", secondary=cor_site_type, lazy="noload")


@serializable
class TBaseVisits(DB.Model):
Expand All @@ -84,7 +137,7 @@ class TBaseVisits(DB.Model):
# Pour le moment non défini comme une clé étrangère
# pour les questions de perfs
# a voir en fonction des usage
id_module = DB.Column(DB.Integer)
id_module = DB.Column(DB.Integer, ForeignKey("gn_commons.t_modules.id_module"))

visit_date_min = DB.Column(DB.DateTime)
visit_date_max = DB.Column(DB.DateTime)
Expand All @@ -102,15 +155,16 @@ class TBaseVisits(DB.Model):

observers = DB.relationship(
User,
secondary=corVisitObserver,
primaryjoin=(corVisitObserver.c.id_base_visit == id_base_visit),
secondaryjoin=(corVisitObserver.c.id_role == User.id_role),
foreign_keys=[corVisitObserver.c.id_base_visit, corVisitObserver.c.id_role],
secondary=cor_visit_observer,
primaryjoin=(cor_visit_observer.c.id_base_visit == id_base_visit),
secondaryjoin=(cor_visit_observer.c.id_role == User.id_role),
foreign_keys=[cor_visit_observer.c.id_base_visit, cor_visit_observer.c.id_role],
)

observers_txt = DB.Column(DB.Unicode)

dataset = relationship(
TDatasets,
lazy="joined",
primaryjoin=(TDatasets.id_dataset == id_dataset),
foreign_keys=[id_dataset],
)
Expand All @@ -128,7 +182,6 @@ class TBaseSites(DB.Model):
id_base_site = DB.Column(DB.Integer, primary_key=True)
id_inventor = DB.Column(DB.Integer, ForeignKey("utilisateurs.t_roles.id_role"))
id_digitiser = DB.Column(DB.Integer, ForeignKey("utilisateurs.t_roles.id_role"))
id_nomenclature_type_site = DB.Column(DB.Integer)
base_site_name = DB.Column(DB.Unicode)
base_site_description = DB.Column(DB.Unicode)
base_site_code = DB.Column(DB.Unicode)
Expand All @@ -153,8 +206,23 @@ class TBaseSites(DB.Model):
"TModules",
lazy="select",
enable_typechecks=False,
secondary=corSiteModule,
primaryjoin=(corSiteModule.c.id_base_site == id_base_site),
secondaryjoin=(corSiteModule.c.id_module == TModules.id_module),
foreign_keys=[corSiteModule.c.id_base_site, corSiteModule.c.id_module],
secondary=cor_site_module,
primaryjoin=(cor_site_module.c.id_base_site == id_base_site),
secondaryjoin=(cor_site_module.c.id_module == TModules.id_module),
foreign_keys=[cor_site_module.c.id_base_site, cor_site_module.c.id_module],
)


@serializable
class TObservations(DB.Model):
__tablename__ = "t_observations"
__table_args__ = {"schema": "gn_monitoring"}
id_observation = DB.Column(DB.Integer, primary_key=True, nullable=False, unique=True)
id_base_visit = DB.Column(DB.ForeignKey("gn_monitoring.t_base_visits.id_base_visit"))
id_digitiser = DB.Column(DB.Integer, DB.ForeignKey("utilisateurs.t_roles.id_role"))
digitiser = DB.relationship(
User, primaryjoin=(User.id_role == id_digitiser), foreign_keys=[id_digitiser]
)
cd_nom = DB.Column(DB.Integer)
comments = DB.Column(DB.String)
uuid_observation = DB.Column(UUID(as_uuid=True), default=select(func.uuid_generate_v4()))
13 changes: 6 additions & 7 deletions backend/geonature/core/gn_monitoring/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Blueprint, request
from geojson import FeatureCollection
from geonature.core.gn_monitoring.models import TBaseSites, corSiteArea, corSiteModule
from geonature.core.gn_monitoring.models import TBaseSites, cor_site_area, cor_site_module
from geonature.utils.env import DB
from ref_geo.models import LAreas
from sqlalchemy import select
Expand Down Expand Up @@ -79,17 +79,16 @@ def get_site_areas(id_site):
params = request.args

query = (
# [email protected]_4326
select(corSiteArea, func.ST_Transform(LAreas.geom, 4326))
.join(LAreas, LAreas.id_area == corSiteArea.c.id_area)
.where(corSiteArea.c.id_base_site == id_site)
select(cor_site_area, func.ST_Transform(LAreas.geom, 4326))
.join(LAreas, LAreas.id_area == cor_site_area.c.id_area)
.where(cor_site_area.c.id_base_site == id_site)
)

if "id_area_type" in params:
query = query.where(LAreas.id_type == params["id_area_type"])
if "id_module" in params:
query = query.join(corSiteModule, corSiteModule.c.id_base_site == id_site).where(
corSiteModule.c.id_module == params["id_module"]
query = query.join(cor_site_module, cor_site_module.c.id_base_site == id_site).where(
cor_site_module.c.id_module == params["id_module"]
)

data = DB.session.scalars(query).all()
Expand Down
8 changes: 4 additions & 4 deletions backend/geonature/core/gn_synthese/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,10 @@ def general_stats(permissions):
return data


@routes.route("/taxon_stats/<int:cd_ref>", methods=["GET"])
@routes.route("/taxon_stats/<int:cd_nom>", methods=["GET"])
@permissions.check_cruved_scope("R", get_scope=True, module_code="SYNTHESE")
@json_resp
def taxon_stats(scope, cd_ref):
def taxon_stats(scope, cd_nom):
"""Return stats for a specific taxon"""

area_type = request.args.get("area_type")
Expand All @@ -986,7 +986,7 @@ def taxon_stats(scope, cd_ref):
.where(BibAreasTypes.type_code == area_type)
.alias("areas")
)

cd_ref = db.session.scalar(select(Taxref.cd_ref).where(Taxref.cd_nom == cd_nom))
taxref_cd_nom_list = db.session.scalars(select(Taxref.cd_nom).where(Taxref.cd_ref == cd_ref))

# Main query to fetch stats
Expand Down Expand Up @@ -1021,7 +1021,7 @@ def taxon_stats(scope, cd_ref):
synthese_stats = result.fetchone()

data = {
"cd_ref": cd_ref,
"cd_ref": cd_nom,
"observation_count": synthese_stats["observation_count"],
"observer_count": synthese_stats["observer_count"],
"area_count": synthese_stats["area_count"],
Expand Down
21 changes: 13 additions & 8 deletions backend/geonature/core/imports/checks/sql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
__all__ = ["init_rows_validity", "check_orphan_rows"]


def init_rows_validity(imprt: TImports):
def init_rows_validity(imprt: TImports, dataset_name_field: str = "id_dataset"):
"""
Validity columns are three-states:
- None: the row does not contains data for the given entity
Expand Down Expand Up @@ -48,16 +48,21 @@ def init_rows_validity(imprt: TImports):
.where(BibFields.name_field.in_(selected_fields_names))
.where(BibFields.entities.any(EntityField.entity == entity))
.where(~BibFields.entities.any(EntityField.entity != entity))
.where(BibFields.name_field != dataset_name_field)
.all()
)
db.session.execute(
sa.update(transient_table)
.where(transient_table.c.id_import == imprt.id_import)
.where(
sa.or_(*[transient_table.c[field.source_column].isnot(None) for field in fields])

if fields:
db.session.execute(
sa.update(transient_table)
.where(transient_table.c.id_import == imprt.id_import)
.where(
sa.or_(
*[transient_table.c[field.source_column].isnot(None) for field in fields]
)
)
.values({entity.validity_column: True})
)
.values({entity.validity_column: True})
)


def check_orphan_rows(imprt: TImports):
Expand Down
Loading

0 comments on commit 925249c

Please sign in to comment.