From 37832c5bb1418a3a70ffbb4702893af66c907840 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Tue, 12 Apr 2022 19:44:09 +0200 Subject: [PATCH] fichier - formualaire offre, minimum 1 dept pour une offre, ameliorations date expiration --- app/entreprises/forms.py | 38 +++++++++++++++++++++++------ app/entreprises/routes.py | 14 ++++++++++- app/templates/entreprises/form.html | 7 ++++-- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 796ae027f7..603f6d845c 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -82,13 +82,6 @@ class EntrepriseCreationForm(FlaskForm): submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) - def validate(self): - validate = True - if not FlaskForm.validate(self): - validate = False - - return validate - def validate_siret(self, siret): if EntreprisePreferences.get_check_siret(): siret_data = siret.data.replace(" ", "") @@ -151,7 +144,14 @@ class OffreCreationForm(FlaskForm): duree = _build_string_field("Durée (*)") depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) expiration_date = DateField("Date expiration", validators=[Optional()]) - correspondant = SelectField("Correspondant à contacté (*)", validators=[Optional()]) + correspondant = SelectField("Correspondant à contacté", validators=[Optional()]) + fichier = FileField( + "Fichier (*)", + validators=[ + Optional(), + FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"), + ], + ) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) def __init__(self, *args, **kwargs): @@ -168,6 +168,17 @@ class OffreCreationForm(FlaskForm): (dept.id, dept.acronym) for dept in Departement.query.all() ] + def validate(self): + validate = True + if not FlaskForm.validate(self): + validate = False + + if len(self.depts.data) < 1: + self.depts.errors.append("Choisir au moins un département") + validate = False + + return validate + class OffreModificationForm(FlaskForm): hidden_entreprise_id = HiddenField() @@ -203,6 +214,17 @@ class OffreModificationForm(FlaskForm): (dept.id, dept.acronym) for dept in Departement.query.all() ] + def validate(self): + validate = True + if not FlaskForm.validate(self): + validate = False + + if len(self.depts.data) < 1: + self.depts.errors.append("Choisir au moins un département") + validate = False + + return validate + class CorrespondantCreationForm(FlaskForm): nom = _build_string_field("Nom (*)", render_kw={"class": "form-control"}) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 6cdeb437e0..91a595fd2e 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -18,7 +18,6 @@ from app.entreprises.forms import ( SuppressionConfirmationForm, OffreCreationForm, OffreModificationForm, - CorrespondantCreationForm, CorrespondantModificationForm, ContactCreationForm, ContactModificationForm, @@ -509,6 +508,19 @@ def add_offre(id): dept_id=dept, ) db.session.add(offre_dept) + if form.fichier.data: + date = f"{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}" + path = os.path.join( + Config.SCODOC_VAR_DIR, + "entreprises", + f"{offre.entreprise_id}", + f"{offre.id}", + f"{date}", + ) + os.makedirs(path) + file = form.fichier.data + filename = secure_filename(file.filename) + file.save(os.path.join(path, filename)) log = EntrepriseLog( authenticated_user=current_user.user_name, object=entreprise.id, diff --git a/app/templates/entreprises/form.html b/app/templates/entreprises/form.html index 5aa9fc6d72..59cf878528 100644 --- a/app/templates/entreprises/form.html +++ b/app/templates/entreprises/form.html @@ -25,14 +25,17 @@ var closest_form_control = champ_depts.closest(".form-control") closest_form_control.classList.remove("form-control") } - + + if(document.getElementById("expiration_date").value === "") + expiration() + document.getElementById("type_offre").addEventListener("change", expiration); function expiration() { var date = new Date() var expiration = document.getElementById("expiration_date") var type_offre = document.getElementById("type_offre").value - if (type_offre == "Alternance") { + if (type_offre === "Alternance") { expiration.value = `${date.getFullYear() + 1}-01-01` } else { if(date.getMonth() + 1 < 7)