Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6d55b19
Merged feat/package-module-alembic
Dec 6, 2022
466a6e9
Merged feat/bib-categorie-site
Dec 9, 2022
a625733
feat: [6.2] Page d'accueil module monitoring layout et config title e…
andriacap Dec 12, 2022
697823d
Feat/monitoring sites (#16)
mvergez Dec 19, 2022
e717216
Feat/site type categories and module categorie (#18)
mvergez Dec 22, 2022
8b09df2
Feat/edit categories module (#19)
mvergez Dec 22, 2022
1d58043
test: refactor fixtures to load them automatically (#20)
mvergez Dec 23, 2022
4b0139a
test: move test_route in parent dir (#17)
mvergez Dec 23, 2022
aa379ee
Feat/create marshmallow schemas and remove id_module (#21)
mvergez Jan 4, 2023
6a135d4
refactor(api): remove id_type in admin (#22)
mvergez Jan 4, 2023
ecc02a4
style(config): rename attribut label of categories (#23)
mvergez Jan 4, 2023
15edb4b
Fix/paginate utils (#24)
mvergez Jan 4, 2023
a38eda5
Feat/improve filter (#25)
mvergez Jan 4, 2023
83aa91a
fix(config): changed categories into items (#29)
mvergez Jan 5, 2023
dbcf9a0
fix: rebase failure: missing geonature
Jan 10, 2023
a122e9b
Fix/db migrations (#31)
mvergez Jan 13, 2023
b0d968e
perf(api): improved loading of modules (#30)
mvergez Jan 13, 2023
e549193
Fix/pagination (#28)
mvergez Jan 13, 2023
ec1bf2a
style(api): restore data_utils spaces (#33)
mvergez Jan 13, 2023
a4c7f29
Fix/db migrations checkconstrainton bib_type_site.id_nomenclature (#34)
andriacap Jan 16, 2023
2834de1
fix(api): invert filter condition with Unicode (#35)
mvergez Jan 17, 2023
0e15017
fix(db): add NOT VALID in constraint for bib_type_site (#36)
mvergez Jan 17, 2023
20994e3
feat(db): migration to add geom col to sites_group
Jan 3, 2023
16b9f5d
feat(api): add geom column to model
Jan 3, 2023
fafde2d
fix(db): changed down_revision to be the last one
Jan 17, 2023
85e54b6
feat(api): updated model and schema
Jan 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/gn_module_monitoring/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"""

from flask import Blueprint, current_app
from geonature.core.admin.admin import admin as flask_admin
from geonature.utils.env import DB

from gn_module_monitoring.monitoring.admin import BibTypeSiteView
from .command.cmd import commands

blueprint = Blueprint("monitorings", __name__)
Expand All @@ -12,3 +16,5 @@
blueprint.cli.short_help = "Commandes pour l" "administration du module MONITORINGS"
for cmd in commands:
blueprint.cli.add_command(cmd)

flask_admin.add_view(BibTypeSiteView(DB.session, name="Types de site", category="Monitorings"))
7 changes: 5 additions & 2 deletions backend/gn_module_monitoring/conf_schema_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
Fichier à ne pas modifier. Paramètres surcouchables dans config/config_gn_module.tml
"""

from marshmallow import Schema, fields, validates_schema, ValidationError
from marshmallow import Schema, fields


class GnModuleSchemaConf(Schema):
pass
DESCRIPTION_MODULE = fields.String(missing="Vous trouverez ici la liste des modules")
TITLE_MODULE = fields.String(missing="Module de suivi")


# AREA_TYPE = fields.List(fields.String(), missing=["COM", "M1", "M5", "M10"])
# BORNE_OBS = fields.List(fields.Integer(), missing=[1, 20, 40, 60, 80, 100, 120])
# BORNE_TAXON = fields.List(fields.Integer(), missing=[1, 5, 10, 15])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""remove_id_module_from_sites_complements

Revision ID: 6673266fb79c
Revises: a54bafb13ce8
Create Date: 2022-12-13 16:00:00.512562

"""
import sqlalchemy as sa
from alembic import op

from gn_module_monitoring import MODULE_CODE

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

monitorings_schema = "gn_monitoring"


def upgrade():
op.drop_column("t_site_complements", "id_module", schema=monitorings_schema)


def downgrade():
op.add_column(
"t_site_complements",
sa.Column(
"id_module",
sa.Integer(),
sa.ForeignKey(
f"gn_commons.t_modules.id_module",
name="fk_t_site_complements_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=True,
),
schema=monitorings_schema,
)
# Cannot use orm here because need the model to be "downgraded" as well
# Need to set nullable True above for existing rows
# FIXME: find a better way because need to assign a module...
statement = sa.text(
f"""
update {monitorings_schema}.t_site_complements
set id_module = (select id_module
from gn_commons.t_modules tm
where module_code = :module_code);
"""
).bindparams(module_code=MODULE_CODE)
op.execute(statement)
op.alter_column("t_site_complements", "id_module", nullable=False, schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""add_geom_column_sites_groups

Revision ID: 7419574e4571
Revises:
Create Date: 2023-01-03 16:18:24.512562

"""
from alembic import op
import geoalchemy2
from geonature.core.gn_monitoring.models import TBaseSites
from gn_module_monitoring.monitoring.models import TMonitoringSites, TMonitoringSitesGroups
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "7419574e4571"
down_revision = "f24adb481f54"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"


def upgrade():
op.add_column(
"t_sites_groups",
sa.Column(
"geom",
geoalchemy2.types.Geometry(
srid=4326,
name="geometry",
),
nullable=True,
),
schema=monitorings_schema,
)
statement = sa.text(
"""
WITH t AS
(SELECT ST_ConvexHull(ST_COLLECT(tbs.geom)) AS geom,
tsg.id_sites_group AS id_sites_group
FROM gn_monitoring.t_sites_groups tsg
JOIN gn_monitoring.t_site_complements tsc ON tsc.id_sites_group = tsg.id_sites_group
JOIN gn_monitoring.t_base_sites tbs ON tbs.id_base_site = tsc.id_base_site
GROUP BY tsg.id_sites_group)
UPDATE gn_monitoring.t_sites_groups
SET geom = t.geom
FROM t
WHERE gn_monitoring.t_sites_groups.id_sites_group = t.id_sites_group;
"""
)
op.execute(statement)


def downgrade():
op.drop_column("t_sites_groups", "geom", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""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",
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,
)


def downgrade():
op.drop_table("cor_module_type", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""create_bib_type_site

Revision ID: b53bafb13ce8
Revises: e78003460441
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 = "e78003460441"
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",
sa.Integer(),
sa.ForeignKey(
f"{nomenclature_schema}.t_nomenclatures.id_nomenclature",
name="fk_t_nomenclatures_id_nomenclature",
),
nullable=False,
unique=True,
),
sa.PrimaryKeyConstraint("id_nomenclature"),
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",
# "bib_type_site",
# f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature,'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 CHECK (
{nomenclature_schema}.check_nomenclature_type_by_mnemonique(
id_nomenclature, 'TYPE_SITE' :: character varying
)
) NOT VALID
"""
)
op.execute(statement)


def downgrade():
op.drop_table("bib_type_site", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""create_cor_type_site

Revision ID: ce54ba49ce5c
Revises: b53bafb13ce8
Create Date: 2022-12-06 16:18:24.512562

"""
from alembic import op
import sqlalchemy as sa

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

monitorings_schema = "gn_monitoring"


def upgrade():
op.create_table(
"cor_type_site",
sa.Column(
"id_type_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_type_site.id_nomenclature",
name="fk_cor_type_site_id_nomenclature",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column(
"id_base_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.t_base_sites.id_base_site",
name="fk_cor_type_site_id_base_site",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.PrimaryKeyConstraint("id_type_site", "id_base_site", name="pk_cor_type_site"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_type_site", schema=monitorings_schema)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""remove_id_module_from_sites_groups

Revision ID: f24adb481f54
Revises: 6673266fb79c
Create Date: 2022-12-13 16:00:00.512562

"""
import sqlalchemy as sa
from alembic import op

from gn_module_monitoring import MODULE_CODE

# revision identifiers, used by Alembic.
revision = "f24adb481f54"
down_revision = "6673266fb79c"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"


def upgrade():
op.drop_column("t_sites_groups", "id_module", schema=monitorings_schema)


def downgrade():
op.add_column(
"t_sites_groups",
sa.Column(
"id_module",
sa.Integer(),
sa.ForeignKey(
f"gn_commons.t_modules.id_module",
name="fk_t_sites_groups_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=True,
),
schema=monitorings_schema,
)
# Cannot use orm here because need the model to be "downgraded" as well
# Need to set nullable True above for existing rows
# FIXME: find a better way because need to assign a module...
statement = sa.text(
f"""
update {monitorings_schema}.t_sites_groups
set id_module = (select id_module
from gn_commons.t_modules tm
where module_code = :module_code);
"""
).bindparams(module_code=MODULE_CODE)
op.execute(statement)
op.alter_column("t_sites_groups", "id_module", nullable=False, schema=monitorings_schema)
8 changes: 7 additions & 1 deletion backend/gn_module_monitoring/modules/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
get_modules
"""

from sqlalchemy.orm import Load
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

from geonature.utils.env import DB
Expand Down Expand Up @@ -87,7 +88,12 @@ def get_modules():

try:
res = (
DB.session.query(TMonitoringModules).order_by(TMonitoringModules.module_label)
DB.session.query(TMonitoringModules)
.options(
# Raise load not to load any relationship
Load(TMonitoringModules).raiseload("*")
)
.order_by(TMonitoringModules.module_label)
.all()
)

Expand Down
Loading