From 57a3b02da3fd0869848d8aff04c4a2b5e2e3f006 Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 1 Nov 2025 20:45:59 +0000 Subject: [PATCH] Implement selection-enabled table base class and update QuestionTable to inherit from it --- generic/tables.py | 22 ++++++++++++++++++++++ sbas/tables.py | 10 ++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/generic/tables.py b/generic/tables.py index fb3b700d..dbe32f08 100644 --- a/generic/tables.py +++ b/generic/tables.py @@ -12,6 +12,28 @@ from generic.models import CidUser, Examination, Supervisor, findMiddle from django.contrib.auth.models import User +# Generic selection-enabled table base class. +# Inherit from this class to automatically get a selection checkbox +# column (input name="selection") and table attrs that client-side +# scripts can use to find selectable tables. +class SelectionTable(tables.Table): + # Provide a default selection checkbox column. Subclasses that wish to + # customise can override this attribute. + selection = tables.CheckBoxColumn( + accessor="pk", + orderable=False, + attrs={ + 'th': {'class': 'selection-col'}, + 'td': {'class': 'selection-col'}, + 'input': {'name': 'selection'}, + }, + verbose_name='' + ) + + class Meta: + # Defaults: add js-row-selectable class so client-side code can + # locate tables that support row selection. + attrs = {"class": "table js-row-selectable", "data-selection-name": "selection"} class SingleImageColumn(tables.Column): def render(self, value): diff --git a/sbas/tables.py b/sbas/tables.py index c21d8dae..d9b3f837 100644 --- a/sbas/tables.py +++ b/sbas/tables.py @@ -6,16 +6,18 @@ from .models import Question, UserAnswer from django.utils.html import format_html +from generic.tables import SelectionTable -class QuestionTable(tables.Table): + +class QuestionTable(SelectionTable): def get_view_cell(record): if record.open_access: - return format_html(f""" + return format_html(""" View""") else: - return f'View' + return 'View' #edit = tables.LinkColumn( # "sbas:sbas_question_update", text="Edit", args=[A("pk")], orderable=False #) @@ -27,7 +29,7 @@ class QuestionTable(tables.Table): #) #exams = tables.ManyToManyColumn(verbose_name="Exams") - selection = tables.CheckBoxColumn(accessor="pk", orderable=False) + #selection = tables.CheckBoxColumn(accessor="pk", orderable=False) stem = tables.Column() answers = tables.TemplateColumn("
  1. {{ record.a_answer|safe}}
  2. {{ record.b_answer|safe}}
  3. {{ record.c_answer|safe}}
  4. {{ record.d_answer|safe}}
  5. {{ record.e_answer|safe}}
")