From c86d780585bedf58a10a9ddbcba49320a8f37e92 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Wed, 23 Feb 2022 19:12:26 +0100 Subject: [PATCH] =?UTF-8?q?page=20preferences=20fonctionnelle=20(mail=20a?= =?UTF-8?q?=20v=C3=A9rifier)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/entreprises/app_relations_entreprises.py | 34 +++++++++++++++ app/entreprises/forms.py | 37 ++++++++-------- app/entreprises/models.py | 44 ++++++++++++++++++++ app/entreprises/routes.py | 36 ++++------------ 4 files changed, 105 insertions(+), 46 deletions(-) diff --git a/app/entreprises/app_relations_entreprises.py b/app/entreprises/app_relations_entreprises.py index a08b0417f..421f08824 100644 --- a/app/entreprises/app_relations_entreprises.py +++ b/app/entreprises/app_relations_entreprises.py @@ -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) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 4f2cfd849..5b1b175f8 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -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,23 +100,24 @@ class EntrepriseCreationForm(FlaskForm): return validate def validate_siret(self, siret): - 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() - if entreprise is not None: - lien = f'ici' - raise ValidationError( - Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}") - ) + if EntreprisePreferences.get_check_siret(): + 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() + if entreprise is not None: + lien = f'ici' + raise ValidationError( + Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}") + ) class EntrepriseModificationForm(FlaskForm): diff --git a/app/entreprises/models.py b/app/entreprises/models.py index ce0acc984..923b2445f 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -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() diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index c3bfcfecd..301813341 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -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",