Ajout exporterJSON

This commit is contained in:
Éric Li 2021-06-09 12:19:14 +02:00
parent 82d84d6b50
commit d7d30e43dd
5 changed files with 76 additions and 11 deletions

BIN
app.db

Binary file not shown.

View File

@ -10,20 +10,29 @@ from app import db
import app.models as models
import yaml
import json
import os
import re
REPERTOIRE_YAML = "./export/"
REPERTOIRE_EXPORT = "./export/"
if not os.path.exists(REPERTOIRE_YAML):
os.makedirs(REPERTOIRE_YAML)
if not os.path.exists(REPERTOIRE_EXPORT):
os.makedirs(REPERTOIRE_EXPORT)
categorie_liste = ["saes","ressources","acs"]
categorie_to_model = {"saes": "SAE", "ressources": "Ressource", "acs": "AC"}
categorie_to_model = {"saes": "SAE", "ressources": "Ressource", "acs": "AC", "coefsaes": "CoefSAE", "coefressources": "CoefRessource", "pns": "PN", "semestres": "Semestre", "competences": "Competence"}
separateur = None
BaseModelForm = model_form_factory(FlaskForm, include_primary_keys=True)
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, models.Semestre):
return obj.num
if isinstance(obj, models.Competence) or isinstance(obj, models.SAE) or isinstance(obj, models.Ressource) or isinstance(obj, models.AC):
return obj.code
return json.JSONEncoder.default(self, obj)
class ModelForm(BaseModelForm):
@classmethod
def get_session(self):
@ -35,8 +44,20 @@ class RefListField(QuerySelectMultipleField):
class AccueilForm(FlaskForm):
ajouter = SubmitField("Ajouter")
reset = SubmitField("Reset")
exporter = SubmitField("Exporter")
importer = SubmitField("Importer")
exporterJSON = SubmitField("ExporterJSON")
exporterYAML = SubmitField("ExporterYAML")
importerJSON = SubmitField("ImporterJSON")
importerYAML = SubmitField("ImporterYAML")
def exportJSON(self):
result = {"semestres": [], "competences": [], "acs": [], "pns": [], "saes": [], "coefsaes": [], "ressources": [], "coefressources": []}
for key in result.keys():
model = getattr(models, categorie_to_model[key])
for ref in model.query.all():
result[key].append(ref.export())
fichier = REPERTOIRE_EXPORT + "referentiels" + ".json"
with open(fichier, "w", encoding="utf8") as fid:
json.dump(result, fid, cls=CustomEncoder, indent=4)
class CoefForm(FlaskForm):
objetformation = HiddenField("Objet de formation")
@ -103,7 +124,7 @@ class Form(ModelForm):
output[categorie] = [ referentiel.code for referentiel in valeur ]
else:
output[categorie] = valeur
fichier = REPERTOIRE_YAML + self.code.data + ".yml"
fichier = REPERTOIRE_EXPORT + self.code.data + ".yml"
with open(fichier, "w", encoding="utf8") as fid:
fid.write(yaml.dump(output))
print(yaml.dump(output))

View File

@ -29,6 +29,11 @@ class Semestre(db.Model):
num = db.Column(db.String(1), primary_key = True)
ues = db.relationship("Competence", order_by="Competence.code", secondary=Semestres_Competences, lazy=False, backref=db.backref("semestres", lazy=False))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def __repr__(self):
return "<Semestre {}>".format(self.num)
@ -66,6 +71,11 @@ class Competence(db.Model):
db.session.delete(CoefRessource.query.filter_by(ressource=ressource, competence=self).first())
db.session.commit()
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self):
return self.code + " - " + self.nom
@ -79,6 +89,11 @@ class CoefSAE(db.Model):
competence = db.relationship("Competence", lazy=False, backref=db.backref("saes"))
sae = db.relationship("SAE", lazy=False, backref=db.backref("competences"))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
class CoefRessource(db.Model):
competence_code = db.Column(db.String(3), db.ForeignKey("competence.code"), primary_key=True)
ressource_code = db.Column(db.String(4), db.ForeignKey("ressource.code"), primary_key=True)
@ -86,12 +101,22 @@ class CoefRessource(db.Model):
competence = db.relationship("Competence", lazy=False, backref=db.backref("ressources"))
ressource = db.relationship("Ressource", lazy=False, backref=db.backref("competences"))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
class AC(db.Model):
code = db.Column(db.String(6), primary_key = True, info={'label': 'Code'})
titre = db.Column(db.String(255), info={'label': 'Titre'})
saes = db.relationship("SAE", secondary=SAEs_ACs, lazy=False, backref=db.backref("acs", lazy=False))
ressources = db.relationship("Ressource", order_by="Ressource.code", secondary=Ressources_ACs, lazy=False, backref=db.backref("acs", lazy=False))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self):
return self.code + " - " + self.titre
@ -105,6 +130,11 @@ class PN(db.Model):
description = db.Column(db.Text(), info={'label': 'Description'})
type = db.Column(db.String(1))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def __repr__(self):
return "<PN {}>".format(self.code)
@ -129,6 +159,11 @@ class SAE(db.Model):
def getCode(self):
return self.code[3:]
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self):
return self.code + " - " + self.titre
@ -155,6 +190,11 @@ class Ressource(db.Model):
def getCode(self):
return self.code[1:]
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self):
return self.code + " - " + self.nom

View File

@ -3,8 +3,6 @@ from app import app, db
from app.forms import *
import app.models as models
import yaml
@app.route("/")
@app.route("/index", methods=["GET","POST"])
def index():
@ -18,6 +16,10 @@ def index():
for semestre in models.Semestre.query.all():
db.session.delete(semestre)
db.session.commit()
elif form.exporterJSON.data:
form.exportJSON()
elif form.exporterYAML.data:
form.exportYAML()
semestres = sorted(models.Semestre.query.all(), key=lambda semestre: semestre.num)
return render_template("index.html", semestres=semestres, form=form)

View File

@ -27,8 +27,10 @@
{{ form.ajouter(class="button is-static") }}
{{ form.reset(class="button") }}
{% endif %}
{{ form.exporter(class="button is-static") }}
{{ form.importer(class="button is-static") }}
{{ form.exporterJSON(class="button") }}
{{ form.exporterYAML(class="button") }}
{{ form.importerJSON(class="button is-static") }}
{{ form.importerYAML(class="button is-static") }}
</div>
</form>