cadre info base sirene, gestion erreur (a tester)

This commit is contained in:
Arthur ZHU 2022-02-18 19:57:19 +01:00
parent b849c350f1
commit 9b21157fb7
6 changed files with 84 additions and 8 deletions

View File

@ -144,7 +144,12 @@ def verif_entreprise_data(entreprise_data):
siret = entreprise_data[0].strip() # vérification sur le siret
if re.match("^\d{14}$", siret) is None:
return False
req = requests.get(f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}")
try:
req = requests.get(
f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}"
)
except requests.ConnectionError:
print("no internet")
if req.status_code != 200:
return False
entreprise = Entreprise.query.filter_by(siret=siret).first()

View File

@ -102,9 +102,12 @@ class EntrepriseCreationForm(FlaskForm):
siret = siret.data.strip()
if re.match("^\d{14}$", siret) is None:
raise ValidationError("Format incorrect")
try:
req = requests.get(
f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}"
)
except requests.ConnectionError:
print("no internet")
if req.status_code != 200:
raise ValidationError("SIRET inexistant")
entreprise = Entreprise.query.filter_by(siret=siret).first()

View File

@ -364,7 +364,7 @@ def edit_entreprise(id):
form.ville.data = entreprise.ville
form.pays.data = entreprise.pays
return render_template(
"entreprises/form.html",
"entreprises/form_modification_entreprise.html",
title="Modification entreprise",
form=form,
)

View File

@ -86,3 +86,9 @@
display: flex;
justify-content: space-between;
}
#sirene-data {
border: solid 2px;
border-radius: 10px;
padding: 10px;
}

View File

@ -20,7 +20,6 @@
function autocomplete() {
var input = document.getElementById("siret").value;
data = null
if(input.length == 14) {
fetch("https://entreprise.data.gouv.fr/api/sirene/v1/siret/" + input)
.then(response => {
@ -40,7 +39,6 @@
document.getElementById("adresse").value = response.etablissement.l4_normalisee
document.getElementById("codepostal").value = response.etablissement.code_postal
document.getElementById("ville").value = response.etablissement.libelle_commune
document.getElementById("pays").value = 'FRANCE'
}
function emptyForm() {
@ -48,7 +46,6 @@
document.getElementById("adresse").value = ''
document.getElementById("codepostal").value = ''
document.getElementById("ville").value = ''
document.getElementById("pays").value = ''
}
}

View File

@ -0,0 +1,65 @@
{# -*- mode: jinja-html -*- #}
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block styles %}
{{super()}}
{% endblock %}
{% block app_content %}
<h1>{{ title }}</h1>
<br>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form, novalidate=True) }}
</div>
<div id="sirene-data" class="col-md-5">
<b>Informations de l'API Sirene</b>
<div id="nom_entreprise_base"></div>
<div id="adresse_base"></div>
<div id="codepostal_base"></div>
<div id="ville_base"></div>
<a class="btn btn-primary" onclick="getData()">Copier</a>
</div>
</div>
<script>
var value = document.getElementById("siret").value;
fetch("https://entreprise.data.gouv.fr/api/sirene/v1/siret/" + value)
.then(response => {
if(response.ok)
return response.json()
})
.then(response => showSireneData(response))
.catch(err => {
document.getElementById("sirene-data").style.display = "none"
return err
})
function showSireneData(response) {
document.getElementById("nom_entreprise_base").innerHTML = "Nom de l'entreprise: " + response.etablissement.l1_normalisee
document.getElementById("adresse_base").innerHTML = "Adresse: " + response.etablissement.l4_normalisee
document.getElementById("codepostal_base").innerHTML = "Code postal: " + response.etablissement.code_postal
document.getElementById("ville_base").innerHTML = "Ville: " + response.etablissement.libelle_commune
}
function getData() {
var value = document.getElementById("siret").value;
fetch("https://entreprise.data.gouv.fr/api/sirene/v1/siret/" + value)
.then(response => {
if(response.ok)
return response.json()
})
.then(response => fillForm(response))
.catch(err => err)
}
function fillForm(response) {
document.getElementById("nom").value = response.etablissement.l1_normalisee
document.getElementById("adresse").value = response.etablissement.l4_normalisee
document.getElementById("codepostal").value = response.etablissement.code_postal
document.getElementById("ville").value = response.etablissement.libelle_commune
}
</script>
{% endblock %}