ScoDoc/app/entreprises/forms.py

414 lines
15 KiB
Python
Raw Normal View History

2022-01-01 18:02:23 +01:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2022 Emmanuel Viennet. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
##############################################################################
import re
import requests
2021-12-27 11:48:58 +01:00
from flask_wtf import FlaskForm
2022-01-01 18:02:23 +01:00
from flask_wtf.file import FileField, FileAllowed, FileRequired
2021-12-23 19:28:25 +01:00
from markupsafe import Markup
2022-01-01 18:02:23 +01:00
from sqlalchemy import text
from wtforms import (
StringField,
SubmitField,
TextAreaField,
SelectField,
HiddenField,
SelectMultipleField,
2022-02-11 19:18:01 +01:00
DateField,
2022-02-22 21:52:32 +01:00
BooleanField,
)
from wtforms.validators import ValidationError, DataRequired, Email, Optional
2022-02-04 17:12:56 +01:00
from wtforms.widgets import ListWidget, CheckboxInput
2022-01-01 18:02:23 +01:00
from app.entreprises.models import Entreprise, EntrepriseContact, EntreprisePreferences
from app.models import Identite, Departement
2021-12-23 19:28:25 +01:00
from app.auth.models import User
CHAMP_REQUIS = "Ce champ est requis"
2022-02-11 19:18:01 +01:00
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)
2021-12-23 19:28:25 +01:00
class EntrepriseCreationForm(FlaskForm):
2022-02-11 19:18:01 +01:00
siret = _build_string_field(
2022-02-28 18:57:05 +01:00
"SIRET (*)",
render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"},
)
2022-02-28 18:57:05 +01:00
nom_entreprise = _build_string_field("Nom de l'entreprise (*)")
adresse = _build_string_field("Adresse de l'entreprise (*)")
codepostal = _build_string_field("Code postal de l'entreprise (*)")
ville = _build_string_field("Ville de l'entreprise (*)")
pays = _build_string_field("Pays de l'entreprise", required=False)
nom_contact = _build_string_field("Nom du contact (*)")
prenom_contact = _build_string_field("Prénom du contact (*)")
telephone = _build_string_field("Téléphone du contact (*)", required=False)
2022-02-11 19:18:01 +01:00
mail = StringField(
2022-02-28 18:57:05 +01:00
"Mail du contact (*)",
2022-02-07 17:06:00 +01:00
validators=[Optional(), Email(message="Adresse e-mail invalide")],
)
2022-02-11 19:18:01 +01:00
poste = _build_string_field("Poste du contact", required=False)
service = _build_string_field("Service du contact", required=False)
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
2022-02-07 17:06:00 +01:00
def validate(self):
2022-02-11 19:18:01 +01:00
validate = True
if not FlaskForm.validate(self):
validate = False
2022-02-07 17:06:00 +01:00
if not self.telephone.data and not self.mail.data:
self.telephone.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)")
2022-02-11 19:18:01 +01:00
validate = False
2022-02-07 17:06:00 +01:00
2022-02-11 19:18:01 +01:00
return validate
2022-02-07 17:06:00 +01:00
2021-12-23 19:28:25 +01:00
def validate_siret(self, siret):
if EntreprisePreferences.get_check_siret():
siret = siret.data.strip()
if re.match("^\d{14}$", siret) is None:
raise ValidationError("Format incorrect")
try:
req = requests.get(
f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}"
)
except requests.ConnectionError:
print("no internet")
if req.status_code != 200:
raise ValidationError("SIRET inexistant")
entreprise = Entreprise.query.filter_by(siret=siret).first()
if entreprise is not None:
lien = f'<a href="/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}">ici</a>'
raise ValidationError(
Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}")
)
2021-12-23 19:28:25 +01:00
class EntrepriseModificationForm(FlaskForm):
2022-02-28 18:57:05 +01:00
hidden_entreprise_siret = HiddenField()
siret = StringField("SIRET (*)")
nom = _build_string_field("Nom de l'entreprise (*)")
adresse = _build_string_field("Adresse (*)")
codepostal = _build_string_field("Code postal (*)")
ville = _build_string_field("Ville (*)")
pays = _build_string_field("Pays", required=False)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
2022-02-28 18:57:05 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.siret.render_kw = {
"disabled": "",
"value": self.hidden_entreprise_siret.data,
}
2022-02-04 17:12:56 +01:00
class MultiCheckboxField(SelectMultipleField):
widget = ListWidget(prefix_label=False)
option_widget = CheckboxInput()
2021-12-23 19:28:25 +01:00
class OffreCreationForm(FlaskForm):
2022-03-04 17:10:07 +01:00
hidden_entreprise_id = HiddenField()
2022-02-28 18:57:05 +01:00
intitule = _build_string_field("Intitulé (*)")
description = TextAreaField(
2022-02-28 18:57:05 +01:00
"Description (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
)
type_offre = SelectField(
2022-02-28 18:57:05 +01:00
"Type de l'offre (*)",
choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)],
)
2022-02-25 09:45:14 +01:00
missions = TextAreaField(
2022-02-28 18:57:05 +01:00
"Missions (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
2022-02-25 09:45:14 +01:00
)
2022-02-28 18:57:05 +01:00
duree = _build_string_field("Durée (*)")
2022-02-04 17:12:56 +01:00
depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
2022-03-01 18:45:04 +01:00
expiration_date = DateField("Date expiration", validators=[Optional()])
2022-03-04 17:10:07 +01:00
contact = SelectField("Contact à contacté (*)", validators=[Optional()])
2022-02-11 19:18:01 +01:00
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2022-03-04 17:10:07 +01:00
self.contact.choices = [
(contact.id, f"{contact.nom} {contact.prenom}")
for contact in EntrepriseContact.query.filter_by(
entreprise_id=self.hidden_entreprise_id.data
)
]
self.depts.choices = [
(dept.id, dept.acronym) for dept in Departement.query.all()
]
2021-12-23 19:28:25 +01:00
class OffreModificationForm(FlaskForm):
2022-03-04 17:10:07 +01:00
hidden_entreprise_id = HiddenField()
2022-02-28 18:57:05 +01:00
intitule = _build_string_field("Intitulé (*)")
description = TextAreaField(
2022-02-28 18:57:05 +01:00
"Description (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
)
type_offre = SelectField(
2022-02-28 18:57:05 +01:00
"Type de l'offre (*)",
choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)],
)
2022-02-25 09:45:14 +01:00
missions = TextAreaField(
2022-02-28 18:57:05 +01:00
"Missions (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
2022-02-25 09:45:14 +01:00
)
2022-02-28 18:57:05 +01:00
duree = _build_string_field("Durée (*)")
2022-02-04 17:12:56 +01:00
depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
2022-03-01 18:45:04 +01:00
expiration_date = DateField("Date expiration", validators=[Optional()])
2022-03-04 17:10:07 +01:00
contact = SelectField("Contact à contacté (*)", validators=[Optional()])
2022-02-11 19:18:01 +01:00
submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
2022-02-04 17:12:56 +01:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2022-03-04 17:10:07 +01:00
self.contact.choices = [
(contact.id, f"{contact.nom} {contact.prenom}")
for contact in EntrepriseContact.query.filter_by(
entreprise_id=self.hidden_entreprise_id.data
)
]
2022-02-04 17:12:56 +01:00
self.depts.choices = [
(dept.id, dept.acronym) for dept in Departement.query.all()
]
2021-12-23 19:28:25 +01:00
class ContactCreationForm(FlaskForm):
hidden_entreprise_id = HiddenField()
2022-02-28 18:57:05 +01:00
nom = _build_string_field("Nom (*)")
prenom = _build_string_field("Prénom (*)")
telephone = _build_string_field("Téléphone (*)", required=False)
2022-02-11 19:18:01 +01:00
mail = StringField(
2022-02-28 18:57:05 +01:00
"Mail (*)",
2022-02-07 17:06:00 +01:00
validators=[Optional(), Email(message="Adresse e-mail invalide")],
)
2022-02-11 19:18:01 +01:00
poste = _build_string_field("Poste", required=False)
service = _build_string_field("Service", required=False)
2022-03-04 17:10:07 +01:00
# depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
2022-03-04 17:10:07 +01:00
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.depts.choices = [
# (dept.id, dept.acronym) for dept in Departement.query.all()
# ]
def validate(self):
2022-02-11 19:18:01 +01:00
validate = True
if not FlaskForm.validate(self):
validate = 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("")
2022-02-07 20:43:59 +01:00
validate = False
2022-02-07 17:06:00 +01:00
2022-02-07 20:43:59 +01:00
if not self.telephone.data and not self.mail.data:
self.telephone.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)")
validate = False
2022-02-07 20:43:59 +01:00
return validate
2021-12-23 19:28:25 +01:00
class ContactModificationForm(FlaskForm):
2022-02-07 21:40:58 +01:00
hidden_contact_id = HiddenField()
hidden_entreprise_id = HiddenField()
2022-02-28 18:57:05 +01:00
nom = _build_string_field("Nom (*)")
prenom = _build_string_field("Prénom (*)")
telephone = _build_string_field("Téléphone (*)", required=False)
2022-02-11 19:18:01 +01:00
mail = StringField(
2022-02-28 18:57:05 +01:00
"Mail (*)",
2022-02-07 17:06:00 +01:00
validators=[Optional(), Email(message="Adresse e-mail invalide")],
)
2022-02-11 19:18:01 +01:00
poste = _build_string_field("Poste", required=False)
service = _build_string_field("Service", required=False)
2022-03-04 17:10:07 +01:00
# depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
2022-03-04 17:10:07 +01:00
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self.depts.choices = [
# (dept.id, dept.acronym) for dept in Departement.query.all()
# ]
2022-02-07 17:06:00 +01:00
def validate(self):
2022-02-11 19:18:01 +01:00
validate = True
if not FlaskForm.validate(self):
validate = False
2022-02-07 17:06:00 +01:00
2022-02-07 21:40:58 +01:00
contact = EntrepriseContact.query.filter(
EntrepriseContact.id != self.hidden_contact_id.data,
EntrepriseContact.entreprise_id == self.hidden_entreprise_id.data,
EntrepriseContact.nom == self.nom.data,
EntrepriseContact.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("")
validate = False
2022-02-07 17:06:00 +01:00
if not self.telephone.data and not self.mail.data:
self.telephone.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)")
2022-02-07 21:40:58 +01:00
validate = False
2022-02-07 17:06:00 +01:00
2022-02-07 21:40:58 +01:00
return validate
2022-02-07 17:06:00 +01:00
2021-12-23 19:28:25 +01:00
class HistoriqueCreationForm(FlaskForm):
2022-02-11 19:18:01 +01:00
etudiant = _build_string_field(
2022-02-28 18:57:05 +01:00
"Étudiant (*)",
2022-02-11 19:18:01 +01:00
render_kw={"placeholder": "Tapez le nom de l'étudiant"},
)
type_offre = SelectField(
2022-02-28 18:57:05 +01:00
"Type de l'offre (*)",
choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)],
)
date_debut = DateField(
2022-02-28 18:57:05 +01:00
"Date début (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
)
date_fin = DateField(
"Date fin (*)", validators=[DataRequired(message=CHAMP_REQUIS)]
)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
def validate(self):
2022-02-11 19:18:01 +01:00
validate = True
if not FlaskForm.validate(self):
validate = False
2021-12-23 19:28:25 +01:00
2022-02-11 19:18:01 +01:00
if (
self.date_debut.data
and self.date_fin.data
and self.date_debut.data > self.date_fin.data
):
2021-12-23 19:28:25 +01:00
self.date_debut.errors.append("Les dates sont incompatibles")
self.date_fin.errors.append("Les dates sont incompatibles")
2022-02-11 19:18:01 +01:00
validate = False
return validate
2021-12-23 19:28:25 +01:00
def validate_etudiant(self, etudiant):
etudiant_data = etudiant.data.upper().strip()
stm = text(
"SELECT id, CONCAT(nom, ' ', prenom) as nom_prenom FROM Identite WHERE CONCAT(nom, ' ', prenom)=:nom_prenom"
)
etudiant = (
Identite.query.from_statement(stm).params(nom_prenom=etudiant_data).first()
)
2021-12-23 19:28:25 +01:00
if etudiant is None:
raise ValidationError("Champ incorrect (selectionnez dans la liste)")
2021-12-23 19:28:25 +01:00
class EnvoiOffreForm(FlaskForm):
2022-02-11 19:18:01 +01:00
responsable = _build_string_field(
2022-02-28 18:57:05 +01:00
"Responsable de formation (*)",
2022-02-11 19:18:01 +01:00
render_kw={"placeholder": "Tapez le nom du responsable de formation"},
)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
def validate_responsable(self, responsable):
responsable_data = responsable.data.upper().strip()
stm = text(
"SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data"
)
responsable = (
User.query.from_statement(stm)
.params(responsable_data=responsable_data)
.first()
)
2021-12-23 19:28:25 +01:00
if responsable is None:
raise ValidationError("Champ incorrect (selectionnez dans la liste)")
class AjoutFichierForm(FlaskForm):
fichier = FileField(
2022-02-28 18:57:05 +01:00
"Fichier (*)",
validators=[
FileRequired(message=CHAMP_REQUIS),
FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"),
],
)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Ajouter", render_kw=SUBMIT_MARGE)
2021-12-23 19:28:25 +01:00
class SuppressionConfirmationForm(FlaskForm):
2022-02-11 19:18:01 +01:00
submit = SubmitField("Supprimer", render_kw=SUBMIT_MARGE)
2022-02-01 18:35:48 +01:00
class ValidationConfirmationForm(FlaskForm):
2022-02-11 19:18:01 +01:00
submit = SubmitField("Valider", render_kw=SUBMIT_MARGE)
2022-02-07 18:39:32 +01:00
2022-02-10 21:17:22 +01:00
class ImportForm(FlaskForm):
2022-02-07 18:39:32 +01:00
fichier = FileField(
2022-02-28 18:57:05 +01:00
"Fichier (*)",
2022-02-07 18:39:32 +01:00
validators=[
FileRequired(message=CHAMP_REQUIS),
FileAllowed(["xlsx"], "Fichier .xlsx uniquement"),
],
)
2022-02-11 19:18:01 +01:00
submit = SubmitField("Importer", render_kw=SUBMIT_MARGE)
2022-02-22 21:52:32 +01:00
class PreferencesForm(FlaskForm):
mail_entreprise = StringField(
"Mail notifications",
validators=[Optional(), Email(message="Adresse e-mail invalide")],
)
check_siret = BooleanField("Vérification SIRET")
submit = SubmitField("Valider", render_kw=SUBMIT_MARGE)