From ffdb727e1c4e2424abede8591c4d74516300b19d Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 29 Oct 2020 14:03:08 -0400 Subject: [PATCH] Update BaseTable to accept user instance directly --- netbox/utilities/tables.py | 46 +++++++++++++++++++++----------------- netbox/utilities/views.py | 8 ++----- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index 5fd3a3a79..496f65dba 100644 --- a/netbox/utilities/tables.py +++ b/netbox/utilities/tables.py @@ -1,4 +1,5 @@ import django_tables2 as tables +from django.contrib.auth.models import AnonymousUser from django.contrib.contenttypes.fields import GenericForeignKey from django.core.exceptions import FieldDoesNotExist from django.db.models.fields.related import RelatedField @@ -11,10 +12,11 @@ class BaseTable(tables.Table): """ Default table for object lists - :param add_prefetch: 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()). + :param user: Personalize table display for the given user (optional). Has no effect if AnonymousUser is passed. """ + # 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 class Meta: @@ -22,7 +24,7 @@ class BaseTable(tables.Table): 'class': 'table table-hover table-headings', } - def __init__(self, *args, columns=None, **kwargs): + def __init__(self, *args, user=None, **kwargs): super().__init__(*args, **kwargs) # Set default empty_text if none was provided @@ -36,25 +38,27 @@ class BaseTable(tables.Table): if column.name not in default_columns: self.columns.hide(column.name) - # Apply custom column ordering - if columns is not None: - pk = self.base_columns.pop('pk', None) - actions = self.base_columns.pop('actions', None) + # Apply custom column ordering for user + if user is not None and not isinstance(user, AnonymousUser): + columns = user.config.get(f"tables.{self.__class__.__name__}.columns") + 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(): - if name in columns: - self.columns.show(name) - else: - self.columns.hide(name) - self.sequence = [c for c in columns if c in self.base_columns] + for name, column in self.base_columns.items(): + if name in columns: + self.columns.show(name) + else: + self.columns.hide(name) + self.sequence = [c for c in columns if c in self.base_columns] - # Always include PK and actions column, if defined on the table - if pk: - self.base_columns['pk'] = pk - self.sequence.insert(0, 'pk') - if actions: - self.base_columns['actions'] = actions - self.sequence.append('actions') + # Always include PK and actions column, if defined on the table + if pk: + self.base_columns['pk'] = pk + self.sequence.insert(0, 'pk') + if actions: + self.base_columns['actions'] = actions + self.sequence.append('actions') # Dynamically update the table's QuerySet to ensure related fields are pre-fetched if self.add_prefetch and isinstance(self.data, TableQuerysetData): diff --git a/netbox/utilities/views.py b/netbox/utilities/views.py index e7e7412bf..b6731491c 100644 --- a/netbox/utilities/views.py +++ b/netbox/utilities/views.py @@ -289,12 +289,8 @@ class ObjectListView(ObjectPermissionRequiredMixin, View): perm_name = get_permission_for_model(model, action) permissions[action] = request.user.has_perm(perm_name) - # Construct the table based on the user's permissions - if request.user.is_authenticated: - columns = request.user.config.get(f"tables.{self.table.__name__}.columns") - else: - columns = None - table = self.table(self.queryset, columns=columns) + # Construct the objects table + table = self.table(self.queryset, user=request.user) if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']): table.columns.show('pk')