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 siret = entreprise_data[0].strip() # vérification sur le siret
if re.match("^\d{14}$", siret) is None: if re.match("^\d{14}$", siret) is None:
return False 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: if req.status_code != 200:
return False return False
entreprise = Entreprise.query.filter_by(siret=siret).first() entreprise = Entreprise.query.filter_by(siret=siret).first()

View File

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

View File

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

View File

@ -85,4 +85,10 @@
.offre-recue { .offre-recue {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
}
#sirene-data {
border: solid 2px;
border-radius: 10px;
padding: 10px;
} }

View File

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