Skip to content

Commit

Permalink
glamr/tables: Allow ChainedQuerySet data to be passed to tables
Browse files Browse the repository at this point in the history
This is part 3 of 3 of the ChainQuerySet commit series.  Subsequent
commits will start making use of this from views.ExportMixin.
  • Loading branch information
robert102 committed Nov 22, 2024
1 parent 40cc2f8 commit 6cded67
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions mibios/glamr/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from django.utils.html import escape, format_html, mark_safe

from django_tables2 import Column, ManyToManyColumn, Table as Table0
from django_tables2.data import TableData

from mibios.glamr import models as glamr_models
from mibios.ncbi_taxonomy.models import TaxNode
from mibios.omics import models as omics_models
from mibios.omics.tables import FileTable as OmicsFileTable
from mibios.query import QuerySet
from mibios.query import ChainedQuerySet, QuerySet

from . import HORIZONTAL_ELLIPSIS
from .utils import get_record_url
Expand Down Expand Up @@ -48,8 +49,13 @@ def __init__(self, data=None, view=None, exclude=None, **kwargs):

if data is None:
data = self._meta.model.objects.all()
elif isinstance(data, ChainedQuerySet):
data = ChainedTableData(data)

data = self.customize_queryset(data)
if isinstance(data, TableData):
data.data = self.customize_queryset(data.data)
else:
data = self.customize_queryset(data)

super().__init__(data=data, exclude=exclude, **kwargs)

Expand Down Expand Up @@ -134,7 +140,7 @@ def as_values(self, exclude_columns=None):
Adapting super().as_values() with iterate() and force_str on all
values
"""
if not isinstance(self.data.data, QuerySet):
if not isinstance(self.data.data, (QuerySet, ChainedQuerySet)):
return super().as_values(exclude_columns=exclude_columns)

columns = self._get_export_columns(exclude_columns=exclude_columns)
Expand Down Expand Up @@ -644,3 +650,28 @@ def render_geo_loc_name(self, record):

def render_collection_timestamp(self, record):
return record.format_collection_timestamp()


class ChainedTableData(TableData):
"""
django_table2's TableData that works with ChainedQuerySet data
"""
@property
def ordering(self):
return ('pk',)

@staticmethod
def validate(data):
return isinstance(data, ChainedQuerySet)

def order_by(self, aliases):
"""
This is called by Table.__init__(), we don't do anything here
"""
pass

def __len__(self):
"""
Our data should only be iterated over, shouldn't need to know length.
"""
raise NotImplementedError

0 comments on commit 6cded67

Please sign in to comment.