Skip to content

Commit 3c069a8

Browse files
authored
[LIMS-1909] Display more information in session reports (#23)
* Display more information in session report * Provide fallbacks for container names/locations
1 parent 7c8f16b commit 3c069a8

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/scaup/crud/pdf.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def footer(self):
387387
def add_table(
388388
self,
389389
table_contents: Sequence[tuple[str, ...]],
390-
width: int = 100,
390+
width: int | None = 100,
391391
caption: str = "Table",
392392
col_widths: tuple | None = None,
393393
):
@@ -421,36 +421,35 @@ def generate_report(shipment_id: int, token: str):
421421
"Unknown" if not ispyb_session["beamLineOperator"] else ", ".join(ispyb_session["beamLineOperator"])
422422
)
423423

424-
session_table = [
425-
("Start Date", ispyb_session["startDate"]),
426-
("End Date", ispyb_session["endDate"]),
427-
("Local Contact", local_contacts),
428-
("TEM", ispyb_session["beamLineName"]),
429-
("Camera", ""),
430-
("Software", ""),
431-
]
432-
433424
grids = inner_db.session.scalars(
434425
select(Sample)
435-
.join(Sample.container)
426+
.join(Sample.container, isouter=True)
436427
.filter(Sample.shipmentId == shipment_id)
437428
.options(contains_eager(Sample.container))
438429
.order_by(Sample.subLocation.desc(), Sample.containerId, Sample.location)
439430
)
440431

441432
# TODO: rethink this once we're using user-provided templates
442433
grids_table = [
443-
("Gridbox", "Position", "Cassette Slot", "Foil", "Hole", "Comments"),
444-
*[("",) * 6] * 12,
434+
("Puck", "Puck Pos.", "Gridbox", "Gridbox Pos.", "Cassette Slot", "Foil", "Hole", "Comments"),
435+
*[("",) * 8] * 12,
445436
]
446437

447438
current_row = 1
439+
grid_count = 0
448440

449441
for grid in grids:
450-
if not hasattr(grid, "container"):
451-
continue
442+
# Because grids is a generator, we have to count them here. This allows us to
443+
# access parents (and parents of parents) for all grids in a lazy-loading manner
444+
grid_count += 1
445+
puck_name: str = grid.container.parent.name if grid.container and grid.container.parent else "None"
446+
gridbox_name: str = grid.container.name if grid.container else "None"
447+
gridbox_location: str = str(grid.container.location) if grid.container is not None else "None"
448+
452449
grids_table[current_row] = (
453-
str(grid.container.name),
450+
str(puck_name),
451+
str(gridbox_location),
452+
str(gridbox_name),
454453
str(grid.location),
455454
str(grid.subLocation),
456455
str(grid.details["foil"]),
@@ -459,6 +458,16 @@ def generate_report(shipment_id: int, token: str):
459458
)
460459
current_row += 1
461460

461+
session_table = [
462+
("Start Date", ispyb_session["startDate"]),
463+
("End Date", ispyb_session["endDate"]),
464+
("Local Contact", local_contacts),
465+
("TEM", ispyb_session["beamLineName"]),
466+
("Camera", ""),
467+
("Software", ""),
468+
("Number of Grids", str(grid_count)),
469+
]
470+
462471
pre_session = inner_db.session.scalar(select(PreSession).filter(PreSession.shipmentId == shipment_id))
463472

464473
if pre_session is None:
@@ -474,12 +483,16 @@ def generate_report(shipment_id: int, token: str):
474483
pdf.add_page()
475484

476485
pdf.set_xy(x=5, y=20)
477-
pdf.add_table(session_table, width=100, caption="Session")
486+
pdf.add_table(grids_table, width=None, caption="Grids", col_widths=(2, 2, 2, 2, 2, 3, 1, 4))
487+
488+
# There was no good way of fitting all three tables in one page, even if the grids table was as small
489+
# as possible, so we always create a new page
490+
pdf.add_page()
478491

479-
pdf.set_xy(x=110, y=20)
480-
pdf.add_table(grids_table, width=182, caption="Grids", col_widths=(2, 1, 2, 3, 1, 2))
492+
pdf.set_xy(x=5, y=20)
493+
pdf.add_table(session_table, width=100, caption="Session")
481494

482-
pdf.set_xy(x=5, y=70)
495+
pdf.set_xy(x=140, y=20)
483496
pdf.add_table(pre_session_table, width=100, caption="Data Collection Parameters")
484497

485498
headers = {

src/scaup/models/inner_db/tables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ class Container(Base, BaseColumns):
9595
isCurrent: Mapped[bool] = mapped_column(default=False, comment="Whether container position is current")
9696
registeredContainer: Mapped[str | None] = mapped_column()
9797

98+
parent: Mapped[Optional["Container"]] = relationship("Container", back_populates="children", remote_side=[id])
9899
topLevelContainer: Mapped[Optional["TopLevelContainer"]] = relationship(back_populates="children")
99-
children: Mapped[List["Container"] | None] = relationship("Container")
100+
children: Mapped[List["Container"] | None] = relationship("Container", back_populates="parent")
100101
samples: Mapped[List["Sample"] | None] = relationship(back_populates="container")
101102

102103

0 commit comments

Comments
 (0)