diff --git a/app/forms.py b/app/forms.py index 23337e7..748be8c 100644 --- a/app/forms.py +++ b/app/forms.py @@ -17,7 +17,7 @@ REPERTOIRE_EXPORT = "./export/" if not os.path.exists(REPERTOIRE_EXPORT): os.makedirs(REPERTOIRE_EXPORT) -categorie_liste = ["saes","ressources","acs"] +categorie_liste = ["saes","ressources","acs","ues","semestres","competences"] categorie_to_model = {"saes": "SAE", "ressources": "Ressource", "acs": "AC", "coefsaes": "CoefSAE", "coefressources": "CoefRessource", "pns": "PN", "semestres": "Semestre", "competences": "Competence"} separateur = None @@ -44,15 +44,18 @@ class AccueilForm(FlaskForm): reset = SubmitField("Reset") exporterJSON = SubmitField("ExporterJSON") exporterYAML = SubmitField("ExporterYAML") - importerJSON = SubmitField("ImporterJSON") - importerYAML = SubmitField("ImporterYAML") + fichier = FileField("Choisir fichier", validators=[FileAllowed(["yml","json"], "Fichier YAML/JSON seulement!")]) + importer = SubmitField("Importer") def ListRef(self): result = {"semestres": [], "competences": [], "acs": [], "pns": [], "saes": [], "coefsaes": [], "ressources": [], "coefressources": []} + associations = {"semestres_competences": models.Semestres_Competences, "acs_competences": models.ACs_Competences, "ressources_acs": models.Ressources_ACs, "ressources_saes": models.Ressources_SAEs, "saes_acs": models.SAEs_ACs} for key in result.keys(): model = getattr(models, categorie_to_model[key]) for ref in model.query.all(): result[key].append(ref.export()) + for assoName, association in associations.items(): + result[assoName] = db.session.query(association).all() return result def exportJSON(self): @@ -65,7 +68,71 @@ class AccueilForm(FlaskForm): result = self.ListRef() fichier = REPERTOIRE_EXPORT + "referentiels" + ".yml" with open(fichier, "w", encoding="utf8") as fid: - yaml.dump(yaml.safe_load(json.dumps(result, cls=CustomEncoder)), fid, indent=4) + yaml.dump(yaml.safe_load(json.dumps(result, cls=CustomEncoder)), fid, indent=4, sort_keys=False) + + def importRef(self): + fichier = self.fichier.data + filename, extension = fichier.filename.split(".") + if extension == "yml": + filedata = yaml.safe_load(fichier.read()) + elif extension == "json": + filedata = json.load(fichier) + else: + self.fichier.errors.append("Une erreur a été produite.") + return + for categorie, liste in filedata.items(): + if "_" not in categorie: + model = getattr(models, categorie_to_model[categorie]) + for ref in liste: + print("Adding:", ref) + if categorie == "semestres": + semestre = model.query.filter_by(num=ref["num"]).first() + if not semestre: + ref["ues"] = [] + db.session.add(model(**ref)) + else: + print("Not added: Already in database") + elif "coef" in categorie: + competence = models.Competence.query.filter_by(code=ref["competence"]).first() + if categorie == "coefsaes": + sae = models.SAE.query.filter_by(code=ref["sae"]).first() + if not models.CoefSAE.query.filter_by(sae=sae,competence=competence).first(): + db.session.add(models.CoefSAE(sae=sae,competence=competence,coef=ref["coef"])) + else: + print("Not added: Already in database") + elif categorie == "coefressources": + ressource = models.Ressource.query.filter_by(code=ref["ressource"]).first() + if not models.CoefRessource.query.filter_by(ressource=ressource, competence=competence).first(): + db.session.add(models.CoefRessource(ressource=ressource, competence=competence,coef=ref["coef"])) + else: + print("Not added: Already in database") + else: + refBDD = model.query.filter_by(code=ref["code"]).first() + if not refBDD: + for categorieRef, valeur in ref.items(): + if categorieRef in categorie_liste: + ref[categorieRef] = [] + db.session.add(model(**ref)) + else: + print("Not added: Already in database") + else: + for assoc in liste: + print("Adding association:", assoc) + firstModelname, secondModelname = categorie.split("_") + firstVal, secondVal = assoc + firstModel = getattr(models, categorie_to_model[firstModelname]) + secondModel = getattr(models, categorie_to_model[secondModelname]) + if firstModelname == "semestres": + firstRef = firstModel.query.filter_by(num=firstVal).first() + secondRef = secondModel.query.filter_by(code=secondVal).first() + if secondRef not in firstRef.ues: firstRef.ues.append(secondRef) + else: print("Not added: Association already exist") + else: + firstRef = firstModel.query.filter_by(code=firstVal).first() + secondRef = secondModel.query.filter_by(code=secondVal).first() + if secondRef not in firstRef.__dict__[secondModelname]: firstRef.__dict__[secondModelname].append(secondRef) + else: print("Not added: Association already exist") + db.session.commit() class CoefForm(FlaskForm): objetformation = HiddenField("Objet de formation") diff --git a/app/templates/index.html b/app/templates/index.html index d45dc5e..c642aba 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,6 +2,15 @@ {% block title %}Accueil{% endblock %} {% block content %} +{% if form.fichier.errors|length != 0 %} +
+
+ {% for error in form.fichier.errors %} +

{{error}}

+ {% endfor %} +
+
+{% endif %}
{% for semestre in semestres %}

Semestre {{ semestre.num }}

@@ -17,7 +26,7 @@ {% endfor %}
-
+ {{ form.hidden_tag() }}
{% if not semestres %} @@ -29,8 +38,22 @@ {% endif %} {{ form.exporterJSON(class="button") }} {{ form.exporterYAML(class="button") }} - {{ form.importerJSON(class="button is-static") }} - {{ form.importerYAML(class="button is-static") }} +
+
+ +
+