MonScoDocEssai/app/scodoc/sco_edit_matiere.py

326 lines
11 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2021-01-01 17:51:08 +01:00
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
2020-09-26 16:19:37 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
"""Ajout/Modification/Supression matieres
(portage from DTML)
"""
2021-08-01 10:16:16 +02:00
import flask
2021-08-09 10:09:04 +02:00
from flask import g, url_for
2021-08-01 10:16:16 +02:00
import app.scodoc.notesdb as ndb
import app.scodoc.sco_utils as scu
2021-08-29 19:57:32 +02:00
from app import log
from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message
from app.scodoc.sco_exceptions import ScoValueError, ScoLockedFormError
from app.scodoc import html_sco_header
2020-09-26 16:19:37 +02:00
2021-06-16 18:18:32 +02:00
_matiereEditor = ndb.EditableTable(
"notes_matieres",
"matiere_id",
("matiere_id", "ue_id", "numero", "titre"),
sortkey="numero",
output_formators={"numero": ndb.int_null_is_zero},
)
2021-08-20 01:09:55 +02:00
def do_matiere_list(*args, **kw):
2021-06-16 18:18:32 +02:00
"list matieres"
cnx = ndb.GetDBConnexion()
return _matiereEditor.list(cnx, *args, **kw)
2021-08-20 01:09:55 +02:00
def do_matiere_edit(*args, **kw):
2021-06-16 18:18:32 +02:00
"edit a matiere"
2021-07-19 19:53:01 +02:00
from app.scodoc import sco_edit_ue
from app.scodoc import sco_edit_formation
2021-06-16 18:18:32 +02:00
cnx = ndb.GetDBConnexion()
# check
2021-08-20 01:09:55 +02:00
mat = do_matiere_list({"matiere_id": args[0]["matiere_id"]})[0]
if matiere_is_locked(mat["matiere_id"]):
2021-06-16 18:18:32 +02:00
raise ScoLockedFormError()
# edit
_matiereEditor.edit(cnx, *args, **kw)
2021-08-20 01:09:55 +02:00
formation_id = sco_edit_ue.do_ue_list({"ue_id": mat["ue_id"]})[0]["formation_id"]
2021-07-19 19:53:01 +02:00
sco_edit_formation.invalidate_sems_in_formation(formation_id)
2021-06-16 18:18:32 +02:00
2021-08-20 01:09:55 +02:00
def do_matiere_create(args):
2021-06-16 18:18:32 +02:00
"create a matiere"
from app.scodoc import sco_edit_ue
from app.scodoc import sco_formations
from app.scodoc import sco_news
2021-06-16 18:18:32 +02:00
cnx = ndb.GetDBConnexion()
# check
2021-08-20 01:09:55 +02:00
ue = sco_edit_ue.do_ue_list({"ue_id": args["ue_id"]})[0]
2021-06-16 18:18:32 +02:00
# create matiere
r = _matiereEditor.create(cnx, args)
# news
2021-08-19 10:28:35 +02:00
F = sco_formations.formation_list(args={"formation_id": ue["formation_id"]})[0]
2021-06-16 18:18:32 +02:00
sco_news.add(
typ=sco_news.NEWS_FORM,
2021-06-16 18:18:32 +02:00
object=ue["formation_id"],
text="Modification de la formation %(acronyme)s" % F,
2021-08-01 10:16:16 +02:00
max_frequency=3,
2021-06-16 18:18:32 +02:00
)
return r
2020-09-26 16:19:37 +02:00
2021-08-20 01:09:55 +02:00
def matiere_create(ue_id=None, REQUEST=None):
2021-01-01 18:40:47 +01:00
"""Creation d'une matiere"""
from app.scodoc import sco_edit_ue
2021-08-20 01:09:55 +02:00
UE = sco_edit_ue.do_ue_list(args={"ue_id": ue_id})[0]
2020-09-26 16:19:37 +02:00
H = [
html_sco_header.sco_header(page_title="Création d'une matière"),
2020-09-26 16:19:37 +02:00
"""<h2>Création d'une matière dans l'UE %(titre)s (%(acronyme)s)</h2>""" % UE,
"""<p class="help">Les matières sont des groupes de modules dans une UE
d'une formation donnée. Les matières servent surtout pour la
présentation (bulletins, etc) mais <em>n'ont pas de rôle dans le calcul
des notes.</em>
</p>
<p class="help">Si votre formation n'utilise pas la notion de
"matières", créez une matière par UE, et donnez lui le même nom que l'UE
(en effet, tout module doit appartenir à une matière).
</p>
<p class="help">Comme les UE, les matières n'ont pas de coefficient
associé.
</p>""",
]
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(
("ue_id", {"input_type": "hidden", "default": ue_id}),
("titre", {"size": 30, "explanation": "nom de la matière."}),
(
"numero",
{
"size": 2,
"explanation": "numéro (1,2,3,4...) pour affichage",
"type": "int",
},
),
),
submitlabel="Créer cette matière",
)
2021-08-09 10:09:04 +02:00
dest_url = url_for(
"notes.ue_list", scodoc_dept=g.scodoc_dept, formation_id=UE["formation_id"]
)
2020-09-26 16:19:37 +02:00
if tf[0] == 0:
return "\n".join(H) + tf[1] + html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
elif tf[0] == -1:
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
else:
# check unicity
2021-08-20 01:09:55 +02:00
mats = do_matiere_list(args={"ue_id": ue_id, "titre": tf[2]["titre"]})
2020-09-26 16:19:37 +02:00
if mats:
return (
"\n".join(H)
+ tf_error_message("Titre de matière déjà existant dans cette UE")
+ tf[1]
+ html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
)
2021-08-20 01:09:55 +02:00
_ = do_matiere_create(tf[2])
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
2021-08-20 01:09:55 +02:00
def do_matiere_delete(oid):
2021-06-16 18:18:32 +02:00
"delete matiere and attached modules"
from app.scodoc import sco_formations
from app.scodoc import sco_edit_ue
from app.scodoc import sco_edit_module
from app.scodoc import sco_news
2021-06-16 18:18:32 +02:00
cnx = ndb.GetDBConnexion()
# check
2021-08-20 01:09:55 +02:00
mat = do_matiere_list({"matiere_id": oid})[0]
ue = sco_edit_ue.do_ue_list({"ue_id": mat["ue_id"]})[0]
locked = matiere_is_locked(mat["matiere_id"])
2021-06-16 18:18:32 +02:00
if locked:
log("do_matiere_delete: mat=%s" % mat)
log("do_matiere_delete: ue=%s" % ue)
log("do_matiere_delete: locked sems: %s" % locked)
raise ScoLockedFormError()
log("do_matiere_delete: matiere_id=%s" % oid)
# delete all modules in this matiere
2021-08-20 01:09:55 +02:00
mods = sco_edit_module.do_module_list({"matiere_id": oid})
2021-06-16 18:18:32 +02:00
for mod in mods:
2021-08-20 01:09:55 +02:00
sco_edit_module.do_module_delete(mod["module_id"])
2021-06-16 18:18:32 +02:00
_matiereEditor.delete(cnx, oid)
# news
2021-08-19 10:28:35 +02:00
F = sco_formations.formation_list(args={"formation_id": ue["formation_id"]})[0]
2021-06-16 18:18:32 +02:00
sco_news.add(
typ=sco_news.NEWS_FORM,
2021-06-16 18:18:32 +02:00
object=ue["formation_id"],
text="Modification de la formation %(acronyme)s" % F,
2021-08-01 10:16:16 +02:00
max_frequency=3,
2021-06-16 18:18:32 +02:00
)
2021-08-20 01:09:55 +02:00
def matiere_delete(matiere_id=None, REQUEST=None):
2020-09-26 16:19:37 +02:00
"""Delete an UE"""
from app.scodoc import sco_edit_ue
2021-08-20 01:09:55 +02:00
M = do_matiere_list(args={"matiere_id": matiere_id})[0]
UE = sco_edit_ue.do_ue_list(args={"ue_id": M["ue_id"]})[0]
2020-09-26 16:19:37 +02:00
H = [
html_sco_header.sco_header(page_title="Suppression d'une matière"),
2020-09-26 16:19:37 +02:00
"<h2>Suppression de la matière %(titre)s" % M,
" dans l'UE (%(acronyme)s))</h2>" % UE,
]
2021-06-15 12:34:33 +02:00
dest_url = scu.NotesURL() + "/ue_list?formation_id=" + str(UE["formation_id"])
2020-09-26 16:19:37 +02:00
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(("matiere_id", {"input_type": "hidden"}),),
initvalues=M,
submitlabel="Confirmer la suppression",
cancelbutton="Annuler",
)
if tf[0] == 0:
return "\n".join(H) + tf[1] + html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
elif tf[0] == -1:
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
else:
2021-08-20 01:09:55 +02:00
do_matiere_delete(matiere_id)
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
2021-08-20 01:09:55 +02:00
def matiere_edit(matiere_id=None, REQUEST=None):
2020-09-26 16:19:37 +02:00
"""Edit matiere"""
from app.scodoc import sco_formations
from app.scodoc import sco_edit_ue
2021-08-20 01:09:55 +02:00
F = do_matiere_list(args={"matiere_id": matiere_id})
2020-09-26 16:19:37 +02:00
if not F:
raise ScoValueError("Matière inexistante !")
F = F[0]
2021-08-20 01:09:55 +02:00
U = sco_edit_ue.do_ue_list(args={"ue_id": F["ue_id"]})
2020-09-26 16:19:37 +02:00
if not F:
raise ScoValueError("UE inexistante !")
U = U[0]
2021-08-19 10:28:35 +02:00
Fo = sco_formations.formation_list(args={"formation_id": U["formation_id"]})[0]
2020-09-26 16:19:37 +02:00
2021-08-20 01:09:55 +02:00
ues = sco_edit_ue.do_ue_list(args={"formation_id": U["formation_id"]})
2020-09-26 16:19:37 +02:00
ue_names = ["%(acronyme)s (%(titre)s)" % u for u in ues]
ue_ids = [u["ue_id"] for u in ues]
H = [
html_sco_header.sco_header(page_title="Modification d'une matière"),
2020-09-26 16:19:37 +02:00
"""<h2>Modification de la matière %(titre)s""" % F,
"""(formation %(acronyme)s, version %(version)s)</h2>""" % Fo,
]
help = """<p class="help">Les matières sont des groupes de modules dans une UE
d'une formation donnée. Les matières servent surtout pour la
présentation (bulletins, etc) mais <em>n'ont pas de rôle dans le calcul
des notes.</em>
</p>
<p class="help">Si votre formation n'utilise pas la notion de
"matières", créez une matière par UE, et donnez lui le même nom que l'UE
(en effet, tout module doit appartenir à une matière).
</p>
<p class="help">Comme les UE, les matières n'ont pas de coefficient
associé.
</p>"""
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(
("matiere_id", {"input_type": "hidden"}),
(
"ue_id",
{"input_type": "menu", "allowed_values": ue_ids, "labels": ue_names},
),
("titre", {"size": 30, "explanation": "nom de cette matière"}),
(
"numero",
{
"size": 2,
"explanation": "numéro (1,2,3,4...) pour affichage",
"type": "int",
},
),
),
initvalues=F,
submitlabel="Modifier les valeurs",
)
2021-08-09 10:09:04 +02:00
dest_url = scu.NotesURL() + "/ue_list?formation_id=" + str(U["formation_id"])
2020-09-26 16:19:37 +02:00
if tf[0] == 0:
return "\n".join(H) + tf[1] + help + html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
elif tf[0] == -1:
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2020-09-26 16:19:37 +02:00
else:
# check unicity
2021-08-20 01:09:55 +02:00
mats = do_matiere_list(args={"ue_id": tf[2]["ue_id"], "titre": tf[2]["titre"]})
2020-09-26 16:19:37 +02:00
if len(mats) > 1 or (len(mats) == 1 and mats[0]["matiere_id"] != matiere_id):
return (
"\n".join(H)
+ tf_error_message("Titre de matière déjà existant dans cette UE")
+ tf[1]
+ html_sco_header.sco_footer()
2020-09-26 16:19:37 +02:00
)
# changement d'UE ?
if tf[2]["ue_id"] != F["ue_id"]:
log("attaching mat %s to new UE %s" % (matiere_id, tf[2]["ue_id"]))
2021-02-03 22:00:41 +01:00
ndb.SimpleQuery(
2020-09-26 16:19:37 +02:00
"UPDATE notes_modules SET ue_id = %(ue_id)s WHERE matiere_id=%(matiere_id)s",
{"ue_id": tf[2]["ue_id"], "matiere_id": matiere_id},
)
2021-08-20 01:09:55 +02:00
do_matiere_edit(tf[2])
2020-09-26 16:19:37 +02:00
2021-07-31 18:01:10 +02:00
return flask.redirect(dest_url)
2021-06-13 19:12:20 +02:00
2021-08-20 01:09:55 +02:00
def matiere_is_locked(matiere_id):
2021-06-13 19:12:20 +02:00
"""True if matiere should not be modified
(contains modules used in a locked formsemestre)
"""
r = ndb.SimpleDictFetch(
"""SELECT ma.id
FROM notes_matieres ma, notes_modules mod, notes_formsemestre sem, notes_moduleimpl mi
WHERE ma.id = mod.matiere_id
AND mi.module_id = mod.id
AND mi.formsemestre_id = sem.id
2021-08-09 10:09:04 +02:00
AND ma.id = %(matiere_id)s
AND sem.etat = false
2021-06-13 19:12:20 +02:00
""",
{"matiere_id": matiere_id},
)
return len(r) > 0