Compare commits
2 Commits
d6b9a7f6bd
...
42334c7946
Author | SHA1 | Date |
---|---|---|
Éric Li | 42334c7946 | |
Éric Li | 18269242b8 |
25
app/forms.py
25
app/forms.py
|
@ -3,6 +3,7 @@ from flask_wtf.file import FileAllowed
|
|||
from wtforms import StringField, SubmitField, FileField, TextAreaField, RadioField
|
||||
from wtforms.validators import DataRequired, Regexp, Optional
|
||||
|
||||
import app.models as models
|
||||
import yaml
|
||||
import os
|
||||
import re
|
||||
|
@ -28,7 +29,7 @@ class PNForm(Form):
|
|||
nom = StringField("Nom", validators=[DataRequired()] )
|
||||
diminutif = StringField("Diminutif", validators=[DataRequired()] )
|
||||
description = TextAreaField("Description", validators=[DataRequired()] )
|
||||
type = RadioField("Type", choices=[1,2,3], validators=[DataRequired()])
|
||||
type = RadioField("Type", choices=["1","2","3"], validators=[DataRequired()])
|
||||
|
||||
class ACForm(Form):
|
||||
regex = "^AC\d{4}$"
|
||||
|
@ -81,7 +82,7 @@ class CompetenceForm(Form):
|
|||
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"""
|
||||
""" Si le bouton import a été appuyé et qu'il n'y a pas d'erreur d'import => importe le fichier yaml"""
|
||||
# Bouton import appuyé et fichier yaml selectionné
|
||||
if form.importer.data and len(form.fichier.errors) == 0:
|
||||
if form.fichier.data.filename == "":
|
||||
|
@ -92,17 +93,29 @@ def form_import(form):
|
|||
return form
|
||||
fichier_Yaml = yaml.safe_load(form.fichier.data.read())
|
||||
for categorie, valeur in fichier_Yaml.items():
|
||||
form[categorie].data = valeur
|
||||
form[categorie].process_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())[6:-1]:
|
||||
output[categorie] = valeur
|
||||
|
||||
fichier = REPERTOIRE_YAML + form.code.data + ".yml"
|
||||
with open(fichier, "w", encoding="utf8") as fid:
|
||||
fid.write(yaml.dump(output))
|
||||
fid.write(yaml.dump(output))
|
||||
|
||||
def form_charger(form):
|
||||
""" Si le bouton charger est appuyé et qu'un référentiel du BDD a été selectionné => remplie le formulaire avec ses informations """
|
||||
if form.charger.data:
|
||||
if form.referentiel.data == None:
|
||||
form.referentiel.errors.append("Aucun référentiel n'a été selectionné!")
|
||||
else:
|
||||
temp = form.referentiel.data[1:-1].split()
|
||||
model = getattr(models, temp[0])
|
||||
referentiel = model.query.filter_by(code=temp[1]).first()
|
||||
for categorie in list(referentiel.__dict__)[1:]:
|
||||
form[categorie].process_data(referentiel.__dict__[categorie])
|
||||
form.validate_on_submit()
|
||||
return form
|
|
@ -16,7 +16,8 @@ def PN():
|
|||
form.referentiel.choices = [x for x in models.PN.query.all()]
|
||||
form_validation = form.validate_on_submit()
|
||||
form = form_import(form)
|
||||
if form_validation:
|
||||
form = form_charger(form)
|
||||
if form_validation and not form.charger.data:
|
||||
if form.exporter.data:
|
||||
flash("Ajout du référentiel PN: {} ".format(form.code.data))
|
||||
form_export(form)
|
||||
|
@ -36,7 +37,8 @@ def AC():
|
|||
form.referentiel.choices = [x for x in models.AC.query.all()]
|
||||
form_validation = form.validate_on_submit()
|
||||
form = form_import(form)
|
||||
if form_validation:
|
||||
form = form_charger(form)
|
||||
if form_validation and not form.charger.data:
|
||||
if form.exporter.data:
|
||||
flash("Ajout du référentiel AC: {} ".format(form.code.data))
|
||||
form_export(form)
|
||||
|
@ -56,7 +58,8 @@ def SAE():
|
|||
form.referentiel.choices = [x for x in models.SAE.query.all()]
|
||||
form_validation = form.validate_on_submit()
|
||||
form = form_import(form)
|
||||
if form_validation:
|
||||
form = form_charger(form)
|
||||
if form_validation and not form.charger.data:
|
||||
if form.exporter.data:
|
||||
flash("Ajout du référentiel SAE: {} ".format(form.code.data))
|
||||
form_export(form)
|
||||
|
@ -76,7 +79,8 @@ def Ressource():
|
|||
form.referentiel.choices = [x for x in models.Ressource.query.all()]
|
||||
form_validation = form.validate_on_submit()
|
||||
form = form_import(form)
|
||||
if form_validation:
|
||||
form = form_charger(form)
|
||||
if form_validation and not form.charger.data:
|
||||
if form.exporter.data:
|
||||
flash("Ajout du référentiel Ressource: {} ".format(form.code.data))
|
||||
form_export(form)
|
||||
|
@ -96,7 +100,8 @@ def Competence():
|
|||
form.referentiel.choices = [x for x in models.Competence.query.all()]
|
||||
form_validation = form.validate_on_submit()
|
||||
form = form_import(form)
|
||||
if form_validation:
|
||||
form = form_charger(form)
|
||||
if form_validation and not form.charger.data:
|
||||
if form.exporter.data:
|
||||
flash("Ajout du référentielCompetence: {} ".format(form.code.data))
|
||||
form_export(form)
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
{{ form.sauvegarder(class="button")}}
|
||||
</div>
|
||||
<div class="control">
|
||||
{{ form.charger(class="button is-static")}}
|
||||
{{ form.charger(class="button")}}
|
||||
</div>
|
||||
<div class="control">
|
||||
{{ form.exporter(class="button")}}
|
||||
|
|
Loading…
Reference in New Issue