diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 006902be0..19c562ab3 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -110,7 +110,7 @@ class EntrepriseCreationForm(FlaskForm): def validate_siret(self, siret): siret = siret.data.strip() - if re.match("^\d{14}$", siret) == None: + if re.match("^\d{14}$", siret) is None: raise ValidationError("Format incorrect") req = requests.get( f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}" diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 1b01f9634..4611336d5 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -1,9 +1,10 @@ import os -from queue import Empty from config import Config from datetime import datetime, date import glob import shutil +import re +import requests from flask import render_template, redirect, url_for, request, flash, send_file, abort from flask.json import jsonify @@ -869,12 +870,6 @@ def get_import_entreprises_file_sample(): "ville", "codepostal", "pays", - "nom_contact", - "prenom_contact", - "telephone", - "mail", - "poste", - "service", ] titles = keys[:] title = "ImportEntreprises" @@ -883,6 +878,23 @@ def get_import_entreprises_file_sample(): return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) +def verif_entreprise_data(entreprise_data): + print(entreprise_data) + for data in entreprise_data: + if data is "": + return False + siret = entreprise_data[0].strip() + if re.match("^\d{14}$", siret) is None: + return False + req = requests.get(f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}") + if req.status_code != 200: + return False + entreprise = Entreprise.query.filter_by(siret=siret).first() + if entreprise is not None: + return False + return True + + @bp.route("/import_entreprises", methods=["GET", "POST"]) @permission_required(Permission.RelationsEntreprisesExport) def import_entreprises(): @@ -894,8 +906,28 @@ def import_entreprises(): path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") file = form.fichier.data filename = secure_filename(file.filename) - file.save(os.path.join(path, filename)) - # print(sco_excel.excel_file_to_list(filename)) + file_path = os.path.join(path, filename) + file.save(file_path) + data = sco_excel.excel_file_to_list(file_path) + os.remove(file_path) + entreprises_import = [] + for entreprise_data in data[1][1:]: + if verif_entreprise_data(entreprise_data): + entreprise = Entreprise( + siret=entreprise_data[0], + nom=entreprise_data[1], + adresse=entreprise_data[2], + ville=entreprise_data[3], + codepostal=entreprise_data[4], + pays=entreprise_data[5], + ) + entreprises_import.append(entreprise) + return render_template( + "entreprises/import_entreprises.html", + title=("Importation entreprises"), + form=form, + entreprises_import=entreprises_import, + ) return render_template( "entreprises/import_entreprises.html", title=("Importation entreprises"), diff --git a/app/templates/entreprises/import_entreprises.html b/app/templates/entreprises/import_entreprises.html index 5e1a50d77..dfa844874 100644 --- a/app/templates/entreprises/import_entreprises.html +++ b/app/templates/entreprises/import_entreprises.html @@ -18,4 +18,22 @@ {{ wtf.quick_form(form, novalidate=True) }} + + {% if entreprises_import %} + {% for entreprise in entreprises_import %} +
+
+ SIRET : {{ entreprise.siret }}
+ Nom : {{ entreprise.nom }}
+ Adresse : {{ entreprise.adresse }}
+ Code postal : {{ entreprise.codepostal }}
+ Ville : {{ entreprise.ville }}
+ Pays : {{ entreprise.pays }} +
+
+ {% endfor %} + Ajouter + {% else %} +
Aucune entreprise importée (vérifier le fichier .xlsx)
+ {% endif %} {% endblock %} \ No newline at end of file