kopia lustrzana https://github.com/wagtail/wagtail
Add normalize() implementation to TypedTableBlock (#12808)
rodzic
58584896df
commit
794fe25e97
|
@ -5,6 +5,7 @@ Changelog
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
* Add `WAGTAIL_` prefix to Wagtail-specific tag settings (Aayushman Singh)
|
* Add `WAGTAIL_` prefix to Wagtail-specific tag settings (Aayushman Singh)
|
||||||
|
* Implement `normalize` on `TypedTableBlock` to assist with setting `default` and `preview_value` (Sage Abdullah)
|
||||||
* Fix: Take preferred language into account for translatable strings in client-side code (Bernhard Bliem, Sage Abdullah)
|
* Fix: Take preferred language into account for translatable strings in client-side code (Bernhard Bliem, Sage Abdullah)
|
||||||
* Docs: Add missing `django.contrib.admin` to list of apps in "add to Django project" guide (Mohamed Rabiaa)
|
* Docs: Add missing `django.contrib.admin` to list of apps in "add to Django project" guide (Mohamed Rabiaa)
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ depth: 1
|
||||||
|
|
||||||
### Other features
|
### Other features
|
||||||
|
|
||||||
* ...
|
* Implement `normalize` on `TypedTableBlock` to assist with setting `default` and `preview_value` (Sage Abdullah)
|
||||||
|
|
||||||
### Bug fixes
|
### Bug fixes
|
||||||
|
|
||||||
|
|
|
@ -188,6 +188,11 @@ class BaseTypedTableBlock(Block):
|
||||||
"caption": "",
|
"caption": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def normalize(self, value):
|
||||||
|
if value is None or isinstance(value, TypedTable):
|
||||||
|
return value
|
||||||
|
return self.to_python(value)
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
if value:
|
if value:
|
||||||
columns = [
|
columns = [
|
||||||
|
|
|
@ -162,6 +162,33 @@ class TestTableBlock(TestCase):
|
||||||
["fr", "68000000", "A large country with baguettes"],
|
["fr", "68000000", "A large country with baguettes"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_normalize(self):
|
||||||
|
# Should be able to handle JSONish data from the database, which can be
|
||||||
|
# useful when defining a default value for a TypedTableBlock
|
||||||
|
table = self.block.normalize(self.db_data)
|
||||||
|
self.assertEqual(table.caption, "Countries and their food")
|
||||||
|
self.assertIsInstance(table, TypedTable)
|
||||||
|
self.assertEqual(len(table.columns), 2)
|
||||||
|
self.assertEqual(table.columns[0]["heading"], "Country")
|
||||||
|
self.assertEqual(table.columns[1]["heading"], "Description")
|
||||||
|
rows = list(table.rows)
|
||||||
|
self.assertEqual(len(rows), 2)
|
||||||
|
self.assertEqual(
|
||||||
|
[block.value for block in rows[0]],
|
||||||
|
["nl", "A small country with stroopwafels"],
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
[block.value for block in rows[1]], ["fr", "A large country with baguettes"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# For a TypedTable instance, normalize should return the instance as-is
|
||||||
|
normalized_table = self.block.normalize(table)
|
||||||
|
self.assertIs(normalized_table, table)
|
||||||
|
|
||||||
|
# Should normalize None as-is
|
||||||
|
none_value = self.block.normalize(None)
|
||||||
|
self.assertIs(none_value, None)
|
||||||
|
|
||||||
def test_to_python(self):
|
def test_to_python(self):
|
||||||
"""
|
"""
|
||||||
Test that we can turn JSONish data from the database into a TypedTable instance
|
Test that we can turn JSONish data from the database into a TypedTable instance
|
||||||
|
|
Ładowanie…
Reference in New Issue