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

This commit is contained in:
Fabi
2026-04-01 12:36:21 -04:00
committed by GitHub
parent 3ce2bf75b4
commit e98e5e11a7
2 changed files with 12 additions and 1 deletions

View File

@@ -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.

View File

@@ -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')