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 app.models as models
import yaml import yaml
import json
import os import os
import re import re
REPERTOIRE_YAML = "./export/" REPERTOIRE_EXPORT = "./export/"
if not os.path.exists(REPERTOIRE_YAML): if not os.path.exists(REPERTOIRE_EXPORT):
os.makedirs(REPERTOIRE_YAML) os.makedirs(REPERTOIRE_EXPORT)
categorie_liste = ["saes","ressources","acs"] 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 separateur = None
BaseModelForm = model_form_factory(FlaskForm, include_primary_keys=True) 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): class ModelForm(BaseModelForm):
@classmethod @classmethod
def get_session(self): def get_session(self):
@ -35,8 +44,20 @@ class RefListField(QuerySelectMultipleField):
class AccueilForm(FlaskForm): class AccueilForm(FlaskForm):
ajouter = SubmitField("Ajouter") ajouter = SubmitField("Ajouter")
reset = SubmitField("Reset") reset = SubmitField("Reset")
exporter = SubmitField("Exporter") exporterJSON = SubmitField("ExporterJSON")
importer = SubmitField("Importer") 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): class CoefForm(FlaskForm):
objetformation = HiddenField("Objet de formation") objetformation = HiddenField("Objet de formation")
@ -103,7 +124,7 @@ class Form(ModelForm):
output[categorie] = [ referentiel.code for referentiel in valeur ] output[categorie] = [ referentiel.code for referentiel in valeur ]
else: else:
output[categorie] = valeur 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: with open(fichier, "w", encoding="utf8") as fid:
fid.write(yaml.dump(output)) fid.write(yaml.dump(output))
print(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) 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)) 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): def __repr__(self):
return "<Semestre {}>".format(self.num) 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.delete(CoefRessource.query.filter_by(ressource=ressource, competence=self).first())
db.session.commit() db.session.commit()
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self): def getLabel(self):
return self.code + " - " + self.nom return self.code + " - " + self.nom
@ -79,6 +89,11 @@ class CoefSAE(db.Model):
competence = db.relationship("Competence", lazy=False, backref=db.backref("saes")) competence = db.relationship("Competence", lazy=False, backref=db.backref("saes"))
sae = db.relationship("SAE", lazy=False, backref=db.backref("competences")) 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): class CoefRessource(db.Model):
competence_code = db.Column(db.String(3), db.ForeignKey("competence.code"), primary_key=True) 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) 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")) competence = db.relationship("Competence", lazy=False, backref=db.backref("ressources"))
ressource = db.relationship("Ressource", lazy=False, backref=db.backref("competences")) 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): class AC(db.Model):
code = db.Column(db.String(6), primary_key = True, info={'label': 'Code'}) code = db.Column(db.String(6), primary_key = True, info={'label': 'Code'})
titre = db.Column(db.String(255), info={'label': 'Titre'}) titre = db.Column(db.String(255), info={'label': 'Titre'})
saes = db.relationship("SAE", secondary=SAEs_ACs, lazy=False, backref=db.backref("acs", lazy=False)) 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)) 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): def getLabel(self):
return self.code + " - " + self.titre return self.code + " - " + self.titre
@ -105,6 +130,11 @@ class PN(db.Model):
description = db.Column(db.Text(), info={'label': 'Description'}) description = db.Column(db.Text(), info={'label': 'Description'})
type = db.Column(db.String(1)) type = db.Column(db.String(1))
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def __repr__(self): def __repr__(self):
return "<PN {}>".format(self.code) return "<PN {}>".format(self.code)
@ -129,6 +159,11 @@ class SAE(db.Model):
def getCode(self): def getCode(self):
return self.code[3:] return self.code[3:]
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self): def getLabel(self):
return self.code + " - " + self.titre return self.code + " - " + self.titre
@ -155,6 +190,11 @@ class Ressource(db.Model):
def getCode(self): def getCode(self):
return self.code[1:] return self.code[1:]
def export(self):
result = dict(self.__dict__)
result.pop("_sa_instance_state", None)
return result
def getLabel(self): def getLabel(self):
return self.code + " - " + self.nom return self.code + " - " + self.nom

View File

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

View File

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