From 6c76bb806191357a4603c5e2c5523ea8d3b61b8f Mon Sep 17 00:00:00 2001 From: Etienne Delclaux Date: Thu, 15 Feb 2024 18:16:50 +0100 Subject: [PATCH] feat(import): add coresponding entity to import errors --- .../core/imports/checks/dataframe/utils.py | 1 + .../core/imports/checks/sql/utils.py | 2 + backend/geonature/core/imports/models.py | 7 ++- .../geonature/core/imports/routes/imports.py | 4 +- .../templates/import_template_pdf.html | 2 + ...90691737d_add_id_entity_in_users_errors.py | 53 +++++++++++++++++++ .../import_errors.component.html | 3 +- .../import_report.component.html | 8 +++ 8 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py diff --git a/backend/geonature/core/imports/checks/dataframe/utils.py b/backend/geonature/core/imports/checks/dataframe/utils.py index 176b475224..fe74977358 100644 --- a/backend/geonature/core/imports/checks/dataframe/utils.py +++ b/backend/geonature/core/imports/checks/dataframe/utils.py @@ -62,6 +62,7 @@ def report_error(imprt, entity, df, error): { "id_import": imprt.id_import, "id_error": error_type.pk, + "id_entity": entity.id_entity, "column_error": column, "id_rows": ordered_invalid_rows, "comment": error.get("comment"), diff --git a/backend/geonature/core/imports/checks/sql/utils.py b/backend/geonature/core/imports/checks/sql/utils.py index d0b1991c67..bf2d908e72 100644 --- a/backend/geonature/core/imports/checks/sql/utils.py +++ b/backend/geonature/core/imports/checks/sql/utils.py @@ -72,6 +72,7 @@ def report_erroneous_rows(imprt, entity, error_type, error_column, whereclause): error = select( literal(imprt.id_import).label("id_import"), literal(error_type.pk).label("id_type"), + literal(entity.id_entity).label("id_entity"), array_agg( aggregate_order_by(cte.c.line_no, cte.c.line_no), ).label("rows"), @@ -81,6 +82,7 @@ def report_erroneous_rows(imprt, entity, error_type, error_column, whereclause): names=[ ImportUserError.id_import, ImportUserError.id_type, + ImportUserError.id_entity, ImportUserError.rows, ImportUserError.column, ], diff --git a/backend/geonature/core/imports/models.py b/backend/geonature/core/imports/models.py index 11518d3d77..ac78a7f3f9 100644 --- a/backend/geonature/core/imports/models.py +++ b/backend/geonature/core/imports/models.py @@ -94,7 +94,12 @@ class ImportUserError(db.Model): type = db.relationship("ImportUserErrorType") column = db.Column("column_error", db.Unicode) rows = db.Column("id_rows", db.ARRAY(db.Integer)) - comment = db.Column(db.UnicodeText) + comment = db.Column(db.UnicodeText), + id_entity = db.Column( + db.Integer, + db.ForeignKey("gn_imports.bib_entities.id_entity", onupdate="CASCADE", ondelete="CASCADE"), + ) + entity = db.relationship("Entity") def __str__(self): return f"" diff --git a/backend/geonature/core/imports/routes/imports.py b/backend/geonature/core/imports/routes/imports.py index 427d0fa25c..9659629518 100644 --- a/backend/geonature/core/imports/routes/imports.py +++ b/backend/geonature/core/imports/routes/imports.py @@ -510,7 +510,7 @@ def get_import_errors(scope, imprt): """ if not imprt.has_instance_permission(scope): raise Forbidden - return jsonify([error.as_dict(fields=["type"]) for error in imprt.errors]) + return jsonify([error.as_dict(fields=["type", "entity"]) for error in imprt.errors]) @blueprint.route("//imports//source_file", methods=["GET"]) @@ -644,7 +644,7 @@ def export_pdf(scope, imprt): """ if not imprt.has_instance_permission(scope): raise Forbidden - ctx = imprt.as_dict(fields=["errors", "errors.type", "dataset.dataset_name"]) + ctx = imprt.as_dict(fields=["errors", "errors.type", "errors.entity", "dataset.dataset_name"]) ctx["map"] = request.form.get("map") ctx["chart"] = request.form.get("chart") diff --git a/backend/geonature/core/imports/templates/import_template_pdf.html b/backend/geonature/core/imports/templates/import_template_pdf.html index 00f21f10ab..7b4951a6e7 100644 --- a/backend/geonature/core/imports/templates/import_template_pdf.html +++ b/backend/geonature/core/imports/templates/import_template_pdf.html @@ -138,6 +138,7 @@
Zone géographique
Type d'erreur Champ Nombre d'erreur(s) + Entité @@ -146,6 +147,7 @@
Zone géographique
{{ error.type.description }} {{ error.column }} {{ error.rows | length }} + {{ error.entity.label }} {% endfor %} diff --git a/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py b/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py new file mode 100644 index 0000000000..0ac1ce6874 --- /dev/null +++ b/backend/geonature/migrations/versions/imports/bfc90691737d_add_id_entity_in_users_errors.py @@ -0,0 +1,53 @@ +"""Add id_entity in users errors + +Revision ID: bfc90691737d +Revises: 2b0b3bd0248c +Create Date: 2024-02-15 16:20:57.049889 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'bfc90691737d' +down_revision = '2b0b3bd0248c' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column( + schema="gn_imports", + table_name="t_user_errors", + column=sa.Column( + "id_entity", + sa.Integer, + ), + ) + op.create_foreign_key( + constraint_name="t_user_errors_id_entity_fkey", + source_schema="gn_imports", + source_table="t_user_errors", + local_cols=["id_entity"], + referent_schema="gn_imports", + referent_table="bib_entities", + remote_cols=["id_entity"], + onupdate="CASCADE", + ondelete="CASCADE", + ) + pass + + +def downgrade(): + op.drop_constraint( + schema="gn_imports", + table_name="t_user_errors", + constraint_name="t_user_errors_id_entity_fkey", + ) + op.drop_column( + schema="gn_imports", + table_name="t_user_errors", + column_name="id_entity" + ) + pass diff --git a/frontend/src/app/modules/imports/components/import_errors/import_errors.component.html b/frontend/src/app/modules/imports/components/import_errors/import_errors.component.html index 67f672e247..a1c78e9a42 100644 --- a/frontend/src/app/modules/imports/components/import_errors/import_errors.component.html +++ b/frontend/src/app/modules/imports/components/import_errors/import_errors.component.html @@ -34,7 +34,7 @@

Erreurs

Champ Description erreur Nombre d'erreur(s) - Numéro des lignes en erreur + Entité @@ -44,6 +44,7 @@

Erreurs

{{error.type.description}}
{{error.comment}}
{{error.rows.length || ''}} {{error.rows.join(', ')}} + {{error.entity.label}} diff --git a/frontend/src/app/modules/imports/components/import_report/import_report.component.html b/frontend/src/app/modules/imports/components/import_report/import_report.component.html index 1173bcc129..d33efd5ddb 100644 --- a/frontend/src/app/modules/imports/components/import_report/import_report.component.html +++ b/frontend/src/app/modules/imports/components/import_report/import_report.component.html @@ -167,6 +167,7 @@
{{ importErrors.length }} erreur(s)
Description erreur Nombre d'erreur(s) Numéro des lignes en erreur + Entité @@ -197,6 +198,9 @@
{{ importErrors.length }} erreur(s)
+ + {{ error.entity.label }} + @@ -215,6 +219,7 @@
{{ importWarnings.length }} alerte(s)
Description erreur Nombre d'erreur(s) Numéro des lignes en erreur + Entité @@ -245,6 +250,9 @@
{{ importWarnings.length }} alerte(s)
+ + {{ error.entity.label }} +