diff --git a/backend/gn_module_export/blueprint.py b/backend/gn_module_export/blueprint.py index e14430d1..8197cc95 100644 --- a/backend/gn_module_export/blueprint.py +++ b/backend/gn_module_export/blueprint.py @@ -160,6 +160,7 @@ 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", @@ -167,8 +168,9 @@ def __init__(self, session, **kwargs): # 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", ) @@ -177,6 +179,7 @@ def __init__(self, session, **kwargs): "label", "schema_name", "view_name", + "view_pk_column", "desc", "geometry_field", "geometry_srid", @@ -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: @@ -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") diff --git a/backend/gn_module_export/migrations/fdc2d823a8b9_add_primary_key_view_column_in_t_exports.py b/backend/gn_module_export/migrations/fdc2d823a8b9_add_primary_key_view_column_in_t_exports.py new file mode 100644 index 00000000..799306da --- /dev/null +++ b/backend/gn_module_export/migrations/fdc2d823a8b9_add_primary_key_view_column_in_t_exports.py @@ -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") diff --git a/backend/gn_module_export/models.py b/backend/gn_module_export/models.py index b9842576..8fc149c5 100644 --- a/backend/gn_module_export/models.py +++ b/backend/gn_module_export/models.py @@ -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)