from datetime import date, timedelta from colloscope.models import * from fpdf import FPDF class PDF(FPDF): def liste_eleves(self, periode): classe = periode.classe etudiants = Etudiant.objects.filter(classe=classe) with self.table( align="RIGHT", col_widths=(4, 3, 1, 1, 1, 1), width=80, line_height=3) as table: header = table.row() for th in ("Nom", "Prénom", "Grp.", "TD", "LV1", "LV2"): header.cell(th) for etu in etudiants: row = table.row() row.cell(etu.nom.upper()) # Nom row.cell(etu.prenom) # Prénom row.cell(etu.groupe_de_colle(periode).libelle) # Groupe row.cell("??") #row.cell(etu.groupe_du_critere(periode, "TD")) # TD row.cell("??") # LV1 row.cell("??") # LV2 def table_colloscope(self, periode, heading=True, est_colle=True): semaines = periode.range_semaines() lundis = [ periode.classe.date_debut_sem(n) for n in semaines ] creneaux = Creneau.objects.filter(periode=periode, est_colle=est_colle) jours = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"] with self.table( align="LEFT", width=190, line_height=3, col_widths=(2, 1, 1, 3, 1, *(1,)*len(semaines)), num_heading_rows=2 if heading else 0, first_row_as_headings=heading) as table: if heading: header = table.row() for th in ("Matière", "Jour", "Heure", "Colleur", "Salle"): header.cell(th, align="CENTER", rowspan=2) for sem in semaines: header.cell(str(sem), align="CENTER") header2 = table.row() for lundi in lundis: header2.cell(lundi.strftime("%d/%m/%y"), align="CENTER") for i, c in enumerate(creneaux): matiere = c.matiere jour = c.jour heure = c.heure colleur = c.colleur salle = c.salle row = table.row() row.cell(matiere.libelle) row.cell(jours[jour]) row.cell(heure.strftime("%H:%M")) row.cell("{} {}".format("M." if colleur.civilite=="M" else "Mme", colleur.nom.upper())) row.cell(salle) for s in semaines: if Rotation.objects.filter(creneau=c, semaine=s).exists(): r = Rotation.objects.get(creneau=c, semaine=s) groupes = r.groupes content = ", ".join(g.libelle for g in groupes.all()) with self.local_context(fill_color=(255, 100, 100) if r.est_modifiee() else None): row.cell(content, align="CENTER") else: row.cell() def generate(periode): classe = periode.classe pdf = PDF(orientation="landscape", format="a4") pdf.set_font("helvetica", size=6) titre = f"Colloscope {classe.libelle} {periode.libelle}" pdf.set_title(titre) pdf.set_author("projet colloscope") pdf.set_author("projet colloscope") pdf.add_page() pdf.cell(text=titre, center=True, border=1, h=5) base_y = pdf.t_margin + 10 pdf.set_y(base_y) pdf.liste_eleves(periode) pdf.set_y(base_y) pdf.table_colloscope(periode) pdf.y += 3 pdf.table_colloscope(periode, heading=False, est_colle=False) pdf.output("test.pdf") return pdf def main(): classe = Classe.objects.get(id=1) periode = Periode.objects.get(classe=classe, libelle="Semestre 5/2") return generate(periode) if __name__ == "__main__": main()