Compare commits

...

2 Commits

Author SHA1 Message Date
Éric Li 932fdfddef Ajout d'importation 2021-06-14 18:44:53 +02:00
Éric Li 835990a81e Fix: erreur suppr 2021-06-14 18:44:17 +02:00
3 changed files with 101 additions and 8 deletions

View File

@ -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")

View File

@ -7,6 +7,7 @@ import app.models as models
@app.route("/index", methods=["GET","POST"])
def index():
form = AccueilForm()
valide = form.validate_on_submit()
if form.ajouter.data:
for i in range(1,7):
semestre = models.Semestre(num=i)
@ -20,6 +21,8 @@ def index():
form.exportJSON()
elif form.exporterYAML.data:
form.exportYAML()
elif form.importer.data and valide:
form.importRef()
semestres = sorted(models.Semestre.query.all(), key=lambda semestre: semestre.num)
return render_template("index.html", semestres=semestres, form=form)
@ -68,7 +71,7 @@ def Semestre(num):
codeClass, code = coeffield.objetformation.data[1:-1].split()
model = getattr(models, codeClass)
objetformation = model.query.filter_by(code=code).first()
if objetformation in querySAE or objetformation in queryRessource:
if objetformation in list(set(querySAE)&set(field.saes.data)) or objetformation in list(set(queryRessource)&set(field.ressources.data)):
objetformation.setCoef(coeffield.coef.data, ue)
db.session.commit()
return redirect(url_for("Semestre", num=num))

View File

@ -2,6 +2,15 @@
{% block title %}Accueil{% endblock %}
{% 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">
{% 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>
@ -17,7 +26,7 @@
</ul>
{% endfor %}
</div>
<form action="" method="post" novalidate>
<form action="" enctype=multipart/form-data method="post" novalidate>
{{ form.hidden_tag() }}
<div class="buttons">
{% 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") }}
<div class="control">
<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>
</form>