2021-12-02 17:36:32 +01:00
|
|
|
"""
|
|
|
|
PN / Référentiel de compétences
|
|
|
|
|
|
|
|
Emmanuel Viennet, 2021
|
|
|
|
"""
|
2024-04-05 23:41:34 +02:00
|
|
|
|
2022-02-12 20:25:22 +01:00
|
|
|
from pathlib import Path
|
|
|
|
import re
|
2021-12-02 17:36:32 +01:00
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import flash, url_for
|
2021-12-02 17:36:32 +01:00
|
|
|
from flask import current_app, g, request
|
|
|
|
from flask.templating import render_template
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask_json import as_json
|
2021-12-02 17:36:32 +01:00
|
|
|
from flask_login import current_user
|
2023-07-11 06:57:38 +02:00
|
|
|
from markupsafe import Markup
|
2021-12-02 17:36:32 +01:00
|
|
|
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
|
|
|
|
2023-07-11 06:57:38 +02:00
|
|
|
from app import db, 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
|
2024-04-05 23:41:34 +02:00
|
|
|
from app.but import change_refcomp
|
2021-12-03 11:03:33 +01:00
|
|
|
from app.but.import_refcomp import orebut_import_refcomp
|
2024-04-05 23:41:34 +02:00
|
|
|
from app.but.forms.refcomp_forms import (
|
|
|
|
FormationChangeRefCompForm,
|
|
|
|
FormationRefCompForm,
|
|
|
|
RefCompLoadForm,
|
2024-10-21 19:12:01 +02:00
|
|
|
RefCompUploadForm,
|
2024-04-05 23:41:34 +02:00
|
|
|
)
|
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
|
2024-04-05 23:41:34 +02:00
|
|
|
from app.scodoc.sco_exceptions import (
|
|
|
|
ScoFormatError,
|
|
|
|
ScoNoReferentielCompetences,
|
|
|
|
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)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2021-12-02 17:36:32 +01:00
|
|
|
def refcomp(refcomp_id):
|
2023-01-12 17:08:39 +01:00
|
|
|
"""Le référentiel de compétences, en JSON."""
|
2023-03-28 21:59:09 +02:00
|
|
|
ref: ApcReferentielCompetences = ApcReferentielCompetences.query.get_or_404(
|
|
|
|
refcomp_id
|
|
|
|
)
|
2023-04-06 16:10:32 +02:00
|
|
|
return 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):
|
2023-01-12 17:08:39 +01:00
|
|
|
"""Affichage du référentiel de compétences."""
|
2024-06-02 12:05:01 +02:00
|
|
|
referentiel_competence: ApcReferentielCompetences = (
|
|
|
|
ApcReferentielCompetences.query.get_or_404(refcomp_id)
|
|
|
|
)
|
2024-04-05 23:41:34 +02:00
|
|
|
# Autres référentiels "équivalents" pour proposer de changer les formations:
|
|
|
|
referentiels_equivalents = referentiel_competence.equivalents()
|
2021-12-04 22:20:08 +01:00
|
|
|
return render_template(
|
2023-01-30 22:25:17 +01:00
|
|
|
"but/refcomp_show.j2",
|
2023-03-26 10:08:50 +02:00
|
|
|
ref=referentiel_competence,
|
2024-04-05 23:41:34 +02:00
|
|
|
referentiels_equivalents=referentiels_equivalents,
|
2021-12-04 22:20:08 +01:00
|
|
|
title="Référentiel de compétences",
|
2022-01-09 23:13:09 +01:00
|
|
|
data_source=url_for(
|
|
|
|
"notes.refcomp",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
refcomp_id=refcomp_id,
|
|
|
|
),
|
2023-03-26 10:08:50 +02:00
|
|
|
sco=ScoData(),
|
2023-03-28 21:59:09 +02:00
|
|
|
table_niveaux_parcours=referentiel_competence.table_niveaux_parcours(),
|
2021-12-04 22:20:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/referentiel/comp/delete/<int:refcomp_id>", methods=["GET", "POST"])
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.EditFormation)
|
2021-12-04 22:20:08 +01:00
|
|
|
def refcomp_delete(refcomp_id):
|
2023-01-12 17:08:39 +01:00
|
|
|
"""Suppression du référentiel de la base. Le fichier source n'est pas affecté."""
|
2021-12-04 22:20:08 +01:00
|
|
|
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)
|
2022-12-02 23:02:42 +01:00
|
|
|
columns_ids = ("type_titre", "specialite_long", "version", "json", "nb_formations")
|
2023-09-29 21:17:31 +02:00
|
|
|
if current_user.has_permission(Permission.EditFormation):
|
2021-12-04 22:20:08 +01:00
|
|
|
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é",
|
2022-12-02 23:02:42 +01:00
|
|
|
"version": "Version",
|
2021-12-04 22:20:08 +01:00
|
|
|
"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
|
|
|
|
),
|
2022-12-02 23:02:42 +01:00
|
|
|
"version": ref.get_version(),
|
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
|
|
|
|
],
|
2024-05-22 00:06:30 +02:00
|
|
|
table_id="refcomp_table",
|
2021-12-03 15:46:46 +01:00
|
|
|
)
|
2021-12-04 21:06:32 +01:00
|
|
|
return render_template(
|
2023-01-30 22:25:17 +01:00
|
|
|
"but/refcomp_table.j2",
|
2021-12-04 21:06:32 +01:00
|
|
|
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
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.EditFormation)
|
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 = [
|
2022-12-02 17:41:19 +01:00
|
|
|
(r.id, f"{r.type_titre} {r.specialite_long} ({r.get_version()})")
|
2021-12-03 11:03:33 +01:00
|
|
|
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 (
|
2023-07-11 06:57:38 +02:00
|
|
|
db.session.get(ApcReferentielCompetences, referentiel_competence_id)
|
|
|
|
is not None
|
2021-12-03 11:03:33 +01:00
|
|
|
)
|
2023-07-17 15:09:17 +02:00
|
|
|
if formation.referentiel_competence_id != referentiel_competence_id:
|
|
|
|
formation.refcomp_desassoc()
|
|
|
|
formation.referentiel_competence_id = referentiel_competence_id
|
|
|
|
db.session.add(formation)
|
|
|
|
db.session.commit()
|
|
|
|
flash("Nouveau référentiel de compétences associé")
|
|
|
|
else:
|
|
|
|
flash("Pas de changements")
|
2021-12-03 11:03:33 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return render_template(
|
2023-01-30 22:25:17 +01:00
|
|
|
"but/refcomp_assoc.j2",
|
2021-12-03 11:03:33 +01:00
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-05-22 03:26:39 +02:00
|
|
|
@bp.route("/referentiel/comp/desassoc_formation/<int:formation_id>", methods=["GET"])
|
|
|
|
@scodoc
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.EditFormation)
|
2022-05-22 03:26:39 +02:00
|
|
|
def refcomp_desassoc_formation(formation_id: int):
|
|
|
|
"""Désassocie la formation de son ref. de compétence"""
|
2023-07-17 15:09:17 +02:00
|
|
|
formation: Formation = Formation.query.get_or_404(formation_id)
|
2022-10-29 08:22:17 +02:00
|
|
|
formation.refcomp_desassoc()
|
2022-05-22 03:26:39 +02:00
|
|
|
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
|
2023-09-29 21:17:31 +02:00
|
|
|
@permission_required(Permission.EditFormation)
|
2021-12-03 11:03:33 +01:00
|
|
|
def refcomp_load(formation_id=None):
|
|
|
|
"""Formulaire association ref. compétence"""
|
2024-10-21 19:12:01 +02:00
|
|
|
formation = (
|
|
|
|
Formation.get_formation(formation_id) if formation_id is not None else None
|
|
|
|
)
|
2022-02-12 20:25:22 +01:00
|
|
|
refs_distrib_files = sorted(
|
|
|
|
list(
|
|
|
|
(
|
|
|
|
Path(current_app.config["SCODOC_DIR"])
|
2023-07-17 14:23:50 +02:00
|
|
|
/ "ressources/referentiels/but2023/competences"
|
2022-02-12 20:25:22 +01:00
|
|
|
).glob("*.xml")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
refs_distrib_dict = [{"id": 0, "specialite": "Aucun", "created": "", "serial": ""}]
|
|
|
|
i = 1
|
|
|
|
for filename in refs_distrib_files:
|
2022-06-23 14:16:53 +02:00
|
|
|
m = re.match(r".*/but-([A-Za-z0-9_]+)-([0-9]+)-([0-9]+).xml", str(filename))
|
|
|
|
if not m:
|
|
|
|
log(f"refcomp_load: ignoring {filename} (invalid filename)")
|
|
|
|
elif (
|
|
|
|
ApcReferentielCompetences.query.filter_by(
|
2022-02-12 20:25:22 +01:00
|
|
|
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:
|
2022-06-23 14:16:53 +02:00
|
|
|
log(f"refcomp_load: ignoring {filename} (already loaded)")
|
2022-02-12 20:25:22 +01:00
|
|
|
|
2021-12-03 11:03:33 +01:00
|
|
|
form = RefCompLoadForm()
|
2022-02-12 20:25:22 +01:00
|
|
|
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():
|
2024-10-21 19:12:01 +02:00
|
|
|
if form.referentiel_standard.data:
|
2022-02-12 20:25:22 +01:00
|
|
|
try:
|
|
|
|
filename = refs_distrib_dict[int(form.referentiel_standard.data)][
|
|
|
|
"filename"
|
|
|
|
]
|
2022-11-29 00:07:34 +01:00
|
|
|
except (ValueError, IndexError) as exc:
|
|
|
|
raise ScoValueError("choix invalide") from exc
|
2022-02-12 20:25:22 +01:00
|
|
|
else:
|
|
|
|
raise ScoValueError("choix invalide")
|
2024-10-21 19:12:01 +02:00
|
|
|
with open(filename, encoding="utf-8") as stream:
|
|
|
|
_upload_ref_xml(filename, stream)
|
2022-02-12 20:25:22 +01:00
|
|
|
|
2023-04-07 17:10:17 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.refcomp_table",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
2021-12-03 11:03:33 +01:00
|
|
|
)
|
2023-04-07 17:10:17 +02:00
|
|
|
)
|
2021-12-03 11:03:33 +01:00
|
|
|
|
|
|
|
return render_template(
|
2023-01-30 22:25:17 +01:00
|
|
|
"but/refcomp_load.j2",
|
2021-12-03 11:03:33 +01:00
|
|
|
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
|
|
|
)
|
2024-04-05 23:41:34 +02:00
|
|
|
|
|
|
|
|
2024-10-21 19:12:01 +02:00
|
|
|
def _upload_ref_xml(filename: str, stream):
|
|
|
|
"""
|
|
|
|
peut lever ScoFormatError.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
xml_data = stream.read()
|
|
|
|
_ = orebut_import_refcomp(
|
|
|
|
xml_data, dept_id=g.scodoc_dept_id, orig_filename=Path(filename).name
|
|
|
|
)
|
|
|
|
except TypeError as exc:
|
|
|
|
raise ScoFormatError(f"fichier XML Orébut invalide (1): {exc.args}") from exc
|
|
|
|
#
|
|
|
|
flash(
|
|
|
|
Markup(f"Référentiel <tt>{Path(filename).name}</tt> chargé."),
|
|
|
|
category="info",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route(
|
|
|
|
"/referentiel/comp/upload_new",
|
|
|
|
defaults={"formation_id": None},
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
)
|
|
|
|
@bp.route("/referentiel/comp/upload_new/<int:formation_id>", methods=["GET", "POST"])
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.EditFormation)
|
|
|
|
def refcomp_upload_new(formation_id=None):
|
|
|
|
"""Formulaire chargement fichier externe ref. compétence"""
|
|
|
|
formation = (
|
|
|
|
Formation.get_formation(formation_id) if formation_id is not None else None
|
|
|
|
)
|
|
|
|
form = RefCompUploadForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if form.upload.data:
|
|
|
|
f = form.upload.data
|
|
|
|
filename = secure_filename(f.filename)
|
|
|
|
_upload_ref_xml(filename, f)
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.refcomp_table",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if form.cancel.data: # cancel button
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.refcomp_load",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"but/refcomp_upload_new.j2",
|
|
|
|
form=form,
|
|
|
|
formation=formation,
|
|
|
|
title="Chargement réf. compétences externe",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-05 23:41:34 +02:00
|
|
|
@bp.route("/formation/<int:formation_id>/change_refcomp", methods=["GET", "POST"])
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.EditFormation)
|
|
|
|
def formation_change_refcomp(formation_id: int):
|
|
|
|
"""Tente de changer le ref. de comp. de la formation"""
|
|
|
|
formation = Formation.get_formation(formation_id)
|
|
|
|
ref_comp: ApcReferentielCompetences = formation.referentiel_competence
|
|
|
|
if ref_comp is None:
|
|
|
|
raise ScoNoReferentielCompetences(formation=formation)
|
|
|
|
# Autres référentiels "équivalents" pour proposer de changer les formations:
|
|
|
|
referentiels_equivalents = ref_comp.equivalents()
|
|
|
|
form = FormationChangeRefCompForm()
|
|
|
|
form.object_select.choices = [
|
|
|
|
(ref.id, ref.get_title()) for ref in referentiels_equivalents
|
|
|
|
]
|
|
|
|
if request.method == "POST" and form.cancel.data: # cancel button
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.refcomp_show",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
refcomp_id=formation.referentiel_competence.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
try:
|
|
|
|
new_ref_id = int(form.object_select.data)
|
|
|
|
except TypeError as exc:
|
|
|
|
raise ScoValueError("nouveau refcomp id invalide") from exc
|
|
|
|
new_ref = None
|
|
|
|
for ref in referentiels_equivalents:
|
|
|
|
if ref.id == new_ref_id:
|
|
|
|
new_ref = ref
|
|
|
|
break
|
|
|
|
if new_ref is None:
|
|
|
|
raise ScoValueError("nouveau refcomp invalide")
|
|
|
|
change_refcomp.formation_change_referentiel(formation, new_ref)
|
|
|
|
flash("Formation changée de référentiel")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"but/change_refcomp.j2",
|
|
|
|
form=form,
|
|
|
|
formation=formation,
|
|
|
|
referentiels_equivalents=referentiels_equivalents,
|
|
|
|
title="Changer de référentiel de compétences",
|
|
|
|
)
|