Source code for apis_core.generic.tables
import django_tables2 as tables
from apis_core.generic.helpers import permission_fullname
[docs]
class CustomTemplateColumn(tables.TemplateColumn):
"""
A custom template column - the `tables.TemplateColumn` class does not allow
to set attributes via class variables. Therefor we use this
CustomTemplateColumn to set some arguments based on class attributes and
override the attributes in child classes.
"""
template_name = None
orderable = None
exclude_from_export = False
verbose_name = None
def __init__(self, *args, **kwargs):
super().__init__(
template_name=self.template_name,
orderable=self.orderable,
exclude_from_export=self.exclude_from_export,
verbose_name=self.verbose_name,
*args,
**kwargs,
)
[docs]
class ActionColumn(CustomTemplateColumn):
"""
A custom template column with some additional attributes
for actions.
"""
orderable = False
exclude_from_export = True
attrs = {"td": {"style": "width:1%;"}, "th": {"style": "font-size: 0"}}
[docs]
def render(self, record, table, *args, **kwargs):
if permission := getattr(self, "permission", False):
if not table.request.user.has_perm(permission_fullname(permission, record)):
return ""
return super().render(record, table, *args, **kwargs)
[docs]
class ActionsColumn(CustomTemplateColumn):
orderable = False
exclude_from_export = True
template_name = "columns/actions.html"
attrs = {"td": {"style": "width:8em;"}, "th": {"style": "font-size: 0"}}
[docs]
class DescriptionColumn(CustomTemplateColumn):
"""
A column showing a model description
"""
template_name = "columns/description.html"
orderable = False
[docs]
class GenericTable(tables.Table):
"""
A generic table that contains an edit button column, a delete button column
and a description column
"""
desc = DescriptionColumn()
actions = ActionsColumn()
[docs]
class MoreLessColumn(tables.TemplateColumn):
"""
Useful for displaying long fields.
A preview is shown initially with a "Show more" link
which is replaced with a "Show less" link when expanded.
"""
template_name = "columns/more-less.html"
def __init__(self, preview, fulltext, *args, **kwargs):
self.preview = preview
self.fulltext = fulltext
super().__init__(template_name=self.template_name, *args, **kwargs)
[docs]
def render(self, record, **kwargs):
self.extra_context["preview"] = self.preview(record)
self.extra_context["fulltext"] = self.fulltext(record)
return super().render(record, **kwargs)