Skip to content

Commit

Permalink
Ajout colonne clé d'unicité d'une vue - cf #129
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed May 11, 2023
1 parent 40efba3 commit b96b78b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
14 changes: 12 additions & 2 deletions backend/gn_module_export/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ def __init__(self, session, **kwargs):
label="Nom de l'export",
schema_name="Nom du schema PostgreSQL",
view_name="Nom de la vue SQL",
view_pk_column="Nom de colonne d'unicité de la vue",
desc="Description",
geometry_field="Nom de champ géométrique",
geometry_srid="SRID du champ géométrique",
)
# Description des colonnes
column_descriptions = dict(
label="Nom libre de l'export",
schema_name="Nom exact du schéma postgreSQL contenant la vue SQL.",
schema_name="Nom exact du schéma PostgreSQL contenant la vue SQL.",
view_name="Nom exact de la vue SQL permettant l'export de vos données.", # noqa E501
view_pk_column="Nom exact de la colonne d'unicité de la vue. Si non renseigné, la première colonne sera utilisée pour les exports.",
desc="Décrit la nature de l'export",
public="L'export est accessible à tous",
)
Expand All @@ -177,6 +179,7 @@ def __init__(self, session, **kwargs):
"label",
"schema_name",
"view_name",
"view_pk_column",
"desc",
"geometry_field",
"geometry_srid",
Expand All @@ -194,6 +197,7 @@ def validate_form(self, form):
schema_name = getattr(form, "schema_name", "")
geometry_field = getattr(form, "geometry_field", None)
geometry_srid = getattr(form, "geometry_srid", None)
view_pk_column = getattr(form, "view_pk_column", None)
if is_form_submitted() and view_name and schema_name:
try:
if geometry_field.data and geometry_srid.data is None:
Expand All @@ -208,7 +212,13 @@ def validate_form(self, form):
geometry_field=geometry_field.data,
filters=[],
)
query.return_query()
columns = query.view.tableDef.columns.keys()

# test if columns exists
test_columns = (geometry_field.data, view_pk_column.data)
for col in test_columns:
if col and col not in columns:
raise KeyError(f"Column {view_pk_column.data} doesn't exists")

except Exception as exp:
flash(exp, category="error")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Add primary key view column in t_exports
Revision ID: fdc2d823a8b9
Revises: c2d02e345a06
Create Date: 2023-05-11 17:33:00.460204
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "fdc2d823a8b9"
down_revision = "4cac712a2ce6"
branch_labels = None
depends_on = None


def upgrade():
op.add_column(
"t_exports",
sa.Column("view_pk_column", sa.Unicode, server_default=None),
schema="gn_exports",
)
op.execute(
"""
UPDATE gn_exports.t_exports SET view_pk_column='id_synthese'
WHERE label = 'Synthese SINP';
"""
)


def downgrade():
op.drop_column("t_exports", "view_pk_column", schema="gn_exports")
1 change: 1 addition & 0 deletions backend/gn_module_export/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Export(DB.Model):
label = DB.Column(DB.Text, nullable=False, unique=True, index=True)
schema_name = DB.Column(DB.Text, nullable=False)
view_name = DB.Column(DB.Text, nullable=False)
view_pk_column = DB.Column(DB.Text, nullable=False)
desc = DB.Column(DB.Text)
geometry_field = DB.Column(DB.Text)
geometry_srid = DB.Column(DB.Integer)
Expand Down

0 comments on commit b96b78b

Please sign in to comment.