1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
from datetime import datetime
from django.test import TestCase
import django_tables2 as tables
from django_tables2 import RequestConfig
from .app.models import Person
from .utils import build_request
request = build_request("/")
MEMORY_DATA = [
{"i": 2, "alpha": "b", "beta": "b"},
{"i": 1, "alpha": "a", "beta": "c"},
{"i": 3, "alpha": "c", "beta": "a"},
]
PEOPLE = [
{"first_name": "Bradley", "last_name": "Ayers"},
{"first_name": "Bradley", "last_name": "Fake"},
{"first_name": "Chris", "last_name": "Doble"},
{"first_name": "Davina", "last_name": "Adisusila"},
{"first_name": "Ross", "last_name": "Ayers"},
]
class UnorderedTable(tables.Table):
i = tables.Column()
alpha = tables.Column()
beta = tables.Column()
class OrderedTable(UnorderedTable):
class Meta:
order_by = "alpha"
class OrderingTest(TestCase):
def test_meta_ordering_list(self):
class Table(UnorderedTable):
class Meta:
order_by = ["i", "alpha"]
self.assertEqual(Table([]).order_by, ("i", "alpha"))
self.assertEqual(Table([], order_by=["alpha", "i"]).order_by, ("alpha", "i"))
def test_meta_ordering_tuple(self):
class Table(UnorderedTable):
class Meta:
order_by = ("i", "alpha")
self.assertEqual(Table([]).order_by, ("i", "alpha"))
def test_ordering(self):
# fallback to Table.Meta
self.assertEqual(OrderedTable([], order_by=None).order_by, ("alpha",))
self.assertEqual(OrderedTable([]).order_by, ("alpha",))
# values of order_by are wrapped in tuples before being returned
self.assertEqual(OrderedTable([], order_by="alpha").order_by, ("alpha",))
self.assertEqual(OrderedTable([], order_by=("beta",)).order_by, ("beta",))
for test in [[], (), ""]:
table = OrderedTable([])
table.order_by = test
self.assertEqual(table.order_by, ())
self.assertEqual(table.order_by, OrderedTable([], order_by=[]).order_by)
# apply an ordering
table = UnorderedTable([])
table.order_by = "alpha"
self.assertEqual(table.order_by, ("alpha",))
self.assertEqual(UnorderedTable([], order_by="alpha").order_by, ("alpha",))
# let's check the data
table = OrderedTable(MEMORY_DATA, order_by="beta")
self.assertEqual(table.rows[0].get_cell("i"), 3)
table = OrderedTable(MEMORY_DATA, order_by="-beta")
self.assertEqual(table.rows[0].get_cell("i"), 1)
# allow fallback to Table.Meta.order_by
table = OrderedTable(MEMORY_DATA)
self.assertEqual(table.rows[0].get_cell("i"), 1)
# column's can't be ordered if they're not allowed to be
class TestTable2(tables.Table):
a = tables.Column(orderable=False)
b = tables.Column()
table = TestTable2([], order_by="a")
self.assertEqual(table.order_by, ())
table = TestTable2([], order_by="b")
self.assertEqual(table.order_by, ("b",))
# ordering disabled by default
class TestTable3(tables.Table):
a = tables.Column(orderable=True)
b = tables.Column()
class Meta:
orderable = False
table = TestTable3([], order_by="a")
self.assertEqual(table.order_by, ("a",))
table = TestTable3([], order_by="b")
self.assertEqual(table.order_by, ())
table = TestTable3([], orderable=True, order_by="b")
self.assertEqual(table.order_by, ("b",))
def test_ordering_different_types(self):
data = [
{"i": 1, "alpha": datetime.now(), "beta": [1]},
{"i": {}, "alpha": None, "beta": ""},
{"i": 2, "alpha": None, "beta": []},
]
table = OrderedTable(data)
self.assertEqual(table.rows[0].get_cell("alpha"), "—")
table = OrderedTable(data, order_by="i")
self.assertEqual(table.rows[0].get_cell("i"), {})
table = OrderedTable(data, order_by="beta")
self.assertEqual(table.rows[0].get_cell("beta"), [])
def test_multi_column_ordering_by_table(self):
class PersonTable(tables.Table):
first_name = tables.Column()
last_name = tables.Column()
brad, brad2, chris, davina, ross = PEOPLE
table = PersonTable(PEOPLE, order_by=("first_name", "last_name"))
self.assertEqual([brad, brad2, chris, davina, ross], [r.record for r in table.rows])
table = PersonTable(PEOPLE, order_by=("first_name", "-last_name"))
self.assertEqual([brad2, brad, chris, davina, ross], [r.record for r in table.rows])
def test_multi_column_ordering_by_column(self):
# let's try column order_by using multiple keys
class PersonTable(tables.Table):
name = tables.Column(order_by=("first_name", "last_name"))
brad, brad2, chris, davina, ross = PEOPLE
# add 'name' key for each person.
for person in PEOPLE:
person["name"] = f"{person['first_name']} {person['last_name']}"
self.assertEqual(brad["name"], "Bradley Ayers")
table = PersonTable(PEOPLE, order_by="name")
self.assertEqual([brad, brad2, chris, davina, ross], [r.record for r in table.rows])
table = PersonTable(PEOPLE, order_by="-name")
self.assertEqual([ross, davina, chris, brad2, brad], [r.record for r in table.rows])
def test_ordering_by_custom_field(self):
"""
When defining a custom field in a table, as name=tables.Column() with
methods to render and order render_name and order_name, sorting by this
column causes an error if the custom field is not in last position.
https://github.com/jieter/django-tables2/issues/413
"""
Person.objects.create(first_name="Alice", last_name="Beta")
Person.objects.create(first_name="Bob", last_name="Alpha")
from django.db.models import F, Value
from django.db.models.functions import Concat
class PersonTable(tables.Table):
first_name = tables.Column()
last_name = tables.Column()
full_name = tables.Column()
class Meta:
model = Person
fields = ("first_name", "last_name", "full_name")
def render_full_name(self, record):
return record.last_name + " " + record.first_name
def order_full_name(self, queryset, is_descending):
queryset = queryset.annotate(
full_name=Concat(F("last_name"), Value(" "), F("first_name"))
).order_by(("-" if is_descending else "") + "full_name")
return queryset, True
table = PersonTable(Person.objects.all())
request = build_request("/?sort=full_name&sort=first_name")
RequestConfig(request).configure(table)
self.assertEqual(table.rows[0].record.first_name, "Bob")
def test_list_table_data_supports_ordering(self):
class Table(tables.Table):
name = tables.Column()
data = [{"name": "Bradley"}, {"name": "Davina"}]
table = Table(data)
self.assertEqual(table.rows[0].get_cell("name"), "Bradley")
table.order_by = "-name"
self.assertEqual(table.rows[0].get_cell("name"), "Davina")
def test_ordering_non_database_data(self):
class Table(tables.Table):
name = tables.Column()
country = tables.Column()
data = [
{"name": "Adrian", "country": "Australia"},
{"name": "Adrian", "country": "Brazil"},
{"name": "Audrey", "country": "Chile"},
{"name": "Bassie", "country": "Belgium"},
]
table = Table(data, order_by=("-name", "-country"))
self.assertEqual(
[(row.get_cell("name"), row.get_cell("country")) for row in table.rows],
[
("Bassie", "Belgium"),
("Audrey", "Chile"),
("Adrian", "Brazil"),
("Adrian", "Australia"),
],
)
def test_table_ordering_attributes(self):
class Table(tables.Table):
alpha = tables.Column()
beta = tables.Column()
table = Table(
MEMORY_DATA,
attrs={
"th": {
"class": "custom-header-class",
"_ordering": {
"orderable": "sortable",
"ascending": "ascend",
"descending": "descend",
},
}
},
order_by="alpha",
)
self.assertIn("sortable", table.columns[0].attrs["th"]["class"])
self.assertIn("ascend", table.columns[0].attrs["th"]["class"])
self.assertIn("custom-header-class", table.columns[1].attrs["th"]["class"])
def test_table_ordering_attributes_in_meta(self):
class Table(tables.Table):
alpha = tables.Column()
beta = tables.Column()
class Meta(OrderedTable.Meta):
attrs = {
"th": {
"class": "custom-header-class-in-meta",
"_ordering": {
"orderable": "sortable",
"ascending": "ascend",
"descending": "descend",
},
}
}
table = Table(MEMORY_DATA)
self.assertIn("sortable", table.columns[0].attrs["th"]["class"])
self.assertIn("ascend", table.columns[0].attrs["th"]["class"])
self.assertIn("custom-header-class-in-meta", table.columns[1].attrs["th"]["class"])
def test_column_ordering_attributes(self):
class Table(tables.Table):
alpha = tables.Column(
attrs={
"th": {
"class": "custom-header-class",
"_ordering": {"orderable": "sort", "ascending": "ascending"},
}
}
)
beta = tables.Column(
attrs={"th": {"_ordering": {"orderable": "canOrder"}}, "td": {"class": "cell-2"}}
)
table = Table(MEMORY_DATA, attrs={"class": "only-on-table"}, order_by="alpha")
self.assertNotIn("only-on-table", table.columns[0].attrs["th"]["class"])
self.assertIn("custom-header-class", table.columns[0].attrs["th"]["class"])
self.assertIn("ascending", table.columns[0].attrs["th"]["class"])
self.assertIn("sort", table.columns[0].attrs["th"]["class"])
self.assertIn("canOrder", table.columns[1].attrs["th"]["class"])
|