improved excel formatting

This commit is contained in:
2025-11-23 12:32:12 +01:00
parent cbdf49adfd
commit bb634d28ed
2 changed files with 25 additions and 19 deletions

View File

@@ -10,7 +10,7 @@ from typing import List
from ..models.weekpref import WeekPreference from ..models.weekpref import WeekPreference
from ..models.missions import Training from ..models.missions import Training
from ..models.hourbuildings import HourBuilding, HourBuildingLegFlight, HourBuildingLegStop from ..models.hourbuildings import HourBuilding, HourBuildingLegFlight, HourBuildingLegStop, HourBuildingLegBase
def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse: def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) -> HttpResponse:
@@ -41,7 +41,7 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
# Header titles # Header titles
days = [f"{datetime.strptime(f"{year} {week} {x}", "%G %V %u").strftime("%A")} {datetime.strptime(f"{year} {week} {x}", "%G %V %u").day}" for x in range(1,8)] days = [f"{datetime.strptime(f"{year} {week} {x}", "%G %V %u").strftime("%A")} {datetime.strptime(f"{year} {week} {x}", "%G %V %u").day}" for x in range(1,8)]
headers = ["Week", "Student", "Course", *days, "Cell.", "Mail", "Notes"] headers = ["Week", "Student", "Course", *days, "Notes", "Cell.", "Mail"]
# Header fields positions # Header fields positions
week_index: int = headers.index("Week") + 1 week_index: int = headers.index("Week") + 1
@@ -60,6 +60,8 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
# Cell styles # Cell styles
border_thick: Side = Side(style='thick', color='000000') border_thick: Side = Side(style='thick', color='000000')
border_bottom: Border = Border(bottom=border_thick) border_bottom: Border = Border(bottom=border_thick)
border_left: Border = Border(left=border_thick)
border_right: Border = Border(right=border_thick)
border_all: Border = Border(bottom=border_thick, top=border_thick, left=border_thick, right=None) border_all: Border = Border(bottom=border_thick, top=border_thick, left=border_thick, right=None)
# Scrittura header # Scrittura header
@@ -102,12 +104,11 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
mission_name if t.sunday else "" mission_name if t.sunday else ""
] ]
mission_notes = t.notes if t.notes else "--" mission_notes = t.notes if t.notes else "--"
mission_data.append([str(q.week), *student_data, *mission_days, student_phone, student_email, mission_notes]) mission_data.append([str(q.week), *student_data, *mission_days, mission_notes, student_phone, student_email, ])
# Fill HourBuilding rows # Fill HourBuilding rows
hb_name: str hb_name: str
hb_days: List[str] hb_days: List[str]
hb_notes: str
hb_data: List[List[str]] = [] hb_data: List[List[str]] = []
for h in HourBuilding.objects.filter(weekpref = q.id): for h in HourBuilding.objects.filter(weekpref = q.id):
hb_name = f"HB-{h.aircraft}\nVedi Note ->" hb_name = f"HB-{h.aircraft}\nVedi Note ->"
@@ -120,12 +121,14 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
hb_name if h.saturday else "", hb_name if h.saturday else "",
hb_name if h.sunday else "" hb_name if h.sunday else ""
] ]
hb_notes = f"{h.notes}\n----\n" if h.notes else "" hb_notes: List[str] = [f"{h.notes}", "---"] if h.notes else []
hb_legs = HourBuildingLegFlight.objects.filter(hb_id = h.id) hb_legs_all = HourBuildingLegBase.objects.filter(hb_id = h.id)
#for hh in hb_legs: for hh in hb_legs_all:
# hb_notes += f"{hh.departure} -> {hh.destination} [{hh.time}]\n" if not hh.stop else f"STOP at {hh.departure} [{hh.time}]\n" if isinstance(hh, HourBuildingLegFlight):
hb_notes.strip('\n') hb_notes.append(f"{hh.departure} -> {hh.destination} [{hh.time}]")
hb_data.append([str(q.week), *student_data, *hb_days, str(q.student.phone), q.student.email, hb_notes]) elif isinstance(hh, HourBuildingLegStop):
hb_notes.append(f"STOP [{hh.time}] {"Refuel" if hh.refuel else ""}" )
hb_data.append([str(q.week), *student_data, *hb_days, "\n".join(hb_notes), str(q.student.phone), q.student.email])
# Build rows for table # Build rows for table
all_data: List[List[str]] = mission_data + hb_data all_data: List[List[str]] = mission_data + hb_data
@@ -164,9 +167,16 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
# End week preferences for this student # End week preferences for this student
student_end: int = row + row_offset -1 student_end: int = row + row_offset -1
# Add thick border to the last cell row of this student
for i in range(course_index, len(all_data[0])+1):
ws.cell(row=student_end, column=i).border = border_bottom
if i == len(all_data[0]):
for j in range(student_start, student_end + 1):
ws.cell(row=j, column=i).border += border_right
# Merge Week, thick border # Merge Week, thick border
# ws.cell(row=student_start, column=week_index).border = border_all #ws.cell(row=student_start, column=week_index).border = border_bottom
# ws.merge_cells(start_row=student_start, end_row=student_end, start_column=week_index, end_column=week_index) #ws.merge_cells(start_row=student_start, end_row=student_end, start_column=week_index, end_column=week_index)
# Merge Name, thick border # Merge Name, thick border
ws.cell(row=student_start, column=student_index).border = border_all ws.cell(row=student_start, column=student_index).border = border_all
ws.merge_cells(start_row=student_start, end_row=student_end, start_column=student_index, end_column=student_index) ws.merge_cells(start_row=student_start, end_row=student_end, start_column=student_index, end_column=student_index)
@@ -177,10 +187,6 @@ def export_selected(request: HttpRequest, queryset: QuerySet[WeekPreference]) ->
# Merge Mail # Merge Mail
ws.merge_cells(start_row=student_start, end_row=student_end, start_column=mail_index, end_column=mail_index) ws.merge_cells(start_row=student_start, end_row=student_end, start_column=mail_index, end_column=mail_index)
# Add thick border to the last cell row of this student
for i in range(course_index, len(all_data[0])+1):
ws.cell(row=student_end, column=i).border = border_bottom
# Keep the largest column # Keep the largest column
for column_cells in ws.columns: for column_cells in ws.columns:
length: int = max(len(str(cell.value)) for cell in column_cells) length: int = max(len(str(cell.value)) for cell in column_cells)

View File

@@ -114,14 +114,14 @@ class HourBuildingLegFlight(HourBuildingLegBase):
verbose_name = "Flight leg" verbose_name = "Flight leg"
verbose_name_plural = "Flight legs" verbose_name_plural = "Flight legs"
def __str__(self):
return f"{self.departure} -> {self.destination}"
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.departure = self.departure.upper().strip() self.departure = self.departure.upper().strip()
self.destination = self.destination.upper().strip() self.destination = self.destination.upper().strip()
super().save(*args, **kwargs) super().save(*args, **kwargs)
def __str__(self):
return f"{self.departure} -> {self.destination}"
class HourBuildingLegStop(HourBuildingLegBase): class HourBuildingLegStop(HourBuildingLegBase):
refuel = models.BooleanField( refuel = models.BooleanField(