Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUIVI-EOLIEN] Séparation des groupes de sites et sites (db + api), style page d'accueil #204

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 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
668f14f
Fix/review checkpoint1 (#37)
mvergez Jan 18, 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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dans mon souvenir on avait pas dit qu'on évitait les delete cascades?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voilà ce qu'on a décidé :

  • Si supprime t_modules => supprime cor_module_type
  • Si supprime bib_type_site => supprime cor_module_type
  • Si supprime t_nomenclature associé à bib_type_site => ERREUR
  • Si supprime t_nomenclature associé à cor_type_site => ERREUR

Donc CASCADE seulement sur cor_module_type sur id_type_site et id_module

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalement, après vérification, tout était déjà bon. J'attends ton retour

),
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si j'ai bien compris id_module de t_site_compléments sera peuplé avec la valeur 'MONITORINGS' ce qui n'est pas un vrai retour en arrière

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui ce n'est pas idéal mais je ne sais pas si c'est possible de faire le downgrade à l'identique ici car on désolidarise les sites des modules

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,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,71 @@
"""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"

TYPE_SITE = "TYPE_SITE"


def upgrade():
op.create_table(
"bib_type_site",
sa.Column(
"id_nomenclature",
mvergez marked this conversation as resolved.
Show resolved Hide resolved
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,
)

statement = sa.text(
f"""
CREATE OR REPLACE FUNCTION {monitorings_schema}.ck_bib_type_site_id_nomenclature()
amandine-sahl marked this conversation as resolved.
Show resolved Hide resolved
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
perform {nomenclature_schema}.check_nomenclature_type_by_mnemonique(NEW.id_nomenclature, :mnemonique );
RETURN NEW;
END;
$function$
;
DROP TRIGGER IF EXISTS ck_bib_type_site_id_nomenclature on gn_monitoring.bib_type_site;
CREATE TRIGGER ck_bib_type_site_id_nomenclature BEFORE
INSERT
OR
UPDATE ON {monitorings_schema}.bib_type_site FOR EACH ROW EXECUTE PROCEDURE {monitorings_schema}.ck_bib_type_site_id_nomenclature();
"""
).bindparams(mnemonique=TYPE_SITE)
op.execute(statement)


def downgrade():

op.drop_table("bib_type_site", schema=monitorings_schema)
statement = sa.text(
f"""
DROP FUNCTION IF EXISTS {monitorings_schema}.ck_bib_type_site_id_nomenclature;
"""
)
op.execute(statement)
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