138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
from datetime import date, timedelta
|
|
|
|
from django.shortcuts import redirect
|
|
from django.http import HttpResponse
|
|
from fpdf import FPDF
|
|
|
|
from colloscope.models import *
|
|
|
|
|
|
class PDF(FPDF):
|
|
def liste_eleves(self, term):
|
|
cls = term.cls
|
|
students = Student.objects.filter(cls=cls)
|
|
|
|
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 students:
|
|
row = table.row()
|
|
row.cell(etu.last_name.upper()) # Nom
|
|
row.cell(etu.first_name) # Prénom
|
|
row.cell(etu.colle_group(term).description) # Groupe
|
|
#row.cell(etu.group_of_type(term, "td").description)
|
|
#row.cell("??") # LV1
|
|
#row.cell("??") # LV2
|
|
|
|
|
|
def table_colloscope(self, term, heading=True, type="colle"):
|
|
weeks = term.range_weeks()
|
|
lundis = [term.cls.week_beginning_date(n) for n in weeks]
|
|
slots = Slot.objects.filter(term=term, type__description=type)
|
|
weekdays = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"]
|
|
|
|
with self.table(
|
|
align="LEFT",
|
|
width=270,
|
|
line_height=3,
|
|
col_widths=(1.2, 1.2, 0.8, 2.4, 1, *(1,)*len(weeks)),
|
|
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 weeks:
|
|
header.cell(str(sem), align="CENTER")
|
|
|
|
header2 = table.row()
|
|
for lundi in lundis:
|
|
header2.cell(lundi.strftime("%d/%m"), align="CENTER")
|
|
|
|
|
|
for i, c in enumerate(slots):
|
|
subject = c.subject
|
|
day = c.day
|
|
time = c.time
|
|
colleur = c.colleur
|
|
room = c.room
|
|
|
|
row = table.row()
|
|
row.cell(subject.description)
|
|
row.cell(weekdays[day])
|
|
row.cell(time.strftime("%H:%M"))
|
|
row.cell("{} {}".format("M." if colleur.gender=="M" else "Mme", colleur.name.upper()))
|
|
row.cell(room)
|
|
|
|
for s in weeks:
|
|
lundi = term.cls.week_beginning_date(s)
|
|
|
|
if Colle.objects.filter(slot=c, datetime__gte=lundi, datetime__lt=lundi + timedelta(weeks=1)).exists():
|
|
r = Colle.objects.get(slot=c, datetime__gte=lundi, datetime__lt=lundi + timedelta(weeks=1))
|
|
groups = r.groups
|
|
content = ", ".join(g.description for g in groups.all())
|
|
|
|
with self.local_context(fill_color=(255, 100, 100) if r.is_edited() else None):
|
|
row.cell(content, align="CENTER")
|
|
else:
|
|
row.cell()
|
|
|
|
def generate(term):
|
|
pdf = PDF(orientation="landscape", format="a4")
|
|
pdf.set_font("helvetica", size=6)
|
|
|
|
titre = f"Colloscope {term.cls.description} {term.description}"
|
|
|
|
pdf.set_title(titre)
|
|
pdf.set_author("colles.mp2i-vms.fr")
|
|
|
|
pdf.add_page()
|
|
pdf.cell(text=titre, center=True, border=1, h=5)
|
|
pdf.set_line_width(0.1)
|
|
base_y = pdf.t_margin + 10
|
|
pdf.set_y(base_y)
|
|
#pdf.liste_eleves(term)
|
|
pdf.set_y(base_y)
|
|
pdf.table_colloscope(term)
|
|
pdf.y += 3
|
|
pdf.table_colloscope(term, heading=False, type="td")
|
|
|
|
return pdf
|
|
|
|
|
|
def handle(request):
|
|
try:
|
|
student = Profile.from_request(
|
|
request,
|
|
preprocess=lambda query: query \
|
|
.select_related("student__cls") \
|
|
.prefetch_related("student__cls__term_set")
|
|
)
|
|
except ValueError:
|
|
return redirect("colloscope.select_profile")
|
|
|
|
if not isinstance(student, Student):
|
|
return HttpResponse("pas encore supporté")
|
|
|
|
|
|
term_str = request.GET.get("term")
|
|
if term_str is None:
|
|
term = student.cls.current_term()
|
|
else:
|
|
term = Term.objects.get(id=int(term_str), cls=student.cls)
|
|
|
|
return generate(term)
|
|
|
|
|
|
def main():
|
|
term = Term.objects.get(id=3)
|
|
return generate(term)
|