Changeset 278

Show
Ignore:
Timestamp:
02/13/07 10:40:06 (2 years ago)
Author:
matt
Message:

Added a TextAreaList? widget.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r277 r278  
     12007-02-13  Matt Goodall <matt@pollenation.net> 
     2 
     3        Added a TextAreaList widget. The widget is rendered as a <textarea> and 
     4        splits the entered text into a sequence of values. 
     5 
    162007-01-19  Matt Goodall <matt@pollenation.net> 
    27         
  • trunk/formal/__init__.py

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

    r267 r278  
    144144        value = iformal.IStringConvertible(self.original).fromType(value) 
    145145        return self.original.validate(value) 
     146 
     147 
     148class TextAreaList(object): 
     149    """ 
     150    A text area that allows a list of values to be entered, one per line. Any 
     151    empty lines are discarded. 
     152    """ 
     153    implements( iformal.IWidget ) 
     154 
     155    cols = 48 
     156    rows = 6 
     157 
     158    def __init__(self, original, cols=None, rows=None): 
     159        self.original = original 
     160        if cols is not None: 
     161            self.cols = cols 
     162        if rows is not None: 
     163            self.rows = rows 
     164 
     165    def _renderTag(self, ctx, key, values, readonly): 
     166        value = '\n'.join(values) 
     167        tag=T.textarea(name=key, id=render_cssid(key), cols=self.cols, rows=self.rows)[value] 
     168        if readonly: 
     169            tag(class_='readonly', readonly='readonly') 
     170        return tag 
     171 
     172    def render(self, ctx, key, args, errors): 
     173        converter = iformal.IStringConvertible(self.original.type) 
     174        if errors: 
     175            values = args.get(key, []) 
     176        else: 
     177            values = args.get(key) 
     178            if values is not None: 
     179                values = [converter.fromType(v) for v in values] 
     180            else: 
     181                values = [] 
     182        return self._renderTag(ctx, key, values, False) 
     183 
     184    def renderImmutable(self, ctx, key, args, errors): 
     185        converter = iformal.IStringConvertible(self.original.type) 
     186        values = args.get(key) 
     187        if values is not None: 
     188            values = [converter.fromType(v) for v in values] 
     189        else: 
     190            values = [] 
     191        return self._renderTag(ctx, key, values, True) 
     192 
     193    def processInput(self, ctx, key, args): 
     194        # Get the whole string 
     195        value = args.get(key, [''])[0].decode(util.getPOSTCharset(ctx)) 
     196        # Split into lines 
     197        values = value.splitlines() 
     198        # Strip each line 
     199        values = [v.strip() for v in values] 
     200        # Discard empty lines 
     201        values = [v for v in values if v] 
     202        # Convert values to correct type 
     203        converter = iformal.IStringConvertible(self.original.type) 
     204        values = [converter.toType(v) for v in values] 
     205        # Validate and return 
     206        return self.original.validate(values) 
    146207 
    147208 
     
    11391200    'Password', 'SelectChoice', 'TextArea', 'TextInput', 'DatePartsInput', 
    11401201    'DatePartsSelect', 'MMYYDatePartsInput', 'Hidden', 'RadioChoice', 
    1141     'SelectOtherChoice', 'FileUpload', 'FileUploadWidget', 
     1202    'SelectOtherChoice', 'FileUpload', 'FileUploadWidget', 'TextAreaList', 
    11421203    ] 
    11431204 
  • trunk/setup.py

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