Просмотр исходного кода

Update BaseTable to accept user instance directly

Jeremy Stretch 5 лет назад
Родитель
Сommit
ffdb727e1c
2 измененных файлов с 29 добавлено и 29 удалено
  1. 27 23
      netbox/utilities/tables.py
  2. 2 6
      netbox/utilities/views.py

+ 27 - 23
netbox/utilities/tables.py

@@ -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
-      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
     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
-        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]
-
-            # 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')
+        # 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]
+
+                # 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
         # 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):

+ 2 - 6
netbox/utilities/views.py

@@ -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
-        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']):
         if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']):
             table.columns.show('pk')
             table.columns.show('pk')