From 5e57308a5e338bdfdb21d671c0be9aa1266123b4 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 31 Aug 2010 18:17:25 +0000 Subject: [PATCH] - 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. --- CHANGES.txt | 9 +++++++++ colander/__init__.py | 11 ++++++----- colander/tests.py | 10 +++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 2bee9c6..7f35f49 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) ------------------ diff --git a/colander/__init__.py b/colander/__init__.py index 6018513..c9fb592 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -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. diff --git a/colander/tests.py b/colander/tests.py index d77447c..8bd40d4 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -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):