1
0
forked from ScoDoc/ScoDoc
ScoDoc-Front/app/views/refcomp.py

274 lines
9.1 KiB
Python
Raw Normal View History

2021-12-02 17:36:32 +01:00
"""
PN / Référentiel de compétences
Emmanuel Viennet, 2021
"""
from pathlib import Path
import re
2021-12-02 17:36:32 +01:00
from flask import jsonify, flash, url_for
from flask import Markup
2021-12-02 17:36:32 +01:00
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 log
2021-12-02 17:36:32 +01:00
from app.decorators import scodoc, permission_required
2021-12-08 22:33:32 +01:00
from app.models 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 15:46:46 +01:00
from app.scodoc.gen_tables import GenTable
2021-12-04 22:20:08 +01:00
from app.scodoc import sco_utils as scu
from app.scodoc.sco_exceptions import ScoFormatError, ScoValueError
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-04 21:06:32 +01:00
from app.views import ScoData
2021-12-02 17:36:32 +01:00
2021-12-03 15:46:46 +01:00
@bp.route("/referentiel/comp/get/<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
2021-12-04 22:20:08 +01:00
@bp.route("/referentiel/comp/show/<int:refcomp_id>")
@scodoc
@permission_required(Permission.ScoView)
def refcomp_show(refcomp_id):
ref = ApcReferentielCompetences.query.get_or_404(refcomp_id)
return render_template(
"but/refcomp_show.html",
ref=ref,
title="Référentiel de compétences",
sco=ScoData(),
data_source=url_for(
"notes.refcomp",
scodoc_dept=g.scodoc_dept,
refcomp_id=refcomp_id,
),
2021-12-04 22:20:08 +01:00
)
@bp.route("/referentiel/comp/delete/<int:refcomp_id>", methods=["GET", "POST"])
@scodoc
@permission_required(Permission.ScoChangeFormation)
def refcomp_delete(refcomp_id):
ref = ApcReferentielCompetences.query.get_or_404(refcomp_id)
db.session.delete(ref)
db.session.commit()
flash("référentiel de compétences supprimé")
return redirect(url_for("notes.refcomp_table", scodoc_dept=g.scodoc_dept))
2021-12-03 15:46:46 +01:00
@bp.route("/referentiel/comp/table")
@scodoc
@permission_required(Permission.ScoView)
def refcomp_table():
"""Liste html des ref. comp. chargés dans ce département"""
refs = ApcReferentielCompetences.query.filter_by(dept_id=g.scodoc_dept_id)
2021-12-04 22:20:08 +01:00
columns_ids = ("type_titre", "specialite_long", "json", "nb_formations")
if current_user.has_permission(Permission.ScoChangeFormation):
columns_ids = ("suppr",) + columns_ids
suppr_icon = scu.icontag(
"delete_small_img", border="0", alt="supprimer", title="Supprimer"
)
2021-12-03 15:46:46 +01:00
tab = GenTable(
2021-12-04 22:20:08 +01:00
columns_ids=columns_ids,
titles={
"suppr": "",
"type_titre": "Type",
"specialite_long": "Spécialité",
"json": "Export",
"nb_formations": "Formations",
},
2021-12-03 15:46:46 +01:00
rows=[
{
"type_titre": ref.type_titre,
"specialite_long": ref.specialite_long,
2021-12-04 22:20:08 +01:00
"_specialite_long_target": url_for(
"notes.refcomp_show", scodoc_dept=g.scodoc_dept, refcomp_id=ref.id
),
2021-12-03 15:46:46 +01:00
"json": "json",
"_json_target": url_for(
"notes.refcomp", scodoc_dept=g.scodoc_dept, refcomp_id=ref.id
),
"nb_formations": len(ref.formations),
2021-12-04 22:20:08 +01:00
"suppr": suppr_icon,
"_suppr_target": url_for(
"notes.refcomp_delete", scodoc_dept=g.scodoc_dept, refcomp_id=ref.id
),
2021-12-03 15:46:46 +01:00
}
for ref in refs
],
)
2021-12-04 21:06:32 +01:00
return render_template(
"but/refcomp_table.html",
tab=tab,
title="Référentiels de compétences",
sco=ScoData(),
)
2021-12-03 15:46:46 +01:00
2021-12-04 21:06:32 +01:00
@bp.route(
"/referentiel/comp/assoc_formation/<int:formation_id>", methods=["GET", "POST"]
)
2021-12-03 11:03:33 +01:00
@scodoc
@permission_required(Permission.ScoChangeFormation)
2021-12-04 21:06:32 +01:00
def refcomp_assoc_formation(formation_id: int):
2021-12-03 11:03:33 +01:00
"""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,
2021-12-04 21:06:32 +01:00
title="Association réf. compétences",
2021-12-03 11:03:33 +01:00
)
@bp.route("/referentiel/comp/desassoc_formation/<int:formation_id>", methods=["GET"])
@scodoc
@permission_required(Permission.ScoChangeFormation)
def refcomp_desassoc_formation(formation_id: int):
"""Désassocie la formation de son ref. de compétence"""
formation = Formation.query.get_or_404(formation_id)
formation.referentiel_competence = None
db.session.add(formation)
db.session.commit()
return redirect(
url_for("notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation.id)
)
2021-12-03 15:46:46 +01:00
@bp.route(
"/referentiel/comp/load", defaults={"formation_id": None}, methods=["GET", "POST"]
)
@bp.route("/referentiel/comp/load/<int:formation_id>", methods=["GET", "POST"])
2021-12-03 11:03:33 +01:00
@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
refs_distrib_files = sorted(
list(
(
Path(current_app.config["SCODOC_DIR"])
/ "ressources/referentiels/but2022/competences"
).glob("*.xml")
)
)
refs_distrib_dict = [{"id": 0, "specialite": "Aucun", "created": "", "serial": ""}]
i = 1
for filename in refs_distrib_files:
m = re.match(r".*/but-([A-Za-z_]+)-([0-9]+)-([0-9]+).xml", str(filename))
if (
m
and ApcReferentielCompetences.query.filter_by(
scodoc_orig_filename=Path(filename).name, dept_id=g.scodoc_dept_id
).count()
== 0
):
refs_distrib_dict.append(
{
"id": i,
"specialite": m.group(1),
"created": m.group(2),
"serial": m.group(3),
"filename": str(filename),
}
)
i += 1
else:
log(f"refcomp_load: ignoring {filename} (invalid filename)")
2021-12-03 11:03:33 +01:00
form = RefCompLoadForm()
form.referentiel_standard.choices = [
(r["id"], f"{r['specialite']} ({r['created']}-{r['serial']})")
for r in refs_distrib_dict
]
2021-12-03 11:03:33 +01:00
if form.validate_on_submit():
if form.upload.data:
f = form.upload.data
filename = secure_filename(f.filename)
elif form.referentiel_standard.data:
try:
filename = refs_distrib_dict[int(form.referentiel_standard.data)][
"filename"
]
except (ValueError, IndexError):
raise ScoValueError("choix invalide")
f = open(filename)
else:
raise ScoValueError("choix invalide")
2021-12-03 14:13:49 +01:00
try:
xml_data = f.read()
_ = orebut_import_refcomp(
xml_data, dept_id=g.scodoc_dept_id, orig_filename=Path(filename).name
2021-12-03 14:13:49 +01:00
)
except TypeError as exc:
raise ScoFormatError(
f"fichier XML Orébut invalide (1): {exc.args}"
) from exc
2021-12-03 14:13:49 +01:00
except ScoFormatError:
raise
flash(
Markup(f"Référentiel <tt>{Path(filename).name}</tt> chargé."),
category="info",
)
2021-12-03 11:03:33 +01:00
if formation is not None:
return redirect(
url_for(
2021-12-04 21:06:32 +01:00
"notes.refcomp_assoc_formation",
2021-12-03 11:03:33 +01:00
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,
2021-12-04 21:06:32 +01:00
title="Chargement réf. compétences",
2021-12-03 11:03:33 +01:00
)