Ajout d'importation

This commit is contained in:
Éric Li 2021-06-14 18:44:53 +02:00
parent 835990a81e
commit 932fdfddef
2 changed files with 97 additions and 7 deletions

View File

@ -17,7 +17,7 @@ REPERTOIRE_EXPORT = "./export/"
if not os.path.exists(REPERTOIRE_EXPORT): if not os.path.exists(REPERTOIRE_EXPORT):
os.makedirs(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"} categorie_to_model = {"saes": "SAE", "ressources": "Ressource", "acs": "AC", "coefsaes": "CoefSAE", "coefressources": "CoefRessource", "pns": "PN", "semestres": "Semestre", "competences": "Competence"}
separateur = None separateur = None
@ -44,15 +44,18 @@ class AccueilForm(FlaskForm):
reset = SubmitField("Reset") reset = SubmitField("Reset")
exporterJSON = SubmitField("ExporterJSON") exporterJSON = SubmitField("ExporterJSON")
exporterYAML = SubmitField("ExporterYAML") exporterYAML = SubmitField("ExporterYAML")
importerJSON = SubmitField("ImporterJSON") fichier = FileField("Choisir fichier", validators=[FileAllowed(["yml","json"], "Fichier YAML/JSON seulement!")])
importerYAML = SubmitField("ImporterYAML") importer = SubmitField("Importer")
def ListRef(self): def ListRef(self):
result = {"semestres": [], "competences": [], "acs": [], "pns": [], "saes": [], "coefsaes": [], "ressources": [], "coefressources": []} 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(): for key in result.keys():
model = getattr(models, categorie_to_model[key]) model = getattr(models, categorie_to_model[key])
for ref in model.query.all(): for ref in model.query.all():
result[key].append(ref.export()) result[key].append(ref.export())
for assoName, association in associations.items():
result[assoName] = db.session.query(association).all()
return result return result
def exportJSON(self): def exportJSON(self):
@ -65,7 +68,71 @@ class AccueilForm(FlaskForm):
result = self.ListRef() result = self.ListRef()
fichier = REPERTOIRE_EXPORT + "referentiels" + ".yml" fichier = REPERTOIRE_EXPORT + "referentiels" + ".yml"
with open(fichier, "w", encoding="utf8") as fid: 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): class CoefForm(FlaskForm):
objetformation = HiddenField("Objet de formation") objetformation = HiddenField("Objet de formation")

View File

@ -2,6 +2,15 @@
{% block title %}Accueil{% endblock %} {% block title %}Accueil{% endblock %}
{% block content %} {% block content %}
{% if form.fichier.errors|length != 0 %}
<article class="message is-danger">
<div class="message-body">
{% for error in form.fichier.errors %}
<p>{{error}}</p>
{% endfor %}
</div>
</article>
{% endif %}
<div class="content"> <div class="content">
{% for semestre in semestres %} {% for semestre in semestres %}
<h1 class="title"><a style="color: inherit;" href="{{ url_for('Semestre', num=semestre.num) }}">Semestre {{ semestre.num }} <i class="far fa-edit fa-sm"></i></a></h1> <h1 class="title"><a style="color: inherit;" href="{{ url_for('Semestre', num=semestre.num) }}">Semestre {{ semestre.num }} <i class="far fa-edit fa-sm"></i></a></h1>
@ -17,7 +26,7 @@
</ul> </ul>
{% endfor %} {% endfor %}
</div> </div>
<form action="" method="post" novalidate> <form action="" enctype=multipart/form-data method="post" novalidate>
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
<div class="buttons"> <div class="buttons">
{% if not semestres %} {% if not semestres %}
@ -29,8 +38,22 @@
{% endif %} {% endif %}
{{ form.exporterJSON(class="button") }} {{ form.exporterJSON(class="button") }}
{{ form.exporterYAML(class="button") }} {{ form.exporterYAML(class="button") }}
{{ form.importerJSON(class="button is-static") }} <div class="control">
{{ form.importerYAML(class="button is-static") }} <div class="file has-name">
<label class="file-label">
{{ form.fichier(class="file-input") }}
<span class="file-cta">
<span class="file-icon">
<i class="fas fa-file-import"></i>
</span>
<span class="file-label">
{{ form.fichier.label }}
</span>
</span>
{{ form.importer(class="button file-name") }}
</label>
</div>
</div>
</div> </div>
</form> </form>