Files
timetable/templates/department-wise/department_wise_template.py
Talhadeveloperr 868bcbe175 more complex
2026-09-09 18:29:08 +05:00

55 lines
2.2 KiB
Python

"""Renders one timetable grid per department, aggregating all its batches."""
import sys
import os
_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
sys.path.insert(0, os.path.join(_ROOT, "app"))
sys.path.insert(0, os.path.join(_ROOT, "controller"))
from xlsx_grid_helper import build_grid_sheet, new_workbook, save_workbook # noqa: E402
from template_controller import DISPLAY_ROWS # noqa: E402
def generate_department_wise_templates(sessions, data, output_dir):
departments = sorted({b["department"] for b in data["batches"]})
written = []
for department in departments:
dept_batch_codes = {b["batch_code"] for b in data["batches"] if b["department"] == department}
dept_sessions = [s for s in sessions if any(bc in dept_batch_codes for bc in s["batch_codes"])]
by_day_slot = {}
for s in dept_sessions:
for slot_index in s["slot_indices"]:
by_day_slot.setdefault((s["day"], slot_index, s["duration_minutes"]), []).append(s)
def cell_text(day, slot_index, duration_minutes, by_day_slot=by_day_slot):
entries = by_day_slot.get((day, slot_index, duration_minutes))
if not entries:
return ""
lines = []
for s in entries:
tag = " (Lab)" if s["course_type"] == "lab" else (" (General)" if s["is_general"] else "")
batches = "+".join(s["batch_codes"])
lines.append(f'{s["course_name"]}{tag} [Sec {s["section_id"]}] [{batches}]\n{s["instructor_name"]} @ {s["room_id"]}')
return "\n---\n".join(lines)
# for fill-color purposes just use the first session in each cell
first_by_day_slot = {k: v[0] for k, v in by_day_slot.items()}
wb = new_workbook()
ws = wb.active
build_grid_sheet(
ws,
title=f"Department: {department}",
subtitle="All batches / programs",
display_rows=DISPLAY_ROWS,
sessions_by_cell=first_by_day_slot,
cell_text_fn=cell_text,
)
path = save_workbook(wb, output_dir, f"{department}.xlsx")
written.append(path)
return written