From 3c6e37f4818a7e644b74e3037299f1645cde4128 Mon Sep 17 00:00:00 2001 From: Nekori Date: Thu, 6 May 2021 19:26:09 +0200 Subject: [PATCH] Ajout de sauvegarde/update --- app/forms.py | 7 +++---- app/routes.py | 24 +++++++++++++++++++----- app/templates/form.html | 5 +++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/forms.py b/app/forms.py index 8aa3719..d392a68 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,7 +1,7 @@ from flask_wtf import FlaskForm from flask_wtf.file import FileAllowed from wtforms import StringField, SubmitField, FileField, TextAreaField, RadioField -from wtforms.validators import DataRequired, Regexp +from wtforms.validators import DataRequired, Regexp, Optional import yaml import os @@ -19,6 +19,7 @@ class Form(FlaskForm): exporter = SubmitField("Exporter") fichier = FileField("Choisir fichier", validators=[FileAllowed(["yml"], "Fichier Yaml seulement!")]) importer = SubmitField("Importer") + referentiel = RadioField("Liste des référentiels", validators=[Optional()]) class PNForm(Form): regex = "^PN\d$" @@ -36,7 +37,6 @@ class ACForm(Form): titre = StringField("Titre", validators=[DataRequired()] ) saes = StringField("SAEs", validators=[DataRequired()] ) - class SAEForm(Form): regex = "^SAE\d{2}$" @@ -53,7 +53,6 @@ class SAEForm(Form): livrables = TextAreaField("Livrables", validators=[DataRequired()] ) motscles = StringField("Mots clés", validators=[DataRequired()] ) - class RessourceForm(Form): regex = "^R\{3}$" @@ -101,7 +100,7 @@ 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())[5:-1]: + for categorie, valeur in list(form.data.items())[6:-1]: output[categorie] = valeur fichier = REPERTOIRE_YAML + form.code.data + ".yml" diff --git a/app/routes.py b/app/routes.py index 82090fe..5aa6dc7 100644 --- a/app/routes.py +++ b/app/routes.py @@ -13,6 +13,7 @@ def index(): @app.route("/PN", methods=["GET","POST"]) def PN(): form = PNForm() + form.referentiel.choices = [x for x in models.PN.query.all()] form_validation = form.validate_on_submit() form = form_import(form) if form_validation: @@ -20,7 +21,8 @@ def PN(): flash("Ajout du référentiel PN: {} ".format(form.code.data)) form_export(form) if form.sauvegarder.data: - if not "pn" in locals(): + pn = models.PN.query.filter_by(code=form.code.data).first() + if pn == None: pn = models.PN() form.populate_obj(pn) db.session.add(pn) @@ -31,13 +33,16 @@ def PN(): @app.route("/AC", methods=["GET","POST"]) def AC(): form = ACForm() + form.referentiel.choices = [x for x in models.AC.query.all()] form_validation = form.validate_on_submit() form = form_import(form) if form_validation: if form.exporter.data: flash("Ajout du référentiel AC: {} ".format(form.code.data)) form_export(form) - if not "ac" in locals(): + if form.sauvegarder.data: + ac = models.AC.query.filter_by(code=form.code.data).first() + if ac == None: ac = models.AC() form.populate_obj(ac) db.session.add(ac) @@ -48,13 +53,16 @@ def AC(): @app.route("/SAE", methods=["GET","POST"]) def SAE(): form = SAEForm() + form.referentiel.choices = [x for x in models.SAE.query.all()] form_validation = form.validate_on_submit() form = form_import(form) if form_validation: if form.exporter.data: flash("Ajout du référentiel SAE: {} ".format(form.code.data)) form_export(form) - if not "sae" in locals(): + if form.sauvegarder.data: + sae = models.SAE.query.filter_by(code=form.code.data).first() + if sae == None: sae = models.SAE() form.populate_obj(sae) db.session.add(sae) @@ -65,13 +73,16 @@ def SAE(): @app.route("/Ressource", methods=["GET","POST"]) def Ressource(): form = RessourceForm() + form.referentiel.choices = [x for x in models.Ressource.query.all()] form_validation = form.validate_on_submit() form = form_import(form) if form_validation: if form.exporter.data: flash("Ajout du référentiel Ressource: {} ".format(form.code.data)) form_export(form) - if not "ressource" in locals(): + if form.sauvegarder.data: + ressource = models.Ressource.query.filter_by(code=form.code.data).first() + if ressource == None: ressource = models.Ressource() form.populate_obj(ressource) db.session.add(ressource) @@ -82,13 +93,16 @@ def Ressource(): @app.route("/Competence", methods=["GET","POST"]) def Competence(): form = CompetenceForm() + form.referentiel.choices = [x for x in models.Competence.query.all()] form_validation = form.validate_on_submit() form = form_import(form) if form_validation: if form.exporter.data: flash("Ajout du référentielCompetence: {} ".format(form.code.data)) form_export(form) - if not "competence" in locals(): + if form.sauvegarder.data: + competence = models.Competence.query.filter_by(code=form.code.data).first() + if competence == None: competence = models.Competence() form.populate_obj(competence) db.session.add(competence) diff --git a/app/templates/form.html b/app/templates/form.html index ff7e518..ac737ad 100644 --- a/app/templates/form.html +++ b/app/templates/form.html @@ -49,6 +49,11 @@ {% endfor %} + {% endblock %} \ No newline at end of file