From f1bb718f083230d21ee658d6756b2bb535518ac0 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Wed, 9 Feb 2022 20:09:14 +0100 Subject: [PATCH] import entreprises par fichier excel, divers corrections --- app/entreprises/routes.py | 43 +++++++++++++++---- app/templates/entreprises/contacts.html | 2 +- app/templates/entreprises/entreprises.html | 2 +- .../entreprises/import_entreprises.html | 4 +- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 4611336d..d5c34597 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -879,9 +879,8 @@ def get_import_entreprises_file_sample(): def verif_entreprise_data(entreprise_data): - print(entreprise_data) for data in entreprise_data: - if data is "": + if data == "": return False siret = entreprise_data[0].strip() if re.match("^\d{14}$", siret) is None: @@ -911,8 +910,16 @@ def import_entreprises(): data = sco_excel.excel_file_to_list(file_path) os.remove(file_path) entreprises_import = [] + siret_list = [] + ligne = 0 + for entreprise_data in data[1][1:]: - if verif_entreprise_data(entreprise_data): + ligne += 1 + if ( + verif_entreprise_data(entreprise_data) + and entreprise_data[0] not in siret_list + ): + siret_list.append(entreprise_data[0]) entreprise = Entreprise( siret=entreprise_data[0], nom=entreprise_data[1], @@ -920,14 +927,32 @@ def import_entreprises(): ville=entreprise_data[3], codepostal=entreprise_data[4], pays=entreprise_data[5], + visible=True, ) entreprises_import.append(entreprise) - return render_template( - "entreprises/import_entreprises.html", - title=("Importation entreprises"), - form=form, - entreprises_import=entreprises_import, - ) + + else: + flash(f"Erreur lors de l'importation (ligne {ligne})") + return render_template( + "entreprises/import_entreprises.html", + title=("Importation entreprises"), + form=form, + ) + + if len(entreprises_import) > 0: + for entreprise in entreprises_import: + db.session.add(entreprise) + db.session.commit() + flash(f"Importation réussie de {len(entreprises_import)} entreprise(s)") + return render_template( + "entreprises/import_entreprises.html", + title=("Importation entreprises"), + form=form, + entreprises_import=entreprises_import, + ) + else: + flash('Feuille "Entreprises" vide') + return render_template( "entreprises/import_entreprises.html", title=("Importation entreprises"), diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index 264205e5..ee92442c 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -15,7 +15,7 @@ {% endif %} - {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and contacts.items %}
{% if contacts %} Exporter la liste des contacts diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index ab658243..a820a38d 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -23,7 +23,7 @@ Importer des entreprises {% endif %} {% if entreprises %} - {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and entreprises.items%} Exporter la liste des entreprises {% endif %} {% endif %} diff --git a/app/templates/entreprises/import_entreprises.html b/app/templates/entreprises/import_entreprises.html index dfa84487..7d64196b 100644 --- a/app/templates/entreprises/import_entreprises.html +++ b/app/templates/entreprises/import_entreprises.html @@ -20,6 +20,7 @@
{% if entreprises_import %} +
Importation de {{ entreprises_import|length }} entreprise(s)
{% for entreprise in entreprises_import %}
@@ -32,8 +33,5 @@
{% endfor %} - Ajouter - {% else %} -
Aucune entreprise importée (vérifier le fichier .xlsx)
{% endif %} {% endblock %} \ No newline at end of file