Changeset 273

Show
Ignore:
Timestamp:
01/19/07 12:02:36 (2 years ago)
Author:
matt
Message:

Adding fields and groups is such as common operation, they should require as
little typing as possible.

I created the AddHelperMixin? class and included it in Group and Form so that
addField(...) and addGroup(...) can be used consistently and with less
boilerplate.

The underlying add() now also return the added item, further reducing the
amount of code needed, especially when adding a group.

form = formal.Form()
group = form.addGroup("authentication")
group.addField("username", formal.String())
...
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/convenient-adding/formal/__init__.py

    r266 r273  
    44 
    55 
    6 version_info = (0, 10, 0) 
     6version_info = (0, 11, 0) 
    77version = '.'.join([str(i) for i in version_info]) 
    88 
  • branches/convenient-adding/formal/form.py

    r271 r273  
    237237 
    238238 
    239 class Group(object): 
     239class AddHelperMixin(object): 
     240    """ 
     241    A mixin that provides methods for common uses of add(...). 
     242    """ 
     243 
     244     
     245    def addGroup(self, *a, **k): 
     246        return self.add(Group(*a, **k)) 
     247         
     248         
     249    def addField(self, *a, **k): 
     250        return self.add(Field(*a, **k)) 
     251 
     252         
     253 
     254class Group(AddHelperMixin, object): 
    240255 
    241256 
     
    257272 
    258273    key = property(lambda self: itemKey(self)) 
    259  
    260  
     274     
     275     
    261276    def setItemParent(self, itemParent): 
    262277        self.itemParent = itemParent 
     
    314329 
    315330 
    316 class Form(object): 
     331class Form(AddHelperMixin, object): 
    317332 
    318333    implements( iformal.IForm ) 
     
    332347        self.getItemByName = self.items.getItemByName 
    333348 
    334     def addField(self, name, type, widgetFactory=None, label=None, 
    335             description=None, cssClass=None): 
    336         self.add(Field(name, type, widgetFactory, label, description, cssClass)) 
    337349 
    338350    def addAction(self, callback, name="submit", validate=True, label=None): 
     
    429441        self.items.append(item) 
    430442        item.setItemParent(self.itemParent) 
     443        return item 
    431444 
    432445