Changeset 277

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

Make Form construction more convenient by adding (consisten) addField and
addGroup methods to the Form and Group as well as making stan-style
construction possible.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r266 r277  
     12007-01-19  Matt Goodall <matt@pollenation.net> 
     2         
     3        Made construction of the form more convenient by adding addField and 
     4        addGroup methods to Form and Group as well as returning the added item. 
     5 
     6        I also added the ability to construct a Form in a stan-like way. See the 
     7        stanstyle example for details. 
     8 
    192006-11-30  Tim Parkin <tim@pollenation.net> 
    210         
  • trunk/examples.tac

    r196 r277  
    44 
    55application = service.Application('examples') 
    6 service = internet.TCPServer(8000, main.makeSite(application)) 
     6service = internet.TCPServer(8000, main.makeSite()) 
    77service.setServiceParent(application) 
    88 
  • trunk/formal/__init__.py

    r266 r277  
    44 
    55 
    6 version_info = (0, 10, 0) 
     6version_info = (0, 11, 0) 
    77version = '.'.join([str(i) for i in version_info]) 
    88 
  • trunk/formal/examples/main.py

    r256 r277  
    1515    'formal.examples.prepopulate.PrepopulateFormPage', 
    1616    'formal.examples.group.GroupFormPage', 
     17    'formal.examples.stanstyle.StanStyleFormPage', 
    1718    'formal.examples.fileupload.FileUploadFormPage', 
    1819    'formal.examples.smartupload.SmartUploadFormPage', 
     
    2728    ] 
    2829 
    29 def makeSite(application): 
     30def makeSite(): 
    3031    root = RootPage() 
    31     site = appserver.NevowSite(root, logPath='web.log'
     32    site = appserver.NevowSite(root, logPath="web.log"
    3233    return site 
    3334 
     
    110111setattr(RootPage, 'child_formal.css', formal.defaultCSS) 
    111112setattr(RootPage, 'child_js', formal.formsJS) 
     113 
     114if __name__ == '__main__': 
     115     
     116    import sys 
     117    from twisted.internet import reactor 
     118    from twisted.python import log 
     119     
     120    if len(sys.argv) > 1: 
     121        port = int(sys.argv[1]) 
     122    else: 
     123        port = 8000 
     124     
     125    log.startLogging(sys.stdout) 
     126    site = makeSite() 
     127    reactor.listenTCP(port, site) 
     128    reactor.run() 
     129     
  • trunk/formal/form.py

    r271 r277  
    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    def __getitem__(self, items): 
     254        """ 
     255        Overridden to allow stan-style construction of forms. 
     256        """ 
     257        # Items may be a list or a scalar so stick a scalar into a list 
     258        # immediately to simplify the code. 
     259        try: 
     260            items = iter(items) 
     261        except TypeError: 
     262            items = [items] 
     263        # Add each item 
     264        for item in items: 
     265            self.add(item) 
     266        # Return myself 
     267        return self 
     268 
     269 
     270class Group(AddHelperMixin, object): 
    240271 
    241272 
     
    257288 
    258289    key = property(lambda self: itemKey(self)) 
    259  
    260  
     290     
     291     
    261292    def setItemParent(self, itemParent): 
    262293        self.itemParent = itemParent 
     
    314345 
    315346 
    316 class Form(object): 
     347class Form(AddHelperMixin, object): 
    317348 
    318349    implements( iformal.IForm ) 
     
    332363        self.getItemByName = self.items.getItemByName 
    333364 
    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)) 
    337365 
    338366    def addAction(self, callback, name="submit", validate=True, label=None): 
     
    429457        self.items.append(item) 
    430458        item.setItemParent(self.itemParent) 
     459        return item 
    431460 
    432461 
  • trunk/setup.py

    r266 r277  
    33setup( 
    44    name='formal', 
    5     version='0.10.0', 
     5    version='0.11.0', 
    66    description='HTML forms framework for Nevow', 
    77    author='Matt Goodall',