Skip to content

Commit

Permalink
Manage models in geonature not gn_module_monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Dec 11, 2023
1 parent 4a3148e commit 816296f
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 19 deletions.
78 changes: 65 additions & 13 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_type_site = DB.Table(
"cor_type_site",
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_type_site, lazy="noload")


@serializable
class TBaseVisits(DB.Model):
Expand Down Expand Up @@ -102,10 +155,10 @@ 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],
)

dataset = relationship(
Expand All @@ -128,7 +181,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 +205,8 @@ 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],
)
12 changes: 6 additions & 6 deletions backend/geonature/core/gn_monitoring/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from geojson import FeatureCollection

from geonature.utils.env import DB
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 utils_flask_sqla.response import json_resp
from utils_flask_sqla_geo.generic import get_geojson_feature
Expand Down Expand Up @@ -81,16 +81,16 @@ def get_site_areas(id_site):
params = request.args

q = (
DB.session.query(corSiteArea, func.ST_Transform(LAreas.geom, 4326))
.join(LAreas, LAreas.id_area == corSiteArea.c.id_area)
.filter(corSiteArea.c.id_base_site == id_site)
DB.session.query(cor_site_area, func.ST_Transform(LAreas.geom, 4326))
.join(LAreas, LAreas.id_area == cor_site_area.c.id_area)
.filter(cor_site_area.c.id_base_site == id_site)
)

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

data = q.all()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""create_cor_module_type
Revision ID: a54bafb13ce8
Revises: ce54ba49ce5c
Create Date: 2022-12-06 16:18:24.512562
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "a54bafb13ce8"
down_revision = "ce54ba49ce5c"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
referent_schema = "gn_commons"


def upgrade():
op.create_table(
"cor_module_type",
sa.Column(
"id_type_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_type_site.id_nomenclature_type_site",
name="fk_cor_module_type_id_nomenclature",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column(
"id_module",
sa.Integer(),
sa.ForeignKey(
f"{referent_schema}.t_modules.id_module",
name="fk_cor_module_type_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.PrimaryKeyConstraint("id_type_site", "id_module", name="pk_cor_module_type"),
schema=monitorings_schema,
)

# Insertion des données a partir de cor_site_module
op.execute(
"""
INSERT INTO gn_monitoring.cor_module_type (id_module, id_type_site )
SELECT DISTINCT csm.id_module, cts.id_type_site
FROM gn_monitoring.cor_site_module AS csm
JOIN gn_monitoring.cor_type_site AS cts
ON Cts.id_base_site = csm.id_base_site ;
"""
)


def downgrade():
op.drop_table("cor_module_type", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""create_bib_type_site
Revision ID: b53bafb13ce8
Revises: 446e902a14e7
Create Date: 2022-12-06 16:18:24.512562
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "b53bafb13ce8"
down_revision = "446e902a14e7"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
nomenclature_schema = "ref_nomenclatures"


def upgrade():
op.create_table(
"bib_type_site",
sa.Column(
"id_nomenclature_type_site",
sa.Integer(),
sa.ForeignKey(
f"{nomenclature_schema}.t_nomenclatures.id_nomenclature",
name="fk_t_nomenclatures_id_nomenclature_type_site",
),
nullable=False,
unique=True,
),
sa.PrimaryKeyConstraint("id_nomenclature_type_site"),
sa.Column("config", sa.JSON(), nullable=True),
schema=monitorings_schema,
)

# FIXME: if sqlalchemy >= 1.4.32, it should work with postgresql_not_valid=True: cleaner
# op.create_check_constraint(
# "ck_bib_type_site_id_nomenclature_type_site",
# "bib_type_site",
# f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature_type_site,'TYPE_SITE')",
# schema=monitorings_schema,
# postgresql_not_valid=True
# )
statement = sa.text(
f"""
ALTER TABLE {monitorings_schema}.bib_type_site
ADD
CONSTRAINT ck_bib_type_site_id_nomenclature_type_site CHECK (
{nomenclature_schema}.check_nomenclature_type_by_mnemonique(
id_nomenclature_type_site, 'TYPE_SITE' :: character varying
)
) NOT VALID
"""
)
op.execute(statement)
op.create_table_comment(
"bib_type_site",
"Table de définition des champs associés aux types de sites",
schema=monitorings_schema,
)

# Récupération de la liste des types de site avec ceux déja présents dans la table t_base_site
op.execute(
"""
INSERT INTO gn_monitoring.bib_type_site AS bts (id_nomenclature_type_site)
SELECT DISTINCT id_nomenclature_type_site
FROM gn_monitoring.t_base_sites AS tbs ;
"""
)


def downgrade():
op.drop_table("bib_type_site", schema=monitorings_schema)
Loading

0 comments on commit 816296f

Please sign in to comment.