ScoDoc/app/scodoc/sco_prepajury.py

343 lines
12 KiB
Python
Raw Permalink Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2023-12-31 23:04:06 +01:00
# Copyright (c) 1999 - 2024 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
#
##############################################################################
"""Feuille excel pour préparation des jurys classiques (non BUT)
2020-09-26 16:19:37 +02:00
"""
import collections
2021-02-04 20:02:44 +01:00
import time
2020-09-26 16:19:37 +02:00
from openpyxl.styles.numbers import FORMAT_NUMBER_00
2022-02-13 13:58:09 +01:00
from flask import flash
from flask import request
from flask_login import current_user
2022-02-13 13:58:09 +01:00
from app.comp import res_sem
from app.comp.res_compat import NotesTableCompat
2022-07-07 16:24:52 +02:00
from app.models import FormSemestre, Identite, ScolarAutorisationInscription
2023-06-30 15:34:50 +02:00
from app.scodoc import sco_assiduites
from app.scodoc import codes_cursus
from app.scodoc import sco_groups
2022-02-13 13:58:09 +01:00
from app.scodoc import sco_etud
from app.scodoc import sco_excel
from app.scodoc import sco_formsemestre
2022-07-07 16:24:52 +02:00
from app.scodoc import sco_cursus
from app.scodoc import sco_preferences
2022-02-13 13:58:09 +01:00
import app.scodoc.sco_utils as scu
import sco_version
2020-09-26 16:19:37 +02:00
def feuille_preparation_jury(formsemestre_id):
"""Feuille excel pour préparation des jurys classiques.
Non adaptée pour le BUT.
"""
formsemestre = FormSemestre.get_formsemestre(formsemestre_id)
2022-02-13 13:58:09 +01:00
nt: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre)
etuds = nt.get_inscrits(order_by="moy") # tri par moy gen
2021-08-19 10:28:35 +02:00
sem = sco_formsemestre.get_formsemestre(formsemestre_id)
2020-09-26 16:19:37 +02:00
2021-08-19 10:28:35 +02:00
etud_groups = sco_groups.formsemestre_get_etud_groupnames(formsemestre_id)
main_partition_id = sco_groups.formsemestre_get_main_partition(formsemestre_id)[
"partition_id"
]
2020-09-26 16:19:37 +02:00
prev_moy_ue = collections.defaultdict(dict) # ue_code_s : { etudid : moy ue }
2020-09-26 16:19:37 +02:00
prev_ue_acro = {} # ue_code_s : acronyme (à afficher)
prev_moy = {} # moyennes gen sem prec
moy_ue = collections.defaultdict(dict) # ue_acro : moyennes { etudid : moy ue }
ue_acro = {} # ue_code_s : acronyme (à afficher)
2020-09-26 16:19:37 +02:00
moy = {} # moyennes gen
moy_inter = {} # moyenne gen. sur les 2 derniers semestres
code = {} # decision existantes s'il y en a
autorisations = {}
prev_code = {} # decisions sem prec
assidu = {}
parcours = {} # etudid : parcours, sous la forme S1, S2, S2, S3
groupestd = {} # etudid : nom groupe principal
nbabs = {}
nbabsjust = {}
2022-02-13 13:58:09 +01:00
for etud in etuds:
2022-07-07 16:24:52 +02:00
Se = sco_cursus.get_situation_etud_cursus(
2022-02-13 13:58:09 +01:00
etud.to_dict_scodoc7(), formsemestre_id
)
2020-09-26 16:19:37 +02:00
if Se.prev:
2022-02-13 13:58:09 +01:00
formsemestre_prev = FormSemestre.query.get_or_404(
Se.prev["formsemestre_id"]
)
ntp: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre_prev)
2021-12-24 00:08:25 +01:00
for ue in ntp.get_ues_stat_dict(filter_sport=True):
2022-02-13 13:58:09 +01:00
ue_status = ntp.get_etud_ue_status(etud.id, ue["ue_id"])
2020-09-26 16:19:37 +02:00
ue_code_s = (
ue["ue_code"] + "_%s" % ntp.sem["semestre_id"]
) # code indentifiant l'UE
2022-02-13 13:58:09 +01:00
prev_moy_ue[ue_code_s][etud.id] = ue_status["moy"] if ue_status else ""
2020-09-26 16:19:37 +02:00
prev_ue_acro[ue_code_s] = (ue["numero"], ue["acronyme"], ue["titre"])
2022-02-13 13:58:09 +01:00
prev_moy[etud.id] = ntp.get_etud_moy_gen(etud.id)
prev_decision = ntp.get_etud_decision_sem(etud.id)
2020-09-26 16:19:37 +02:00
if prev_decision:
2022-02-13 13:58:09 +01:00
prev_code[etud.id] = prev_decision["code"]
2020-09-26 16:19:37 +02:00
if prev_decision["compense_formsemestre_id"]:
2022-02-13 13:58:09 +01:00
prev_code[etud.id] += "+" # indique qu'il a servi a compenser
2020-09-26 16:19:37 +02:00
2022-02-13 13:58:09 +01:00
moy[etud.id] = nt.get_etud_moy_gen(etud.id)
2021-12-24 00:08:25 +01:00
for ue in nt.get_ues_stat_dict(filter_sport=True):
2022-02-13 13:58:09 +01:00
ue_status = nt.get_etud_ue_status(etud.id, ue["ue_id"])
ue_code_s = f'{ue["ue_code"]}_{nt.sem["semestre_id"]}'
2022-02-13 13:58:09 +01:00
moy_ue[ue_code_s][etud.id] = ue_status["moy"] if ue_status else ""
2020-09-26 16:19:37 +02:00
ue_acro[ue_code_s] = (ue["numero"], ue["acronyme"], ue["titre"])
if Se.prev:
try:
2022-02-13 13:58:09 +01:00
moy_inter[etud.id] = (moy[etud.id] + prev_moy[etud.id]) / 2.0
except (KeyError, TypeError):
2020-09-26 16:19:37 +02:00
pass
2022-02-13 13:58:09 +01:00
decision = nt.get_etud_decision_sem(etud.id)
2020-09-26 16:19:37 +02:00
if decision:
2022-02-13 13:58:09 +01:00
code[etud.id] = decision["code"]
2020-09-26 16:19:37 +02:00
if decision["compense_formsemestre_id"]:
2022-02-13 13:58:09 +01:00
code[etud.id] += "+" # indique qu'il a servi a compenser
assidu[etud.id] = {False: "Non", True: "Oui"}.get(decision["assidu"], "")
2022-07-07 16:24:52 +02:00
autorisations_etud = ScolarAutorisationInscription.query.filter_by(
2022-07-07 16:24:52 +02:00
etudid=etud.id, origin_formsemestre_id=formsemestre_id
).all()
autorisations[etud.id] = ", ".join(
[f"S{x.semestre_id}" for x in autorisations_etud]
)
2020-09-26 16:19:37 +02:00
# parcours:
parcours[etud.id] = Se.get_cursus_descr()
2020-09-26 16:19:37 +02:00
# groupe principal (td)
2022-02-13 13:58:09 +01:00
groupestd[etud.id] = ""
for s in Se.etud["sems"]:
2020-09-26 16:19:37 +02:00
if s["formsemestre_id"] == formsemestre_id:
2022-02-13 13:58:09 +01:00
groupestd[etud.id] = etud_groups.get(etud.id, {}).get(
2020-09-26 16:19:37 +02:00
main_partition_id, ""
)
# absences:
_, nbabsjust[etud.id], nbabs[etud.id] = sco_assiduites.get_assiduites_count(
etud.id, sem
)
2020-09-26 16:19:37 +02:00
# Codes des UE "semestre précédent":
2021-07-09 17:47:06 +02:00
ue_prev_codes = list(prev_moy_ue.keys())
2020-09-26 16:19:37 +02:00
ue_prev_codes.sort(
2021-07-09 23:19:30 +02:00
key=lambda x, prev_ue_acro=prev_ue_acro: prev_ue_acro[ # pylint: disable=undefined-variable
x
]
2020-09-26 16:19:37 +02:00
)
# Codes des UE "semestre courant":
2021-07-09 17:47:06 +02:00
ue_codes = list(moy_ue.keys())
2021-02-01 23:54:46 +01:00
ue_codes.sort(
2021-07-09 23:19:30 +02:00
key=lambda x, ue_acro=ue_acro: ue_acro[x] # pylint: disable=undefined-variable
2021-02-01 23:54:46 +01:00
)
2020-09-26 16:19:37 +02:00
sid = sem["semestre_id"]
sn = sp = ""
if sid >= 0:
sn = f"S{sid}"
2020-09-26 16:19:37 +02:00
if prev_moy: # si qq chose dans precedent
sp = f"S{sid - 1}"
2020-09-26 16:19:37 +02:00
sheet = sco_excel.ScoExcelSheet(sheet_name=f"Prepa Jury {sn}")
# génération des styles
style_bold = sco_excel.excel_make_style(size=10, bold=True)
style_center = sco_excel.excel_make_style(halign="center")
style_boldcenter = sco_excel.excel_make_style(bold=True, halign="center")
style_moy = sco_excel.excel_make_style(
bold=True, halign="center", bgcolor=sco_excel.COLORS.LIGHT_YELLOW
)
style_note = sco_excel.excel_make_style(
halign="right", number_format=FORMAT_NUMBER_00
)
style_note_bold = sco_excel.excel_make_style(
halign="right", bold=True, number_format="General"
)
# Première ligne
sheet.append_single_cell_row(
"Feuille préparation Jury %s" % scu.unescape_html(sem["titreannee"]), style_bold
)
sheet.append_blank_row()
2020-09-26 16:19:37 +02:00
# Ligne de titre
2020-09-26 16:19:37 +02:00
titles = ["Rang"]
if sco_preferences.get_preference("prepa_jury_nip"):
2020-09-26 16:19:37 +02:00
titles.append("NIP")
if sco_preferences.get_preference("prepa_jury_ine"):
2020-09-26 16:19:37 +02:00
titles.append("INE")
titles += [
"etudid",
"Civ.",
"Nom",
"Prénom",
"Naissance",
"Bac",
"Spe",
"Rg Adm",
"Parcours",
"Groupe",
]
if prev_moy: # si qq chose dans precedent
titles += [prev_ue_acro[x][1] for x in ue_prev_codes] + [
f"Moy {sp}",
f"Décision {sp}",
2020-09-26 16:19:37 +02:00
]
titles += [ue_acro[x][1] for x in ue_codes] + [f"Moy {sn}"]
2020-09-26 16:19:37 +02:00
if moy_inter:
titles += [f"Moy {sp}-{sn}"]
2020-09-26 16:19:37 +02:00
titles += ["Abs", "Abs Injust."]
if code:
titles.append("Proposit. {sn}")
2020-09-26 16:19:37 +02:00
if autorisations:
titles.append("Autorisations")
# titles.append('Assidu')
sheet.append_row(sheet.make_row(titles, style_boldcenter))
# if prev_moy:
# tit_prev_moy = "Moy " + sp
# # col_prev_moy = titles.index(tit_prev_moy)
# tit_moy = "Moy " + sn
# col_moy = titles.index(tit_moy)
# col_abs = titles.index("Abs")
2020-09-26 16:19:37 +02:00
def fmt(x):
"reduit les notes a deux chiffres"
2021-02-04 20:02:44 +01:00
x = scu.fmt_note(x, keep_numeric=False)
2020-09-26 16:19:37 +02:00
try:
return float(x)
except:
return x
i = 1 # numero etudiant
2022-02-13 13:58:09 +01:00
for etud in etuds:
cells = []
cells.append(sheet.make_cell(str(i)))
if sco_preferences.get_preference("prepa_jury_nip"):
cells.append(sheet.make_cell(etud.code_nip))
if sco_preferences.get_preference("prepa_jury_ine"):
cells.append(sheet.make_cell(etud.code_ine))
cells += sheet.make_row(
[
2022-02-13 13:58:09 +01:00
etud.id,
etud.civilite_str,
scu.format_nom(etud.nom),
scu.format_prenom(etud.prenom),
2022-02-13 13:58:09 +01:00
etud.date_naissance,
etud.admission.bac if etud.admission else "",
etud.admission.specialite if etud.admission else "",
etud.admission.classement if etud.admission else "",
2022-02-13 13:58:09 +01:00
parcours[etud.id],
groupestd[etud.id],
]
)
co = len(cells)
2020-09-26 16:19:37 +02:00
if prev_moy:
for ue_acro in ue_prev_codes:
cells.append(
sheet.make_cell(
2022-02-13 13:58:09 +01:00
fmt(prev_moy_ue.get(ue_acro, {}).get(etud.id, "")), style_note
)
)
2020-09-26 16:19:37 +02:00
co += 1
cells.append(
sheet.make_cell(fmt(prev_moy.get(etud.id, "")), style_bold)
) # moy gen prev
cells.append(
sheet.make_cell(fmt(prev_code.get(etud.id, "")), style_moy)
) # decision prev
2020-09-26 16:19:37 +02:00
co += 2
for ue_acro in ue_codes:
cells.append(
sheet.make_cell(
fmt(moy_ue.get(ue_acro, {}).get(etud.id, "")), style_note
)
)
2020-09-26 16:19:37 +02:00
co += 1
2022-02-13 13:58:09 +01:00
cells.append(
sheet.make_cell(fmt(moy.get(etud.id, "")), style_note_bold)
2022-02-13 13:58:09 +01:00
) # moy gen
2020-09-26 16:19:37 +02:00
co += 1
if moy_inter:
cells.append(sheet.make_cell(fmt(moy_inter.get(etud.id, "")), style_note))
2023-02-21 21:54:23 +01:00
cells.append(sheet.make_cell(nbabs.get(etud.id, ""), style_center))
cells.append(sheet.make_cell(nbabsjust.get(etud.id, ""), style_center))
2020-09-26 16:19:37 +02:00
if code:
cells.append(sheet.make_cell(code.get(etud.id, ""), style_moy))
cells.append(sheet.make_cell(autorisations.get(etud.id, ""), style_moy))
2022-02-13 13:58:09 +01:00
# l.append(assidu.get(etud.id, ''))
sheet.append_row(cells)
2020-09-26 16:19:37 +02:00
i += 1
#
sheet.append_blank_row()
2020-09-26 16:19:37 +02:00
# Explications des codes
codes = list(codes_cursus.CODES_EXPL.keys())
2020-09-26 16:19:37 +02:00
codes.sort()
sheet.append_single_cell_row("Explication des codes")
2020-09-26 16:19:37 +02:00
for code in codes:
sheet.append_row(
sheet.make_row(["", "", "", code, codes_cursus.CODES_EXPL[code]])
)
sheet.append_row(
sheet.make_row(
[
"",
"",
"",
"ADM+",
"indique que le semestre a déjà servi à en compenser un autre",
]
)
2020-09-26 16:19:37 +02:00
)
# UE : Correspondances acronyme et titre complet
sheet.append_blank_row()
sheet.append_single_cell_row("Titre des UE")
2020-09-26 16:19:37 +02:00
if prev_moy:
2021-12-24 00:08:25 +01:00
for ue in ntp.get_ues_stat_dict(filter_sport=True):
sheet.append_row(sheet.make_row(["", "", "", ue["acronyme"], ue["titre"]]))
2021-12-24 00:08:25 +01:00
for ue in nt.get_ues_stat_dict(filter_sport=True):
sheet.append_row(sheet.make_row(["", "", "", ue["acronyme"], ue["titre"]]))
2020-09-26 16:19:37 +02:00
#
sheet.append_blank_row()
sheet.append_single_cell_row(
"Préparé par %s le %s sur %s pour %s"
% (
2021-08-21 17:07:44 +02:00
sco_version.SCONAME,
time.strftime(scu.DATE_FMT),
request.url_root,
current_user,
)
2020-09-26 16:19:37 +02:00
)
xls = sheet.generate()
2022-02-13 13:58:09 +01:00
flash("Feuille préparation jury générée")
2021-09-18 10:11:46 +02:00
return scu.send_file(
xls,
2021-09-25 09:07:05 +02:00
f"PrepaJury{sn}",
2021-09-18 10:11:46 +02:00
scu.XLSX_SUFFIX,
mime=scu.XLSX_MIMETYPE,
2021-09-18 10:11:46 +02:00
)