From dc1bf460049c124bbbcf2d64221292bd230881c3 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 28 Apr 2025 13:00:46 +0100 Subject: [PATCH] improve case tables --- atlas/tables.py | 106 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 5 deletions(-) diff --git a/atlas/tables.py b/atlas/tables.py index 491980dc..457a79ee 100755 --- a/atlas/tables.py +++ b/atlas/tables.py @@ -35,11 +35,44 @@ class CaseTable(tables.Table): "series__examination", # ForeignKey in the related model "series__plane", # ForeignKey in the related model "series__images", # Reverse relationship + "previous_case", # Prefetch the previous case relationship + #"next_case", # Prefetch the next case relationship ), *args, **kwargs, ) + def render_title(self, value, record): + return value + + def render_description(self, value): + max_length = 50 # Set the maximum length for truncation + if len(value) > max_length: + truncated_value = f"{value[:max_length]}..." # Truncate and add ellipsis + else: + truncated_value = value + # Add a tooltip with the full text + return format_html( + '{}', + value, # Full text for the tooltip + truncated_value, # Truncated text for display + ) + + def render_series(self, value): + max_length = 50 # Set the maximum length for truncation + series_list = [str(series) for series in value.all()] + full_text = ", ".join(series_list) # Full text for the tooltip + if len(full_text) > max_length: + truncated_text = f"{full_text[:max_length]}..." # Truncate and add ellipsis + else: + truncated_text = full_text + # Add a tooltip with the full text + return format_html( + '{}', + full_text, # Full text for the tooltip + truncated_text, # Truncated text for display + ) + def get_view_cell(record): if record.open_access: return format_html(f""" @@ -49,6 +82,71 @@ class CaseTable(tables.Table): else: return f'View' + def render_author(self, value): + max_length = 15 # Set the maximum length for truncating individual author names + authors = [] + for author in value.all(): + full_name = str(author) # Convert the author object to a string (e.g., full name) + if len(full_name) > max_length: + truncated_name = f"{full_name[:max_length]}..." # Truncate and add ellipsis + else: + truncated_name = full_name + # Add a tooltip with the full name + authors.append( + format_html( + '{}', + full_name, # Full name for the tooltip + truncated_name, # Truncated name for display + ) + ) + # Join all authors with commas + return format_html(", ".join(authors)) + + def render_associated_cases(self, record): + links = [] + if record.previous_case: + links.append( + format_html( + 'Previous Case', + record.previous_case.get_absolute_url(), + ) + ) + try: + if record.next_case: + links.append( + format_html( + 'Next Case', + record.next_case.get_absolute_url(), + ) + ) + except Case.DoesNotExist: + pass + if links: + return format_html(", ".join(links)) + return "No associated cases" + + def render_collections(self, record): + collections = record.casecollection_set.all() + if collections: + links = [ + format_html( + '{}', + collection.get_absolute_url(), + collection.name, + ) + for collection in collections + ] + return format_html(", ".join(links)) + return "No collections" + + associated_cases = tables.Column( + verbose_name="Associated Cases", accessor="pk", orderable=False + ) + + collections = tables.Column( + verbose_name="Collections", accessor="pk", orderable=False + ) + edit = tables.LinkColumn( "atlas:case_update", text="Edit", args=[A("pk")], orderable=False ) @@ -62,8 +160,6 @@ class CaseTable(tables.Table): "atlas:case_delete", text="Delete", args=[A("pk")], orderable=False ) - #differential = tables.ManyToManyColumn(verbose_name="Differential") - selection = tables.CheckBoxColumn(accessor="pk", orderable=False) class Meta: @@ -72,13 +168,13 @@ class CaseTable(tables.Table): fields = ( "title", "description", - #"history", - #"condition", "created_date", "series", "author", + "associated_cases", + "collections", ) - sequence = ("view", )#, "series") + sequence = ("view", ) order_by = "-created_date"