Regex type de référentiel à l'import

This commit is contained in:
Éric Li 2021-05-06 12:08:44 +02:00
parent 18478d950b
commit d40acb8c65

View File

@ -1,10 +1,11 @@
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed
from wtforms import StringField, SubmitField, FileField, TextAreaField, RadioField
from wtforms.validators import DataRequired, NumberRange, Length, Regexp
from wtforms.validators import DataRequired, Regexp
import yaml
import os
import re
REPERTOIRE_YAML = "./export/"
@ -18,24 +19,24 @@ class Form(FlaskForm):
importer = SubmitField("Importer")
class PNForm(Form):
file_length = len("PN0")
regex = "^PN\d$"
code = StringField("Code", validators=[DataRequired(), Length(3,3)])
code = StringField("Code", validators=[DataRequired(), Regexp("^PN\d$", message="Le code doit être de la forme PN0.")])
nom = StringField("Nom", validators=[DataRequired()] )
diminutif = StringField("Diminutif", validators=[DataRequired()] )
description = TextAreaField("Description", validators=[DataRequired()] )
type = RadioField("Type", choices=[1,2,3], validators=[DataRequired()])
class ACForm(Form):
file_length = len("AC0000.yml")
regex = "^AC\d{4}$"
code = StringField("Code", validators=[DataRequired(), Length(6,6)])
code = StringField("Code", validators=[DataRequired(), Regexp("^AC\d{4}$", message="Le code doit être de la forme AC0000.")])
class SAEForm(Form):
file_length = len("SAE00.yml")
regex = "^SAE\d{2}$"
code = StringField("Code", validators=[DataRequired(), Length(5,5)])
code = StringField("Code", validators=[DataRequired(), Regexp("^SAE\d{2}$", message="Le code doit être de la forme SAE00.")])
titre = StringField("Titre", validators=[DataRequired()] )
semestre = StringField("Semestre", validators=[DataRequired()] )
heures_encadrees = StringField("Heures encadrées", validators=[DataRequired()] )
@ -50,9 +51,9 @@ class SAEForm(Form):
class RessourceForm(Form):
file_length = len("R000.yml")
regex = "^R\{3}$"
code = StringField("Code", validators=[DataRequired(), Length(4,4)])
code = StringField("Code", validators=[DataRequired(), Regexp("^R\{3}$", message="Le code doit être de la forme R000.")])
nom = StringField("Nom", validators=[DataRequired()] )
semestre = StringField("Semestre", validators=[DataRequired()] )
heures_formation = StringField("Heures formation", validators=[DataRequired()] )
@ -66,9 +67,9 @@ class RessourceForm(Form):
motscles = StringField("Mots clés", validators=[DataRequired()] )
class CompetenceForm(Form):
file_length = len("RT0.yml")
regex = "^RT\d$"
code = StringField("Code", validators=[DataRequired(), Length(3,3)])
code = StringField("Code", validators=[DataRequired(), Regexp("^RT\d$", message="Le code doit être de la forme RT0.")])
nom = StringField("Nom", validators=[DataRequired()] )
diminutif = StringField("Diminutif", validators=[DataRequired()] )
description = TextAreaField("Description", validators=[DataRequired()] )
@ -78,7 +79,14 @@ class CompetenceForm(Form):
def form_import(form):
""" Si import a été appuyé et qu'il n'y a pas d'erreur d'import => importe le fichier yaml"""
if form.importer.data and len(form.fichier.errors) == 0 and len(form.fichier.data.filename) == form.file_length:
# Bouton import appuyé et fichier yaml selectionné
if form.importer.data and len(form.fichier.errors) == 0:
if form.fichier.data.filename == "":
form.fichier.errors.append("Aucun fichier selectioné.")
return form
if re.match(form.regex, form.fichier.data.filename[:-4]) == None:
form.fichier.errors.append("Mauvais type de référentiel!")
return form
fichier_Yaml = yaml.safe_load(form.fichier.data.read())
for categorie, valeur in fichier_Yaml.items():
form[categorie].data = valeur