Skip to content

Commit 90ff085

Browse files
authored
Merge pull request iommirocks#700 from iommirocks/non-sortable-columns-are-not-sortable-by-default
Disallow sorting by default on non-sortable columns
2 parents 859886c + dbbe6df commit 90ff085

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

iommi/table.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
)
2424
from urllib.parse import quote_plus
2525

26-
from django.core.exceptions import ImproperlyConfigured
26+
from django.core.exceptions import (
27+
FieldDoesNotExist,
28+
ImproperlyConfigured,
29+
)
2730
from django.db.models import (
2831
AutoField,
2932
BooleanField,
@@ -507,6 +510,14 @@ def on_bind(self) -> None:
507510
# Not strict evaluate on purpose
508511
self.model = evaluate(self.model, **self.iommi_evaluate_parameters())
509512

513+
if self.table.model and self.attr is not None and self.sortable:
514+
if '__' not in self.attr:
515+
# Only validate one level of the path for now
516+
try:
517+
self.table.model._meta.get_field(self.attr)
518+
except FieldDoesNotExist:
519+
self.sortable = False
520+
510521
if self.auto_rowspan:
511522
assert 'rowspan' not in self.cell.attrs, (
512523
f'Explicitly set rowspan html attribute collides with ' f'auto_rowspan on column {self.iommi_path}'

iommi/table__tests.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,8 +2552,8 @@ class Meta:
25522552
<table class="table" >
25532553
<thead>
25542554
<tr>
2555-
<th class="first_column iommi_sort_header subheader">
2556-
<a href="?order=some_non_existent_property"> Some non existent property </a>
2555+
<th class="first_column subheader">
2556+
Some non existent property
25572557
</th>
25582558
</tr>
25592559
</thead>
@@ -2801,8 +2801,8 @@ class Meta:
28012801
<table class="table" >
28022802
<thead>
28032803
<tr>
2804-
<th class="first_column iommi_sort_header subheader">
2805-
<a href="?order=a"> A </a>
2804+
<th class="first_column subheader">
2805+
A
28062806
</th>
28072807
</tr>
28082808
</thead>
@@ -4712,3 +4712,24 @@ def test_auto_rowspan_two_columns():
47124712
</tbody>
47134713
'''
47144714
)
4715+
4716+
4717+
@pytest.mark.django_db
4718+
def test_disallow_sorting_on_non_sortable_columns_that_have_valid_attr():
4719+
T1.objects.create(pk=1, foo='a', bar='b')
4720+
4721+
def preprocess_rows(rows, **_):
4722+
for row in rows:
4723+
row.synthetic = 1
4724+
return rows
4725+
4726+
t = Table(
4727+
auto__model=T1,
4728+
preprocess_rows=preprocess_rows,
4729+
columns__synthetic=Column(),
4730+
)
4731+
4732+
# This should not crash
4733+
t = t.bind(request=req('get', **{'order': 'synthetic'}))
4734+
4735+
assert not t.columns.synthetic.sortable

0 commit comments

Comments
 (0)