Quellcode durchsuchen

Fixes #21784: Fix AttributeError when an AnonymousUser tries to sort a table (#21817)

Fabi vor 8 Stunden
Ursprung
Commit
e98e5e11a7
2 geänderte Dateien mit 12 neuen und 1 gelöschten Zeilen
  1. 1 1
      netbox/netbox/tables/tables.py
  2. 11 0
      netbox/netbox/tests/test_tables.py

+ 1 - 1
netbox/netbox/tables/tables.py

@@ -159,7 +159,7 @@ class BaseTable(tables.Table):
         columns = None
         ordering = None
 
-        if self.prefixed_order_by_field in request.GET:
+        if request.user.is_authenticated and self.prefixed_order_by_field in request.GET:
             if request.GET[self.prefixed_order_by_field]:
                 # If an ordering has been specified as a query parameter, save it as the
                 # user's preferred ordering for this table.

+ 11 - 0
netbox/netbox/tests/test_tables.py

@@ -1,3 +1,4 @@
+from django.contrib.auth.models import AnonymousUser
 from django.template import Context, Template
 from django.test import RequestFactory, TestCase
 
@@ -46,6 +47,16 @@ class BaseTableTest(TestCase):
         prefetch_lookups = table.data.data._prefetch_related_lookups
         self.assertEqual(prefetch_lookups, tuple())
 
+    def test_configure_anonymous_user_with_ordering(self):
+        """
+        Verify that table.configure() does not raise an error when an anonymous
+        user sorts a table column.
+        """
+        request = RequestFactory().get('/?sort=name')
+        request.user = AnonymousUser()
+        table = DeviceTable(Device.objects.all())
+        table.configure(request)
+
 
 class TagColumnTable(NetBoxTable):
     tags = columns.TagColumn(url_name='dcim:site_list')