1
0
forked from ScoDoc/ScoDoc

suite import entreprises par excel

This commit is contained in:
Arthur ZHU 2022-02-08 19:13:45 +01:00
parent 6d7a7a7c4b
commit 42e74174ba
3 changed files with 60 additions and 10 deletions

View File

@ -110,7 +110,7 @@ class EntrepriseCreationForm(FlaskForm):
def validate_siret(self, siret): def validate_siret(self, siret):
siret = siret.data.strip() siret = siret.data.strip()
if re.match("^\d{14}$", siret) == None: if re.match("^\d{14}$", siret) is None:
raise ValidationError("Format incorrect") raise ValidationError("Format incorrect")
req = requests.get( req = requests.get(
f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}" f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}"

View File

@ -1,9 +1,10 @@
import os import os
from queue import Empty
from config import Config from config import Config
from datetime import datetime, date from datetime import datetime, date
import glob import glob
import shutil import shutil
import re
import requests
from flask import render_template, redirect, url_for, request, flash, send_file, abort from flask import render_template, redirect, url_for, request, flash, send_file, abort
from flask.json import jsonify from flask.json import jsonify
@ -869,12 +870,6 @@ def get_import_entreprises_file_sample():
"ville", "ville",
"codepostal", "codepostal",
"pays", "pays",
"nom_contact",
"prenom_contact",
"telephone",
"mail",
"poste",
"service",
] ]
titles = keys[:] titles = keys[:]
title = "ImportEntreprises" title = "ImportEntreprises"
@ -883,6 +878,23 @@ def get_import_entreprises_file_sample():
return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) 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"]) @bp.route("/import_entreprises", methods=["GET", "POST"])
@permission_required(Permission.RelationsEntreprisesExport) @permission_required(Permission.RelationsEntreprisesExport)
def import_entreprises(): def import_entreprises():
@ -894,8 +906,28 @@ def import_entreprises():
path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") path = os.path.join(Config.SCODOC_VAR_DIR, "tmp")
file = form.fichier.data file = form.fichier.data
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
file.save(os.path.join(path, filename)) file_path = os.path.join(path, filename)
# print(sco_excel.excel_file_to_list(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( return render_template(
"entreprises/import_entreprises.html", "entreprises/import_entreprises.html",
title=("Importation entreprises"), title=("Importation entreprises"),

View File

@ -18,4 +18,22 @@
{{ wtf.quick_form(form, novalidate=True) }} {{ wtf.quick_form(form, novalidate=True) }}
</div> </div>
</div> </div>
{% if entreprises_import %}
{% for entreprise in entreprises_import %}
<div class="entreprise">
<div>
SIRET : {{ entreprise.siret }}<br>
Nom : {{ entreprise.nom }}<br>
Adresse : {{ entreprise.adresse }}<br>
Code postal : {{ entreprise.codepostal }}<br>
Ville : {{ entreprise.ville }}<br>
Pays : {{ entreprise.pays }}
</div>
</div>
{% endfor %}
<a class="btn btn-default" href="#">Ajouter</a>
{% else %}
<div>Aucune entreprise importée (vérifier le fichier .xlsx)</div>
{% endif %}
{% endblock %} {% endblock %}