page preferences fonctionnelle (mail a vérifier)

This commit is contained in:
Arthur ZHU 2022-02-23 19:12:26 +01:00
parent 722ec09eb5
commit c86d780585
4 changed files with 105 additions and 46 deletions

View File

@ -37,8 +37,11 @@ from app.entreprises.models import (
EntrepriseContact, EntrepriseContact,
EntrepriseOffre, EntrepriseOffre,
EntrepriseOffreDepartement, EntrepriseOffreDepartement,
EntreprisePreferences,
) )
from app import email
from app.scodoc import sco_preferences
from app.models import Departement from app.models import Departement
from app.scodoc.sco_permissions import Permission from app.scodoc.sco_permissions import Permission
@ -101,6 +104,37 @@ def get_offre_files_and_depts(offre: EntrepriseOffre, depts: list):
return None return None
def send_email_notifications_entreprise(
subject, entreprise: Entreprise, contact: EntrepriseContact
):
txt = [
"Une entreprise est en attente de validation",
"Entreprise:",
f"\tnom: {entreprise.nom}",
f"\tsiret: {entreprise.siret}",
f"\tadresse: {entreprise.adresse}",
f"\tcode postal: {entreprise.codepostal}",
f"\tville: {entreprise.ville}",
f"\tpays: {entreprise.pays}",
"",
"Contact:",
f"nom: {contact.nom}",
f"prenom: {contact.prenom}",
f"telephone: {contact.telephone}",
f"mail: {contact.mail}",
f"poste: {contact.poste}",
f"service: {contact.service}",
]
txt = "\n".join(txt)
email.send_email(
subject,
sco_preferences.get_preference("email_from_addr"),
[EntreprisePreferences.get_email_notifications],
txt,
)
return txt
def verif_contact_data(contact_data): def verif_contact_data(contact_data):
""" """
Verifie les données d'une ligne Excel (contact) Verifie les données d'une ligne Excel (contact)

View File

@ -44,7 +44,7 @@ from wtforms import (
from wtforms.validators import ValidationError, DataRequired, Email, Optional from wtforms.validators import ValidationError, DataRequired, Email, Optional
from wtforms.widgets import ListWidget, CheckboxInput from wtforms.widgets import ListWidget, CheckboxInput
from app.entreprises.models import Entreprise, EntrepriseContact from app.entreprises.models import Entreprise, EntrepriseContact, EntreprisePreferences
from app.models import Identite, Departement from app.models import Identite, Departement
from app.auth.models import User from app.auth.models import User
@ -100,23 +100,24 @@ class EntrepriseCreationForm(FlaskForm):
return validate return validate
def validate_siret(self, siret): def validate_siret(self, siret):
siret = siret.data.strip() if EntreprisePreferences.get_check_siret():
if re.match("^\d{14}$", siret) is None: siret = siret.data.strip()
raise ValidationError("Format incorrect") if re.match("^\d{14}$", siret) is None:
try: 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") except requests.ConnectionError:
if req.status_code != 200: print("no internet")
raise ValidationError("SIRET inexistant") if req.status_code != 200:
entreprise = Entreprise.query.filter_by(siret=siret).first() raise ValidationError("SIRET inexistant")
if entreprise is not None: entreprise = Entreprise.query.filter_by(siret=siret).first()
lien = f'<a href="/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}">ici</a>' if entreprise is not None:
raise ValidationError( lien = f'<a href="/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}">ici</a>'
Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}") raise ValidationError(
) Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}")
)
class EntrepriseModificationForm(FlaskForm): class EntrepriseModificationForm(FlaskForm):

View File

@ -146,3 +146,47 @@ class EntreprisePreferences(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text) name = db.Column(db.Text)
value = db.Column(db.Text) value = db.Column(db.Text)
@classmethod
def get_email_notifications(cls):
mail = EntreprisePreferences.query.filter_by(
name="mail_notifications_entreprise"
).first()
if mail is None:
return ""
else:
return mail.value
@classmethod
def set_email_notifications(cls, mail: str):
if mail != cls.get_email_notifications():
m = EntreprisePreferences.query.filter_by(
name="mail_notifications_entreprise"
).first()
if m is None:
prefs = EntreprisePreferences(
name="mail_notifications_entreprise",
value=mail,
)
db.session.add(prefs)
else:
m.value = mail
db.session.commit()
@classmethod
def get_check_siret(cls):
check_siret = EntreprisePreferences.query.filter_by(name="check_siret").first()
if check_siret is None:
return 1
else:
return int(check_siret.value)
@classmethod
def set_check_siret(cls, check_siret: int):
cs = EntreprisePreferences.query.filter_by(name="check_siret").first()
if cs is None:
prefs = EntreprisePreferences(name="check_siret", value=check_siret)
db.session.add(prefs)
else:
cs.value = check_siret
db.session.commit()

View File

@ -304,6 +304,10 @@ def add_entreprise():
else: else:
entreprise.visible = False entreprise.visible = False
db.session.commit() db.session.commit()
if EntreprisePreferences.get_email_notifications():
are.send_email_notifications_entreprise(
"entreprise en attente de validation", entreprise, contact
)
flash("L'entreprise a été ajouté à la liste pour la validation.") flash("L'entreprise a été ajouté à la liste pour la validation.")
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template( return render_template(
@ -1231,36 +1235,12 @@ def delete_offre_file(offre_id, filedir):
def preferences(): def preferences():
form = PreferencesForm() form = PreferencesForm()
if form.validate_on_submit(): if form.validate_on_submit():
exists = EntreprisePreferences.query.filter_by(name="check_siret").first() EntreprisePreferences.set_email_notifications(form.mail_entreprise.data.strip())
if not exists: EntreprisePreferences.set_check_siret(int(form.check_siret.data))
prefs = EntreprisePreferences(
name="check_siret", value=int(form.check_siret.data)
)
db.session.add(prefs)
else:
exists.value = int(form.check_siret.data)
db.session.commit()
exists = EntreprisePreferences.query.filter_by(
name="mail_notifications_entreprise"
).first()
if not exists and form.mail_entreprise.data:
prefs = EntreprisePreferences(
name="mail_notifications_entreprise",
value=form.mail_entreprise.data.strip(),
)
db.session.add(prefs)
else:
exists.value = form.mail_entreprise.data
db.session.commit()
db.session.commit()
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
elif request.method == "GET": elif request.method == "GET":
mail = EntreprisePreferences.query.filter_by( form.mail_entreprise.data = EntreprisePreferences.get_email_notifications()
name="mail_notifications_entreprise" form.check_siret.data = int(EntreprisePreferences.get_check_siret())
).first()
check_siret = EntreprisePreferences.query.filter_by(name="check_siret").first()
form.mail_entreprise.data = mail.value
form.check_siret.data = int(check_siret.value)
return render_template( return render_template(
"entreprises/preferences.html", "entreprises/preferences.html",
title="Préférences", title="Préférences",