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 import yaml import os REPERTOIRE_YAML = "./export/" if not os.path.exists(REPERTOIRE_YAML): os.makedirs(REPERTOIRE_YAML) class Form(FlaskForm): exporter = SubmitField("Exporter") fichier = FileField("Choisir fichier", validators=[FileAllowed(["yml"], "Fichier Yaml seulement!")]) importer = SubmitField("Importer") class PNForm(Form): file_length = len("PN0") code = StringField("Code", validators=[DataRequired(), Length(3,3)]) 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") code = StringField("Code", validators=[DataRequired(), Length(6,6)]) class SAEForm(Form): file_length = len("SAE00.yml") code = StringField("Code", validators=[DataRequired(), Length(5,5)]) titre = StringField("Titre", validators=[DataRequired()] ) semestre = StringField("Semestre", validators=[DataRequired()] ) heures_encadrees = StringField("Heures encadrées", validators=[DataRequired()] ) heures_tp = StringField("Heures TP", validators=[DataRequired()] ) projet = StringField("Projet", validators=[DataRequired()] ) description = TextAreaField("Description", validators=[DataRequired()] ) coef = StringField("Coef.", validators=[DataRequired()] ) acs = StringField("ACs", validators=[DataRequired()] ) ressources = StringField("Ressources", validators=[DataRequired()] ) livrables = TextAreaField("Livrables", validators=[DataRequired()] ) motscles = StringField("Mots clés", validators=[DataRequired()] ) class RessourceForm(Form): file_length = len("R000.yml") code = StringField("Code", validators=[DataRequired(), Length(4,4)]) nom = StringField("Nom", validators=[DataRequired()] ) semestre = StringField("Semestre", validators=[DataRequired()] ) heures_formation = StringField("Heures formation", validators=[DataRequired()] ) heures_tp = StringField("Heures TP", validators=[DataRequired()] ) coef = StringField("Coef.", validators=[DataRequired()] ) acs = StringField("ACs", validators=[DataRequired()] ) saes = StringField("SAEs", validators=[DataRequired()] ) prerequis = StringField("Prérequis", validators=[DataRequired()] ) contexte = TextAreaField("Contexte", validators=[DataRequired()] ) contenu = TextAreaField("Contenu", validators=[DataRequired()] ) motscles = StringField("Mots clés", validators=[DataRequired()] ) class CompetenceForm(Form): file_length = len("RT0.yml") code = StringField("Code", validators=[DataRequired(), Length(3,3)]) nom = StringField("Nom", validators=[DataRequired()] ) diminutif = StringField("Diminutif", validators=[DataRequired()] ) description = TextAreaField("Description", validators=[DataRequired()] ) composantes = TextAreaField("Composantes", validators=[DataRequired()] ) situations = TextAreaField("Situations", validators=[DataRequired()] ) niveaux = TextAreaField("Niveaux", validators=[DataRequired()] ) 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: fichier_Yaml = yaml.safe_load(form.fichier.data.read()) for categorie, valeur in fichier_Yaml.items(): form[categorie].data = valeur form.validate_on_submit() # Réinitialise les messages d'erreur return form def form_export(form): """ Si le formulaire est valide => exporte dans un fichier yaml avec les informations du formulaire """ output = {} for categorie, valeur in list(form.data.items())[3:-1]: output[categorie] = valeur fichier = REPERTOIRE_YAML + form.code.data + ".yml" with open(fichier, "w", encoding="utf8") as fid: fid.write(yaml.dump(output))