- Bug fix: if a `title argument which is the empty string or None` is

passed explicitly to a SchemaNode, it is no longer replaced by a title
  computed from the name.
This commit is contained in:
Chris McDonough
2011-01-26 20:17:49 +00:00
parent 518ac48cb9
commit 5dab4e22c2
3 changed files with 23 additions and 3 deletions

View File

@@ -7,6 +7,10 @@ Next release
- New API: ``colander.required``. Used as the marker value when a
``missing`` argument is left unspecified.
- Bug fix: if a ``title`` argument which is the empty string or ``None`` is
passed explicitly to a SchemaNode, it is no longer replaced by a title
computed from the name.
0.9.1 (2010-12-02)
------------------

View File

@@ -1256,7 +1256,11 @@ class SchemaNode(object):
self.default = kw.pop('default', null)
self.missing = kw.pop('missing', required)
self.name = kw.pop('name', '')
self.title = kw.pop('title', self.name.replace('_', ' ').title())
self.raw_title = kw.pop('title', _marker)
if self.raw_title is _marker:
self.title = self.name.replace('_', ' ').title()
else:
self.title = self.raw_title
self.description = kw.pop('description', '')
self.widget = kw.pop('widget', None)
self.after_bind = kw.pop('after_bind', None)
@@ -1403,14 +1407,13 @@ class SchemaNode(object):
self.name,
)
class _SchemaMeta(type):
def __init__(cls, name, bases, clsattrs):
nodes = []
for name, value in clsattrs.items():
if isinstance(value, SchemaNode):
value.name = name
if not value.title:
if value.raw_title is _marker:
value.title = name.replace('_', ' ').title()
nodes.append((value._order, value))
cls.__schema_nodes__ = nodes

View File

@@ -1608,6 +1608,19 @@ class TestSchema(unittest.TestCase):
self.assertEqual(node.children[0].title, 'Thing A')
self.assertEqual(node.children[1].title, 'bar')
def test_title_munging(self):
import colander
class MySchema(colander.Schema):
thing1 = colander.SchemaNode(colander.String())
thing2 = colander.SchemaNode(colander.String(), title=None)
thing3 = colander.SchemaNode(colander.String(), title='')
thing4 = colander.SchemaNode(colander.String(), title='thing2')
node = MySchema()
self.assertEqual(node.children[0].title, 'Thing1')
self.assertEqual(node.children[1].title, None)
self.assertEqual(node.children[2].title, '')
self.assertEqual(node.children[3].title, 'thing2')
class TestSequenceSchema(unittest.TestCase):
def test_succeed(self):
import colander