ajout offre pouvant etre lié a des depts

This commit is contained in:
Arthur ZHU 2022-02-03 18:07:16 +01:00
parent d19bc3391f
commit 5444c8e848
2 changed files with 29 additions and 4 deletions

View File

@ -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)])

View File

@ -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.")