refactoring

This commit is contained in:
Arthur ZHU 2022-02-11 19:18:01 +01:00
parent c7bd3b19c8
commit 0cef1089b3
4 changed files with 150 additions and 158 deletions

View File

@ -38,8 +38,8 @@ from wtforms import (
SelectField, SelectField,
HiddenField, HiddenField,
SelectMultipleField, SelectMultipleField,
DateField,
) )
from wtforms.fields import EmailField, DateField
from wtforms.validators import ValidationError, DataRequired, Email, Optional from wtforms.validators import ValidationError, DataRequired, Email, Optional
from wtforms.widgets import ListWidget, CheckboxInput from wtforms.widgets import ListWidget, CheckboxInput
@ -48,65 +48,55 @@ from app.models import Identite, Departement
from app.auth.models import User from app.auth.models import User
CHAMP_REQUIS = "Ce champ est requis" CHAMP_REQUIS = "Ce champ est requis"
SUBMIT_MARGE = {"style": "margin-bottom: 10px;"}
def _build_string_field(label, required=True, render_kw=None):
if required:
return StringField(
label,
validators=[DataRequired(message=CHAMP_REQUIS)],
render_kw=render_kw,
)
else:
return StringField(label, validators=[Optional()], render_kw=render_kw)
class EntrepriseCreationForm(FlaskForm): class EntrepriseCreationForm(FlaskForm):
siret = StringField( siret = _build_string_field(
"SIRET", "SIRET",
validators=[DataRequired(message=CHAMP_REQUIS)],
render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"}, render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"},
) )
nom_entreprise = StringField( nom_entreprise = _build_string_field("Nom de l'entreprise")
"Nom de l'entreprise", adresse = _build_string_field("Adresse de l'entreprise")
validators=[DataRequired(message=CHAMP_REQUIS)], codepostal = _build_string_field("Code postal de l'entreprise")
) ville = _build_string_field("Ville de l'entreprise")
adresse = StringField( pays = _build_string_field("Pays de l'entreprise")
"Adresse de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
codepostal = StringField(
"Code postal de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
ville = StringField(
"Ville de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
pays = StringField(
"Pays de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
render_kw={"style": "margin-bottom: 50px;"},
)
nom_contact = StringField( nom_contact = _build_string_field("Nom du contact")
"Nom du contact", validators=[DataRequired(message=CHAMP_REQUIS)] prenom_contact = _build_string_field("Prénom du contact")
) telephone = _build_string_field("Téléphone du contact", required=False)
prenom_contact = StringField( mail = StringField(
"Prénom du contact",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
telephone = StringField("Téléphone du contact", validators=[Optional()])
mail = EmailField(
"Mail du contact", "Mail du contact",
validators=[Optional(), Email(message="Adresse e-mail invalide")], validators=[Optional(), Email(message="Adresse e-mail invalide")],
) )
poste = StringField("Poste du contact", validators=[Optional()]) poste = _build_string_field("Poste du contact", required=False)
service = StringField("Service du contact", validators=[Optional()]) service = _build_string_field("Service du contact", required=False)
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
def validate(self): def validate(self):
rv = FlaskForm.validate(self) validate = True
if not rv: if not FlaskForm.validate(self):
return False validate = False
if not self.telephone.data and not self.mail.data: if not self.telephone.data and not self.mail.data:
self.telephone.errors.append( self.telephone.errors.append(
"Saisir un moyen de contact (mail ou téléphone)" "Saisir un moyen de contact (mail ou téléphone)"
) )
self.mail.errors.append("Saisir un moyen de contact (mail ou téléphone)") self.mail.errors.append("Saisir un moyen de contact (mail ou téléphone)")
return False validate = False
return True return validate
def validate_siret(self, siret): def validate_siret(self, siret):
siret = siret.data.strip() siret = siret.data.strip()
@ -126,18 +116,13 @@ class EntrepriseCreationForm(FlaskForm):
class EntrepriseModificationForm(FlaskForm): class EntrepriseModificationForm(FlaskForm):
siret = StringField("SIRET", validators=[], render_kw={"disabled": ""}) siret = StringField("SIRET", render_kw={"disabled": ""})
nom = StringField( nom = _build_string_field("Nom de l'entreprise")
"Nom de l'entreprise", adresse = _build_string_field("Adresse")
validators=[DataRequired(message=CHAMP_REQUIS)], codepostal = _build_string_field("Code postal")
) ville = _build_string_field("Ville")
adresse = StringField("Adresse", validators=[DataRequired(message=CHAMP_REQUIS)]) pays = _build_string_field("Pays")
codepostal = StringField( submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
"Code postal", validators=[DataRequired(message=CHAMP_REQUIS)]
)
ville = StringField("Ville", validators=[DataRequired(message=CHAMP_REQUIS)])
pays = StringField("Pays", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"})
class MultiCheckboxField(SelectMultipleField): class MultiCheckboxField(SelectMultipleField):
@ -146,8 +131,7 @@ class MultiCheckboxField(SelectMultipleField):
class OffreCreationForm(FlaskForm): class OffreCreationForm(FlaskForm):
intitule = _build_string_field("Intitulé")
intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)])
description = TextAreaField( description = TextAreaField(
"Description", validators=[DataRequired(message=CHAMP_REQUIS)] "Description", validators=[DataRequired(message=CHAMP_REQUIS)]
) )
@ -156,15 +140,13 @@ class OffreCreationForm(FlaskForm):
choices=[("Stage"), ("Alternance")], choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)], validators=[DataRequired(message=CHAMP_REQUIS)],
) )
missions = TextAreaField( missions = _build_string_field("Missions")
"Missions", validators=[DataRequired(message=CHAMP_REQUIS)] duree = _build_string_field("Durée")
)
duree = StringField("Durée", validators=[DataRequired(message=CHAMP_REQUIS)])
depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
expiration_date = DateField( expiration_date = DateField(
"Date expiration", validators=[DataRequired(message=CHAMP_REQUIS)] "Date expiration", validators=[DataRequired(message=CHAMP_REQUIS)]
) )
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -175,7 +157,7 @@ class OffreCreationForm(FlaskForm):
class OffreModificationForm(FlaskForm): class OffreModificationForm(FlaskForm):
intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)]) intitule = _build_string_field("Intitulé")
description = TextAreaField( description = TextAreaField(
"Description", validators=[DataRequired(message=CHAMP_REQUIS)] "Description", validators=[DataRequired(message=CHAMP_REQUIS)]
) )
@ -184,15 +166,13 @@ class OffreModificationForm(FlaskForm):
choices=[("Stage"), ("Alternance")], choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)], validators=[DataRequired(message=CHAMP_REQUIS)],
) )
missions = TextAreaField( missions = _build_string_field("Missions")
"Missions", validators=[DataRequired(message=CHAMP_REQUIS)] duree = _build_string_field("Durée")
)
duree = StringField("Durée", validators=[DataRequired(message=CHAMP_REQUIS)])
depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
expiration_date = DateField( expiration_date = DateField(
"Date expiration", validators=[DataRequired(message=CHAMP_REQUIS)] "Date expiration", validators=[DataRequired(message=CHAMP_REQUIS)]
) )
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -204,30 +184,27 @@ class OffreModificationForm(FlaskForm):
class ContactCreationForm(FlaskForm): class ContactCreationForm(FlaskForm):
hidden_entreprise_id = HiddenField() hidden_entreprise_id = HiddenField()
nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)]) nom = _build_string_field("Nom")
prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)]) prenom = _build_string_field("Prénom")
telephone = StringField("Téléphone", validators=[Optional()]) telephone = _build_string_field("Téléphone", required=False)
mail = EmailField( mail = StringField(
"Mail", "Mail",
validators=[Optional(), Email(message="Adresse e-mail invalide")], validators=[Optional(), Email(message="Adresse e-mail invalide")],
) )
poste = StringField("Poste", validators=[Optional()]) poste = _build_string_field("Poste", required=False)
service = StringField("Service", validators=[Optional()]) service = _build_string_field("Service", required=False)
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
def validate(self): def validate(self):
rv = FlaskForm.validate(self) validate = True
if not rv: if not FlaskForm.validate(self):
return False validate = False
contact = EntrepriseContact.query.filter_by( contact = EntrepriseContact.query.filter_by(
entreprise_id=self.hidden_entreprise_id.data, entreprise_id=self.hidden_entreprise_id.data,
nom=self.nom.data, nom=self.nom.data,
prenom=self.prenom.data, prenom=self.prenom.data,
).first() ).first()
validate = True
if contact is not None: if contact is not None:
self.nom.errors.append("Ce contact existe déjà (même nom et prénom)") self.nom.errors.append("Ce contact existe déjà (même nom et prénom)")
self.prenom.errors.append("") self.prenom.errors.append("")
@ -246,21 +223,21 @@ class ContactCreationForm(FlaskForm):
class ContactModificationForm(FlaskForm): class ContactModificationForm(FlaskForm):
hidden_contact_id = HiddenField() hidden_contact_id = HiddenField()
hidden_entreprise_id = HiddenField() hidden_entreprise_id = HiddenField()
nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)]) nom = _build_string_field("Nom")
prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)]) prenom = _build_string_field("Prénom")
telephone = StringField("Téléphone", validators=[Optional()]) telephone = _build_string_field("Téléphone", required=False)
mail = EmailField( mail = StringField(
"Mail", "Mail",
validators=[Optional(), Email(message="Adresse e-mail invalide")], validators=[Optional(), Email(message="Adresse e-mail invalide")],
) )
poste = StringField("Poste", validators=[Optional()]) poste = _build_string_field("Poste", required=False)
service = StringField("Service", validators=[Optional()]) service = _build_string_field("Service", required=False)
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
def validate(self): def validate(self):
rv = FlaskForm.validate(self) validate = True
if not rv: if not FlaskForm.validate(self):
return False validate = False
contact = EntrepriseContact.query.filter( contact = EntrepriseContact.query.filter(
EntrepriseContact.id != self.hidden_contact_id.data, EntrepriseContact.id != self.hidden_contact_id.data,
@ -268,9 +245,6 @@ class ContactModificationForm(FlaskForm):
EntrepriseContact.nom == self.nom.data, EntrepriseContact.nom == self.nom.data,
EntrepriseContact.prenom == self.prenom.data, EntrepriseContact.prenom == self.prenom.data,
).first() ).first()
validate = True
if contact is not None: if contact is not None:
self.nom.errors.append("Ce contact existe déjà (même nom et prénom)") self.nom.errors.append("Ce contact existe déjà (même nom et prénom)")
self.prenom.errors.append("") self.prenom.errors.append("")
@ -287,10 +261,9 @@ class ContactModificationForm(FlaskForm):
class HistoriqueCreationForm(FlaskForm): class HistoriqueCreationForm(FlaskForm):
etudiant = StringField( etudiant = _build_string_field(
"Étudiant", "Étudiant",
validators=[DataRequired(message=CHAMP_REQUIS)], render_kw={"placeholder": "Tapez le nom de l'étudiant"},
render_kw={"placeholder": "Tapez le nom de l'étudiant puis selectionnez"},
) )
type_offre = SelectField( type_offre = SelectField(
"Type de l'offre", "Type de l'offre",
@ -301,18 +274,23 @@ class HistoriqueCreationForm(FlaskForm):
"Date début", validators=[DataRequired(message=CHAMP_REQUIS)] "Date début", validators=[DataRequired(message=CHAMP_REQUIS)]
) )
date_fin = DateField("Date fin", validators=[DataRequired(message=CHAMP_REQUIS)]) date_fin = DateField("Date fin", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
def validate(self): def validate(self):
rv = FlaskForm.validate(self) validate = True
if not rv: if not FlaskForm.validate(self):
return False validate = False
if self.date_debut.data > self.date_fin.data: if (
self.date_debut.data
and self.date_fin.data
and self.date_debut.data > self.date_fin.data
):
self.date_debut.errors.append("Les dates sont incompatibles") self.date_debut.errors.append("Les dates sont incompatibles")
self.date_fin.errors.append("Les dates sont incompatibles") self.date_fin.errors.append("Les dates sont incompatibles")
return False validate = False
return True
return validate
def validate_etudiant(self, etudiant): def validate_etudiant(self, etudiant):
etudiant_data = etudiant.data.upper().strip() etudiant_data = etudiant.data.upper().strip()
@ -327,11 +305,11 @@ class HistoriqueCreationForm(FlaskForm):
class EnvoiOffreForm(FlaskForm): class EnvoiOffreForm(FlaskForm):
responsable = StringField( responsable = _build_string_field(
"Responsable de formation", "Responsable de formation",
validators=[DataRequired(message=CHAMP_REQUIS)], render_kw={"placeholder": "Tapez le nom du responsable de formation"},
) )
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
def validate_responsable(self, responsable): def validate_responsable(self, responsable):
responsable_data = responsable.data.upper().strip() responsable_data = responsable.data.upper().strip()
@ -355,15 +333,15 @@ class AjoutFichierForm(FlaskForm):
FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"), FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"),
], ],
) )
submit = SubmitField("Ajouter", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Ajouter", render_kw=SUBMIT_MARGE)
class SuppressionConfirmationForm(FlaskForm): class SuppressionConfirmationForm(FlaskForm):
submit = SubmitField("Supprimer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Supprimer", render_kw=SUBMIT_MARGE)
class ValidationConfirmationForm(FlaskForm): class ValidationConfirmationForm(FlaskForm):
submit = SubmitField("Valider", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Valider", render_kw=SUBMIT_MARGE)
class ImportForm(FlaskForm): class ImportForm(FlaskForm):
@ -374,4 +352,4 @@ class ImportForm(FlaskForm):
FileAllowed(["xlsx"], "Fichier .xlsx uniquement"), FileAllowed(["xlsx"], "Fichier .xlsx uniquement"),
], ],
) )
submit = SubmitField("Importer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Importer", render_kw=SUBMIT_MARGE)

View File

@ -61,7 +61,7 @@ def index():
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all() logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
return render_template( return render_template(
"entreprises/entreprises.html", "entreprises/entreprises.html",
title=("Entreprises"), title="Entreprises",
entreprises=entreprises, entreprises=entreprises,
logs=logs, logs=logs,
) )
@ -79,7 +79,7 @@ def logs():
) )
return render_template( return render_template(
"entreprises/logs.html", "entreprises/logs.html",
title=("Logs"), title="Logs",
logs=logs, logs=logs,
) )
@ -93,7 +93,7 @@ def validation():
entreprises = Entreprise.query.filter_by(visible=False).all() entreprises = Entreprise.query.filter_by(visible=False).all()
return render_template( return render_template(
"entreprises/entreprises_validation.html", "entreprises/entreprises_validation.html",
title=("Validation entreprises"), title="Validation entreprises",
entreprises=entreprises, entreprises=entreprises,
) )
@ -114,7 +114,7 @@ def contacts():
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all() logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
return render_template( return render_template(
"entreprises/contacts.html", "entreprises/contacts.html",
title=("Contacts"), title="Contacts",
contacts=contacts, contacts=contacts,
logs=logs, logs=logs,
) )
@ -151,9 +151,7 @@ def fiche_entreprise(id):
offre_id=offre.id offre_id=offre.id
).all() ).all()
offres_with_files.append([offre, files, offre_depts]) offres_with_files.append([offre, files, offre_depts])
contacts = [] contacts = entreprise.contacts[:]
for contact in entreprise.contacts:
contacts.append(contact)
logs = ( logs = (
EntrepriseLog.query.order_by(EntrepriseLog.date.desc()) EntrepriseLog.query.order_by(EntrepriseLog.date.desc())
.filter_by(object=id) .filter_by(object=id)
@ -169,7 +167,7 @@ def fiche_entreprise(id):
) )
return render_template( return render_template(
"entreprises/fiche_entreprise.html", "entreprises/fiche_entreprise.html",
title=("Fiche entreprise"), title="Fiche entreprise",
entreprise=entreprise, entreprise=entreprise,
contacts=contacts, contacts=contacts,
offres=offres_with_files, offres=offres_with_files,
@ -193,7 +191,7 @@ def logs_entreprise(id):
) )
return render_template( return render_template(
"entreprises/logs_entreprise.html", "entreprises/logs_entreprise.html",
title=("Logs"), title="Logs",
logs=logs, logs=logs,
entreprise=entreprise, entreprise=entreprise,
) )
@ -209,7 +207,7 @@ def fiche_entreprise_validation(id):
contacts = entreprise.contacts contacts = entreprise.contacts
return render_template( return render_template(
"entreprises/fiche_entreprise_validation.html", "entreprises/fiche_entreprise_validation.html",
title=("Validation fiche entreprise"), title="Validation fiche entreprise",
entreprise=entreprise, entreprise=entreprise,
contacts=contacts, contacts=contacts,
) )
@ -246,7 +244,7 @@ def offres_recues():
offres_recues_with_files.append([offre[0], offre[1], files]) offres_recues_with_files.append([offre[0], offre[1], files])
return render_template( return render_template(
"entreprises/offres_recues.html", "entreprises/offres_recues.html",
title=("Offres reçues"), title="Offres reçues",
offres_recues=offres_recues_with_files, offres_recues=offres_recues_with_files,
) )
@ -282,7 +280,7 @@ def offres_expirees(id):
offres_expirees_with_files.append([offre, files, offre_depts]) offres_expirees_with_files.append([offre, files, offre_depts])
return render_template( return render_template(
"entreprises/offres_expirees.html", "entreprises/offres_expirees.html",
title=("Offres expirées"), title="Offres expirées",
entreprise=entreprise, entreprise=entreprise,
offres_expirees=offres_expirees_with_files, offres_expirees=offres_expirees_with_files,
) )
@ -335,7 +333,7 @@ def add_entreprise():
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template( return render_template(
"entreprises/ajout_entreprise.html", "entreprises/ajout_entreprise.html",
title=("Ajout entreprise avec contact"), title="Ajout entreprise avec contact",
form=form, form=form,
) )
@ -402,7 +400,7 @@ def edit_entreprise(id):
form.pays.data = entreprise.pays form.pays.data = entreprise.pays
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Modification entreprise"), title="Modification entreprise",
form=form, form=form,
) )
@ -436,7 +434,7 @@ def delete_entreprise(id):
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template( return render_template(
"entreprises/delete_confirmation.html", "entreprises/delete_confirmation.html",
title=("Supression entreprise"), title="Supression entreprise",
form=form, form=form,
) )
@ -462,7 +460,7 @@ def validate_entreprise(id):
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template( return render_template(
"entreprises/validate_confirmation.html", "entreprises/validate_confirmation.html",
title=("Validation entreprise"), title="Validation entreprise",
form=form, form=form,
) )
@ -505,7 +503,7 @@ def add_offre(id):
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Ajout offre"), title="Ajout offre",
form=form, form=form,
) )
@ -560,7 +558,7 @@ def edit_offre(id):
form.depts.data = offre_depts_list form.depts.data = offre_depts_list
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Modification offre"), title="Modification offre",
form=form, form=form,
) )
@ -595,7 +593,7 @@ def delete_offre(id):
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id))
return render_template( return render_template(
"entreprises/delete_confirmation.html", "entreprises/delete_confirmation.html",
title=("Supression offre"), title="Supression offre",
form=form, form=form,
) )
@ -630,7 +628,7 @@ def add_contact(id):
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Ajout contact"), title="Ajout contact",
form=form, form=form,
) )
@ -673,7 +671,7 @@ def edit_contact(id):
form.service.data = contact.service form.service.data = contact.service
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Modification contact"), title="Modification contact",
form=form, form=form,
) )
@ -685,17 +683,18 @@ def delete_contact(id):
Permet de supprimer un contact Permet de supprimer un contact
""" """
contact = EntrepriseContact.query.filter_by(id=id).first_or_404() contact = EntrepriseContact.query.filter_by(id=id).first_or_404()
entreprise_id = contact.entreprise.id
form = SuppressionConfirmationForm() form = SuppressionConfirmationForm()
if form.validate_on_submit(): if form.validate_on_submit():
contact_count = EntrepriseContact.query.filter_by( contact_count = EntrepriseContact.query.filter_by(
entreprise_id=contact.entreprise.id entreprise_id=contact.entreprise_id
).count() ).count()
if contact_count == 1: if contact_count == 1:
flash( flash(
"Le contact n'a pas été supprimé de la fiche entreprise. (1 contact minimum)" "Le contact n'a pas été supprimé de la fiche entreprise. (1 contact minimum)"
) )
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(
url_for("entreprises.fiche_entreprise", id=contact.entreprise_id)
)
else: else:
db.session.delete(contact) db.session.delete(contact)
log = EntrepriseLog( log = EntrepriseLog(
@ -706,10 +705,12 @@ def delete_contact(id):
db.session.add(log) db.session.add(log)
db.session.commit() db.session.commit()
flash("Le contact a été supprimé de la fiche entreprise.") flash("Le contact a été supprimé de la fiche entreprise.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(
url_for("entreprises.fiche_entreprise", id=contact.entreprise_id)
)
return render_template( return render_template(
"entreprises/delete_confirmation.html", "entreprises/delete_confirmation.html",
title=("Supression contact"), title="Supression contact",
form=form, form=form,
) )
@ -752,7 +753,7 @@ def add_historique(id):
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template( return render_template(
"entreprises/ajout_historique.html", "entreprises/ajout_historique.html",
title=("Ajout historique"), title="Ajout historique",
form=form, form=form,
) )
@ -776,7 +777,9 @@ def envoyer_offre(id):
.first() .first()
) )
envoi_offre = EntrepriseEnvoiOffre( envoi_offre = EntrepriseEnvoiOffre(
sender_id=current_user.id, receiver_id=responsable.id, offre_id=offre.id sender_id=current_user.id,
receiver_id=responsable.id,
offre_id=offre.id,
) )
db.session.add(envoi_offre) db.session.add(envoi_offre)
db.session.commit() db.session.commit()
@ -784,7 +787,7 @@ def envoyer_offre(id):
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id))
return render_template( return render_template(
"entreprises/envoi_offre_form.html", "entreprises/envoi_offre_form.html",
title=("Envoyer une offre"), title="Envoyer une offre",
form=form, form=form,
) )
@ -904,10 +907,10 @@ def import_entreprises():
""" """
form = ImportForm() form = ImportForm()
if form.validate_on_submit(): if form.validate_on_submit():
path = os.path.join(Config.SCODOC_VAR_DIR, "tmp")
file = form.fichier.data file = form.fichier.data
filename = secure_filename(file.filename) file_path = os.path.join(
file_path = os.path.join(path, filename) Config.SCODOC_VAR_DIR, "tmp", secure_filename(file.filename)
)
file.save(file_path) file.save(file_path)
data = sco_excel.excel_file_to_list(file_path) data = sco_excel.excel_file_to_list(file_path)
os.remove(file_path) os.remove(file_path)
@ -919,7 +922,7 @@ def import_entreprises():
flash("Veuillez utilisez la feuille excel à remplir") flash("Veuillez utilisez la feuille excel à remplir")
return render_template( return render_template(
"entreprises/import_entreprises.html", "entreprises/import_entreprises.html",
title=("Importation entreprises"), title="Importation entreprises",
form=form, form=form,
) )
for entreprise_data in data[1][1:]: for entreprise_data in data[1][1:]:
@ -944,7 +947,7 @@ def import_entreprises():
flash(f"Erreur lors de l'importation (ligne {ligne})") flash(f"Erreur lors de l'importation (ligne {ligne})")
return render_template( return render_template(
"entreprises/import_entreprises.html", "entreprises/import_entreprises.html",
title=("Importation entreprises"), title="Importation entreprises",
form=form, form=form,
) )
@ -955,7 +958,7 @@ def import_entreprises():
flash(f"Importation réussie de {len(entreprises_import)} entreprise(s)") flash(f"Importation réussie de {len(entreprises_import)} entreprise(s)")
return render_template( return render_template(
"entreprises/import_entreprises.html", "entreprises/import_entreprises.html",
title=("Importation entreprises"), title="Importation entreprises",
form=form, form=form,
entreprises_import=entreprises_import, entreprises_import=entreprises_import,
) )
@ -964,7 +967,7 @@ def import_entreprises():
return render_template( return render_template(
"entreprises/import_entreprises.html", "entreprises/import_entreprises.html",
title=("Importation entreprises"), title="Importation entreprises",
form=form, form=form,
) )
@ -1028,10 +1031,12 @@ def verif_contact_data(contact_data):
if contact_data[0] == "" or contact_data[1] == "" or contact_data[6] == "": if contact_data[0] == "" or contact_data[1] == "" or contact_data[6] == "":
return False return False
# entreprise_id existant
entreprise = Entreprise.query.filter_by(id=contact_data[6]).first() entreprise = Entreprise.query.filter_by(id=contact_data[6]).first()
if entreprise is None: if entreprise is None:
return False return False
# contact possède le meme nom et prénom dans la meme entreprise
contact = EntrepriseContact.query.filter_by( contact = EntrepriseContact.query.filter_by(
nom=contact_data[0], prenom=contact_data[1], entreprise_id=contact_data[6] nom=contact_data[0], prenom=contact_data[1], entreprise_id=contact_data[6]
).first() ).first()
@ -1053,10 +1058,10 @@ def import_contacts():
""" """
form = ImportForm() form = ImportForm()
if form.validate_on_submit(): if form.validate_on_submit():
path = os.path.join(Config.SCODOC_VAR_DIR, "tmp")
file = form.fichier.data file = form.fichier.data
filename = secure_filename(file.filename) file_path = os.path.join(
file_path = os.path.join(path, filename) Config.SCODOC_VAR_DIR, "tmp", secure_filename(file.filename)
)
file.save(file_path) file.save(file_path)
data = sco_excel.excel_file_to_list(file_path) data = sco_excel.excel_file_to_list(file_path)
os.remove(file_path) os.remove(file_path)
@ -1076,7 +1081,7 @@ def import_contacts():
flash("Veuillez utilisez la feuille excel à remplir") flash("Veuillez utilisez la feuille excel à remplir")
return render_template( return render_template(
"entreprises/import_contacts.html", "entreprises/import_contacts.html",
title=("Importation contacts"), title="Importation contacts",
form=form, form=form,
) )
for contact_data in data[1][1:]: for contact_data in data[1][1:]:
@ -1101,7 +1106,7 @@ def import_contacts():
flash(f"Erreur lors de l'importation (ligne {ligne})") flash(f"Erreur lors de l'importation (ligne {ligne})")
return render_template( return render_template(
"entreprises/import_contacts.html", "entreprises/import_contacts.html",
title=("Importation contacts"), title="Importation contacts",
form=form, form=form,
) )
@ -1112,7 +1117,7 @@ def import_contacts():
flash(f"Importation réussie de {len(contacts_import)} contact(s)") flash(f"Importation réussie de {len(contacts_import)} contact(s)")
return render_template( return render_template(
"entreprises/import_contacts.html", "entreprises/import_contacts.html",
title=("Importation Contacts"), title="Importation Contacts",
form=form, form=form,
contacts_import=contacts_import, contacts_import=contacts_import,
) )
@ -1120,7 +1125,7 @@ def import_contacts():
flash('Feuille "Contacts" vide') flash('Feuille "Contacts" vide')
return render_template( return render_template(
"entreprises/import_contacts.html", "entreprises/import_contacts.html",
title=("Importation contacts"), title="Importation contacts",
form=form, form=form,
) )
@ -1183,7 +1188,7 @@ def add_offre_file(offre_id):
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id))
return render_template( return render_template(
"entreprises/form.html", "entreprises/form.html",
title=("Ajout fichier à une offre"), title="Ajout fichier à une offre",
form=form, form=form,
) )
@ -1212,6 +1217,6 @@ def delete_offre_file(offre_id, filedir):
) )
return render_template( return render_template(
"entreprises/delete_confirmation.html", "entreprises/delete_confirmation.html",
title=("Suppression fichier d'une offre"), title="Suppression fichier d'une offre",
form=form, form=form,
) )

View File

@ -51,5 +51,12 @@
document.getElementById("pays").value = '' document.getElementById("pays").value = ''
} }
} }
{# ajout margin-bottom sur le champ pays #}
var champ_pays = document.getElementById("pays")
if (champ_pays !== null) {
var closest_form_group = champ_pays.closest(".form-group")
closest_form_group.style.marginBottom = "50px"
}
</script> </script>
{% endblock %} {% endblock %}

View File

@ -16,9 +16,11 @@
</div> </div>
<script> <script>
<!-- pour le formulaire ajout offre --> {# pour les formulaires offre #}
var element = document.getElementById("depts"); var champ_depts = document.getElementById("depts")
var closest_form_control = element.closest(".form-control") if (champ_depts !== null) {
closest_form_control.classList.remove("form-control") var closest_form_control = champ_depts.closest(".form-control")
closest_form_control.classList.remove("form-control")
}
</script> </script>
{% endblock %} {% endblock %}