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,
EntrepriseOffre,
EntrepriseOffreDepartement,
EntreprisePreferences,
)
from app import email
from app.scodoc import sco_preferences
from app.models import Departement
from app.scodoc.sco_permissions import Permission
@ -101,6 +104,37 @@ def get_offre_files_and_depts(offre: EntrepriseOffre, depts: list):
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):
"""
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.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.auth.models import User
@ -100,6 +100,7 @@ class EntrepriseCreationForm(FlaskForm):
return validate
def validate_siret(self, siret):
if EntreprisePreferences.get_check_siret():
siret = siret.data.strip()
if re.match("^\d{14}$", siret) is None:
raise ValidationError("Format incorrect")

View File

@ -146,3 +146,47 @@ class EntreprisePreferences(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = 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:
entreprise.visible = False
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.")
return redirect(url_for("entreprises.index"))
return render_template(
@ -1231,36 +1235,12 @@ def delete_offre_file(offre_id, filedir):
def preferences():
form = PreferencesForm()
if form.validate_on_submit():
exists = EntreprisePreferences.query.filter_by(name="check_siret").first()
if not exists:
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()
EntreprisePreferences.set_email_notifications(form.mail_entreprise.data.strip())
EntreprisePreferences.set_check_siret(int(form.check_siret.data))
return redirect(url_for("entreprises.index"))
elif request.method == "GET":
mail = EntreprisePreferences.query.filter_by(
name="mail_notifications_entreprise"
).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)
form.mail_entreprise.data = EntreprisePreferences.get_email_notifications()
form.check_siret.data = int(EntreprisePreferences.get_check_siret())
return render_template(
"entreprises/preferences.html",
title="Préférences",