ScoDocMM/app/views/refcomp.py

116 lines
3.8 KiB
Python
Raw Normal View History

2021-12-02 17:36:32 +01:00
"""
PN / Référentiel de compétences
Emmanuel Viennet, 2021
"""
2021-12-03 11:03:33 +01:00
from flask import url_for, flash
2021-12-02 17:36:32 +01:00
from flask import jsonify
from flask import current_app, g, request
from flask.templating import render_template
from flask_login import current_user
from werkzeug.utils import redirect
2021-12-03 11:03:33 +01:00
from werkzeug.utils import secure_filename
2021-12-02 17:36:32 +01:00
from config import Config
from app import db
from app import models
from app.decorators import scodoc, permission_required
2021-12-03 11:03:33 +01:00
from app.models.formations import Formation
2021-12-02 17:36:32 +01:00
from app.models.but_refcomp import ApcReferentielCompetences
2021-12-03 11:03:33 +01:00
from app.but.import_refcomp import orebut_import_refcomp
from app.but.forms.refcomp_forms import FormationRefCompForm, RefCompLoadForm
2021-12-03 14:13:49 +01:00
from app.scodoc.sco_exceptions import ScoFormatError
2021-12-02 17:36:32 +01:00
from app.scodoc.sco_permissions import Permission
from app.views import notes_bp as bp
2021-12-03 11:03:33 +01:00
@bp.route("/referentiel/comp/<int:refcomp_id>")
2021-12-02 17:36:32 +01:00
@scodoc
@permission_required(Permission.ScoView)
def refcomp(refcomp_id):
ref = ApcReferentielCompetences.query.get_or_404(refcomp_id)
return jsonify(ref.to_dict())
2021-12-03 11:03:33 +01:00
@bp.route("/refcomp_assoc/<int:formation_id>", methods=["GET", "POST"])
@scodoc
@permission_required(Permission.ScoChangeFormation)
def refcomp_assoc(formation_id: int):
"""Formulaire association ref. compétence"""
formation = Formation.query.get_or_404(formation_id)
form = FormationRefCompForm()
form.referentiel_competence.choices = [
(r.id, f"{r.type_titre} {r.specialite_long}")
for r in ApcReferentielCompetences.query.filter_by(dept_id=g.scodoc_dept_id)
]
if request.method == "POST" and form.cancel.data: # cancel button
return redirect(
url_for(
"notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation_id
)
)
if form.validate_on_submit():
referentiel_competence_id = form.referentiel_competence.data
assert (
ApcReferentielCompetences.query.get(referentiel_competence_id) is not None
)
formation.referentiel_competence_id = referentiel_competence_id
db.session.add(formation)
db.session.commit()
flash("nouveau référentiel de compétences associé")
return redirect(
url_for(
"notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation_id
)
)
return render_template(
"but/refcomp_assoc.html",
form=form,
referentiel_competence_id=formation.referentiel_competence_id,
formation=formation,
)
@bp.route("/refcomp_load/<int:formation_id>", methods=["GET", "POST"])
@scodoc
@permission_required(Permission.ScoChangeFormation)
def refcomp_load(formation_id=None):
"""Formulaire association ref. compétence"""
if formation_id is not None:
formation = Formation.query.get_or_404(formation_id)
else:
formation = None
form = RefCompLoadForm()
if form.validate_on_submit():
f = form.upload.data
filename = secure_filename(f.filename)
2021-12-03 14:13:49 +01:00
try:
xml_data = f.read()
ref = orebut_import_refcomp(
xml_data, dept_id=g.scodoc_dept_id, orig_filename=filename
)
except TypeError as exc:
raise ScoFormatError("fichier XML Orébut invalide") from exc
except ScoFormatError:
raise
2021-12-03 11:03:33 +01:00
if formation is not None:
return redirect(
url_for(
"notes.refcomp_assoc",
scodoc_dept=g.scodoc_dept,
formation_id=formation.formation_id,
)
)
else:
return redirect(url_for("notes.index_html", scodoc_dept=g.scodoc_dept))
return render_template(
"but/refcomp_load.html",
form=form,
formation=formation,
)