from app.but.prepajury_cells import Cell, base_style from app.but.prepajury_xl import excel_make_style from app.models import ApcCompetence, ApcParcours from app.scodoc.sco_excel import ScoExcelBook, ScoExcelSheet def parite(semestre_idx): return (semestre_idx + 1) % 2 listeAnnees = ["BUT1", "BUT2", "BUT3"] class _Header: def __init__(self): self.lines: list[list[Cell]] = [] self.presets = [None, None, None, None] self.styles = [None, None, None, None] def setLabelOnce(self, rang: int, label: str): self.presets[rang] = label def setStyles(self, rangs: list[int], style): for rang in rangs: self.styles[rang] = style def add_column( self, label0=None, label1=None, label2=None, label3=None, labels: list[str] = None, ): if labels is None: labels = [label0, label1, label2, label3] cells: list(Cell) = [ Cell(preset or label, style=style) for label, preset, style in zip(labels, self.presets, self.styles) ] self.presets = [None, None, None, None] self.lines.append(cells) def extend(self, header): self.lines.extend(header.lines) def write(self, worksheet: ScoExcelSheet): row1 = [] row2 = [] row3 = [] row4 = [] for items in self.lines: row1.append(items[0].make_cell(worksheet)) row2.append(items[1].make_cell(worksheet)) row3.append(items[2].make_cell(worksheet)) row4.append(items[3].make_cell(worksheet)) worksheet.append_row(row1) worksheet.append_row(row2) worksheet.append_row(row3) worksheet.append_row(row4) class NiveauDesc: def __init__(self, scodocNiveau): self.fromScodoc = scodocNiveau self.ues = {} self.ue = [None, None] for scodocUe in scodocNiveau.ues: self.ues[(scodocUe.formation_id, scodocUe.semestre_idx)] = scodocUe if not scodocUe.is_external: self.ue[parite(scodocUe.semestre_idx)] = scodocUe def generate_header(self, header): header.setLabelOnce(1, self.fromScodoc.competence.titre) rcue_style = excel_make_style(base_style) ue_style = excel_make_style(base_style) header.setStyles([1, 2, 3], rcue_style) header.setStyles([2, 3], ue_style) for ue in self.ue: if ue is None: header.setLabelOnce(2, "XXX") header.add_column(label3="Note") header.add_column(label3="Res.") else: header.setLabelOnce(2, ue.acronyme) header.add_column(label3="Note") header.add_column(label3="Res.") header.setStyles([1, 2, 3], rcue_style) header.add_column(label2="RCUE", label3="Note") header.add_column(label3="Res.") return header def get_ues(self, etudiant): """get list of candidates UEs for Niveau""" ues = [None, None] for inscription in etudiant.cursus.inscriptions: formation_id = inscription.formsemestre.formation_id semestre_idx = inscription.formsemestre.semestre_id if (formation_id, semestre_idx) in self.ues: # identifier les ues cocernées ues[semestre_idx % 2] = inscription.formsemestre return ues class CompetenceDesc: def __init__(self, scodocCompetence): self.fromScodoc: ApcCompetence = scodocCompetence self.niveaux = {} for scodocNiveau in scodocCompetence.niveaux.all(): self.niveaux[scodocNiveau.id] = NiveauDesc(scodocNiveau) def getNiveauDesc(self, niveau_id): return self.niveaux[niveau_id] def getNiveaux(self, codeAnnee): niveaux = [] for niveau_id, niveauDesc in self.niveaux.items(): if codeAnnee == niveauDesc.fromScodoc.annee: niveaux.append(niveauDesc) return niveaux class AnneeDesc: def __init__(self, codeAnnee): self.codeAnnee = codeAnnee self.niveaux = {} def addNiveau(self, niveaux): for niveau in niveaux: self.niveaux[niveau.fromScodoc.id] = niveau def generate_blank_niveau(self, header): header.setlabel(1, "-") for _ in range(3): header.add_column() def generate_header(self, header): but_style = excel_make_style(base_style) header.setStyles([0], but_style) for niveau in self.niveaux.values(): niveau.generate_header(header) for i in range(len(self.niveaux), 6): self.generate_blank_niveau(header) header.add_column(label1="Année", label2="Nb", label3="RCUE") header.add_column(label3="Rés.") if self.codeAnnee == "BUT2": header.add_column(label1="DUT", label3="Rés.") if self.codeAnnee == "BUT3": header.add_column(label1="BUT", label3="Rés.") class ParcoursDesc: def __init__(self, formation, scodocParcour: ApcParcours = None): self.fromScodoc: ApcParcours = scodocParcour # None pour le tronc commun 'TC' self.etudiants = [] self.competences = {} self.annees = {} if scodocParcour is None: for ( scodocCompetence ) in formation.referentiel_competence.get_competences_tronc_commun(): self.competences[scodocCompetence.id] = CompetenceDesc(scodocCompetence) else: query = formation.query_competences_parcour(scodocParcour) if not query is None: for scodocCompetence in query.all(): self.competences[scodocCompetence.id] = CompetenceDesc( scodocCompetence ) for codeAnnee in listeAnnees: anneeDesc = AnneeDesc(codeAnnee) for competence_id, competence in self.competences.items(): anneeDesc.addNiveau(competence.getNiveaux(codeAnnee)) self.annees[codeAnnee] = anneeDesc def add_etudiant(self, etudiant): if not etudiant in self.etudiants: self.etudiants.append(etudiant) def getNiveauDesc(self, competence_id, niveau_id): return self.competences[competence_id].getNiveauDesc(niveau_id) def getData(self): data = [] for etudiant in self.etudiants: data.append(etudiant.getData()) return data def append_title_column(self, worksheet, cells, val1, val2, val3, val4): cells1, cells2, cells3, cells4 = cells cells1.append(worksheet.make_cell(val1)) cells2.append(worksheet.make_cell(val2)) cells3.append(worksheet.make_cell(val3)) cells4.append(worksheet.make_cell(val4)) def handle_description(self, header, description, level): title, content_list = description header.setLabelOnce(level, title) if content_list is None: header.add_column() else: for content in content_list: self.handle_description(header, content, level + 1) def generate_etudiant_header(self, header): titles = ( "ETUDIANT", [ ("id", None), ("nip", None), ("Civ", None), ("nom", None), ("prenom", None), ("parcours", None), ("cursus", None), ( "absences", [ ("Tot.", None), ("Non", [("Just.", None)]), ], ), ], ) self.handle_description(header, titles, 0) def generate_header(self, header) -> _Header: self.generate_etudiant_header(header) for codeAnnee in listeAnnees: header.setLabelOnce(0, codeAnnee) self.annees[codeAnnee].generate_header(header) return header def generate(self, workbook: ScoExcelBook): header = _Header() if self.fromScodoc: sheet_name = self.fromScodoc.code else: sheet_name = "TC" worksheet: ScoExcelSheet = workbook.create_sheet(sheet_name) self.generate_header(header).write(worksheet)