Update BaseTable to accept user instance directly

This commit is contained in:
Jeremy Stretch 2020-10-29 14:03:08 -04:00
parent cf328ca51e
commit ffdb727e1c
2 changed files with 27 additions and 27 deletions

View File

@ -1,4 +1,5 @@
import django_tables2 as tables import django_tables2 as tables
from django.contrib.auth.models import AnonymousUser
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import FieldDoesNotExist from django.core.exceptions import FieldDoesNotExist
from django.db.models.fields.related import RelatedField from django.db.models.fields.related import RelatedField
@ -11,10 +12,11 @@ class BaseTable(tables.Table):
""" """
Default table for object lists Default table for object lists
:param add_prefetch: By default, modify the queryset passed to the table upon initialization to automatically :param user: Personalize table display for the given user (optional). Has no effect if AnonymousUser is passed.
prefetch related data. Set this to False if it's necessary to avoid modifying the queryset (e.g. to
accommodate PrefixQuerySet.annotate_depth()).
""" """
# By default, modify the queryset passed to the table upon initialization to automatically prefetch related
# data. Set this to False if it's necessary to avoid modifying the queryset (e.g. to accommodate
# PrefixQuerySet.annotate_depth()).
add_prefetch = True add_prefetch = True
class Meta: class Meta:
@ -22,7 +24,7 @@ class BaseTable(tables.Table):
'class': 'table table-hover table-headings', 'class': 'table table-hover table-headings',
} }
def __init__(self, *args, columns=None, **kwargs): def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Set default empty_text if none was provided # Set default empty_text if none was provided
@ -36,25 +38,27 @@ class BaseTable(tables.Table):
if column.name not in default_columns: if column.name not in default_columns:
self.columns.hide(column.name) self.columns.hide(column.name)
# Apply custom column ordering # Apply custom column ordering for user
if columns is not None: if user is not None and not isinstance(user, AnonymousUser):
pk = self.base_columns.pop('pk', None) columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
actions = self.base_columns.pop('actions', None) if columns is not None:
pk = self.base_columns.pop('pk', None)
actions = self.base_columns.pop('actions', None)
for name, column in self.base_columns.items(): for name, column in self.base_columns.items():
if name in columns: if name in columns:
self.columns.show(name) self.columns.show(name)
else: else:
self.columns.hide(name) self.columns.hide(name)
self.sequence = [c for c in columns if c in self.base_columns] self.sequence = [c for c in columns if c in self.base_columns]
# Always include PK and actions column, if defined on the table # Always include PK and actions column, if defined on the table
if pk: if pk:
self.base_columns['pk'] = pk self.base_columns['pk'] = pk
self.sequence.insert(0, 'pk') self.sequence.insert(0, 'pk')
if actions: if actions:
self.base_columns['actions'] = actions self.base_columns['actions'] = actions
self.sequence.append('actions') self.sequence.append('actions')
# Dynamically update the table's QuerySet to ensure related fields are pre-fetched # Dynamically update the table's QuerySet to ensure related fields are pre-fetched
if self.add_prefetch and isinstance(self.data, TableQuerysetData): if self.add_prefetch and isinstance(self.data, TableQuerysetData):

View File

@ -289,12 +289,8 @@ class ObjectListView(ObjectPermissionRequiredMixin, View):
perm_name = get_permission_for_model(model, action) perm_name = get_permission_for_model(model, action)
permissions[action] = request.user.has_perm(perm_name) permissions[action] = request.user.has_perm(perm_name)
# Construct the table based on the user's permissions # Construct the objects table
if request.user.is_authenticated: table = self.table(self.queryset, user=request.user)
columns = request.user.config.get(f"tables.{self.table.__name__}.columns")
else:
columns = None
table = self.table(self.queryset, columns=columns)
if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']): if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']):
table.columns.show('pk') table.columns.show('pk')