From 5444c8e848ef0e91d419f132271dea7c80a843dc Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Thu, 3 Feb 2022 18:07:16 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20offre=20pouvant=20etre=20li=C3=A9=20a?= =?UTF-8?q?=20des=20depts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/entreprises/forms.py | 22 +++++++++++++++++++--- app/entreprises/routes.py | 11 ++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 6836048d..1451f25c 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -31,12 +31,19 @@ from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed, FileRequired from markupsafe import Markup from sqlalchemy import text -from wtforms import StringField, SubmitField, TextAreaField, SelectField, HiddenField +from wtforms import ( + StringField, + SubmitField, + TextAreaField, + SelectField, + HiddenField, + SelectMultipleField, +) from wtforms.fields import EmailField, DateField -from wtforms.validators import ValidationError, DataRequired, Email +from wtforms.validators import ValidationError, DataRequired, Email, Optional from app.entreprises.models import Entreprise, EntrepriseContact -from app.models import Identite +from app.models import Identite, Departement from app.auth.models import User CHAMP_REQUIS = "Ce champ est requis" @@ -125,6 +132,7 @@ class EntrepriseModificationForm(FlaskForm): class OffreCreationForm(FlaskForm): + intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)]) description = TextAreaField( "Description", validators=[DataRequired(message=CHAMP_REQUIS)] @@ -138,11 +146,19 @@ class OffreCreationForm(FlaskForm): "Missions", validators=[DataRequired(message=CHAMP_REQUIS)] ) duree = StringField("Durée", validators=[DataRequired(message=CHAMP_REQUIS)]) + depts = SelectMultipleField("Départements", validators=[Optional()], coerce=int) expiration_date = DateField( "Date expiration", validators=[DataRequired(message=CHAMP_REQUIS)] ) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.depts.choices = [ + (dept.id, dept.acronym) for dept in Departement.query.all() + ] + class OffreModificationForm(FlaskForm): intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)]) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 32592457..bd39da00 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -32,6 +32,7 @@ from app.entreprises.models import ( EntrepriseLog, EntrepriseEtudiant, EntrepriseEnvoiOffre, + EntrepriseOffreDepartement, ) from app.models import Identite from app.auth.models import User @@ -462,12 +463,20 @@ def add_offre(id): duree=form.duree.data.strip(), expiration_date=form.expiration_date.data, ) + db.session.add(offre) + db.session.commit() + db.session.refresh(offre) + for dept in form.depts.data: + offre_dept = EntrepriseOffreDepartement( + offre_id=offre.id, + dept_id=dept, + ) + db.session.add(offre_dept) log = EntrepriseLog( authenticated_user=current_user.user_name, object=entreprise.id, text="Création d'une offre", ) - db.session.add(offre) db.session.add(log) db.session.commit() flash("L'offre a été ajouté à la fiche entreprise.")