- The title of a schema node now defaults to a titleization of the

``name``.  Underscores in the ``name`` are replaced with empty
  strings and the first letter of every resulting word is capitalized.
  Previously the ``name`` was not split on underscores, and the
  entirety of the ``name`` was capitalized.
This commit is contained in:
Chris McDonough
2010-08-31 18:17:25 +00:00
parent c36df014f0
commit 5e57308a5e
3 changed files with 20 additions and 10 deletions

View File

@@ -1,6 +1,15 @@
Changes
=======
Next release
------------
- The title of a schema node now defaults to a titleization of the
``name``. Underscores in the ``name`` are replaced with empty
strings and the first letter of every resulting word is capitalized.
Previously the ``name`` was not split on underscores, and the
entirety of the ``name`` was capitalized.
0.7.2 (2010/08/30)
------------------

View File

@@ -1150,9 +1150,10 @@ class SchemaNode(object):
an object that implements the
:class:`colander.interfaces.Validator` interface.
- ``title``: The title of this node. Defaults to a captialization
of the ``name``. The title is used by higher-level systems (not
by Colander itself).
- ``title``: The title of this node. Defaults to a titleization
of the ``name`` (underscores replaced with empty strings and the
first letter of every resulting word capitalized). The title is
used by higher-level systems (not by Colander itself).
- ``description``: The description for this node. Defaults to
``''`` (the empty string). The description is used by
@@ -1176,7 +1177,7 @@ class SchemaNode(object):
self.default = kw.pop('default', null)
self.missing = kw.pop('missing', _marker)
self.name = kw.pop('name', '')
self.title = kw.pop('title', self.name.capitalize())
self.title = kw.pop('title', self.name.replace('_', ' ').title())
self.description = kw.pop('description', '')
self.widget = kw.pop('widget', None)
if kw:
@@ -1279,7 +1280,7 @@ class _SchemaMeta(type):
if isinstance(value, SchemaNode):
value.name = name
if not value.title:
value.title = name.capitalize()
value.title = name.replace('_', ' ').title()
nodes.append((value._order, value))
cls.__schema_nodes__ = nodes
# Combine all attrs from this class and its subclasses.

View File

@@ -1271,15 +1271,15 @@ class TestSchemaNode(unittest.TestCase):
self.failUnless(hasattr(node, '_order'))
def test_ctor_no_title(self):
node = self._makeOne(None, 0, validator=1, default=2, name='name',
node = self._makeOne(None, 0, validator=1, default=2, name='name_a',
missing='missing')
self.assertEqual(node.typ, None)
self.assertEqual(node.children, [0])
self.assertEqual(node.validator, 1)
self.assertEqual(node.default, 2)
self.assertEqual(node.missing, 'missing')
self.assertEqual(node.name, 'name')
self.assertEqual(node.title, 'Name')
self.assertEqual(node.name, 'name_a')
self.assertEqual(node.title, 'Name A')
def test_ctor_with_title(self):
node = self._makeOne(None, 0, validator=1, default=2, name='name',
@@ -1434,7 +1434,7 @@ class TestSchema(unittest.TestCase):
def test_it(self):
import colander
class MySchema(colander.Schema):
thing = colander.SchemaNode(colander.String())
thing_a = colander.SchemaNode(colander.String())
thing2 = colander.SchemaNode(colander.String(), title='bar')
node = MySchema(default='abc')
self.failUnless(hasattr(node, '_order'))
@@ -1442,7 +1442,7 @@ class TestSchema(unittest.TestCase):
self.assertEqual(node.__class__, colander.SchemaNode)
self.assertEqual(node.typ.__class__, colander.Mapping)
self.assertEqual(node.children[0].typ.__class__, colander.String)
self.assertEqual(node.children[0].title, 'Thing')
self.assertEqual(node.children[0].title, 'Thing A')
self.assertEqual(node.children[1].title, 'bar')
class TestSequenceSchema(unittest.TestCase):