diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index f7fd62816..d2f3762b6 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -3,10 +3,10 @@ from markupsafe import Markup from flask.app import Flask import requests, re from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField, TextAreaField, SelectField, FileField +from wtforms import StringField, SubmitField, TextAreaField, SelectField, FileField, HiddenField from wtforms.fields.html5 import EmailField, DateField from wtforms.validators import ValidationError, DataRequired, Email -from app.entreprises.models import Entreprise +from app.entreprises.models import Entreprise, EntrepriseContact from app.models import Identite from app.auth.models import User from app.scodoc import sco_etud @@ -71,8 +71,27 @@ class ContactCreationForm(FlaskForm): prenom = StringField("Prénom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) telephone = StringField("Téléphone", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) mail = EmailField("Mail", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) + hidden_entreprise_id = HiddenField() submit = SubmitField("Envoyer") + def validate(self): + rv = FlaskForm.validate(self) + if not rv: + return False + + contact = EntrepriseContact.query.filter_by( + entreprise_id = self.hidden_entreprise_id.data, + nom = self.nom.data, + prenom = self.prenom.data + ).first() + + if contact is not None: + self.nom.errors.append("Ce contact existe déjà (même nom et prénom)") + self.prenom.errors.append("") + return False + + return True + class ContactModificationForm(FlaskForm): nom = StringField("Nom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) prenom = StringField("Prénom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 4808cbebe..1791199e5 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -238,7 +238,7 @@ def delete_offre(id): @bp.route("/add_contact/", methods=["GET", "POST"]) def add_contact(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() - form = ContactCreationForm() + form = ContactCreationForm(hidden_entreprise_id=entreprise.id) if form.validate_on_submit(): contact = EntrepriseContact( entreprise_id=entreprise.id, diff --git a/app/templates/entreprises/ajout_entreprise.html b/app/templates/entreprises/ajout_entreprise.html index 76a9619e4..6b0ba311e 100644 --- a/app/templates/entreprises/ajout_entreprise.html +++ b/app/templates/entreprises/ajout_entreprise.html @@ -12,6 +12,9 @@
+

+ Les champs s'autocomplète selon le SIRET +

{{ wtf.quick_form(form, novalidate=True) }}