Changeset 6258 for main


Ignore:
Timestamp:
31 May 2011, 08:39:48 (13 years ago)
Author:
uli
Message:

Add a zope.formlib-based converter. Just playing around with
possibilities to apply string dicts to objects (in order to import CSV
data respecting invariants and all other constraints defined in some
interface).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py

    r5328 r6258  
    2020    grok.baseclass()
    2121    grok.provides(ISchemaTypeConverter)
    22    
     22
    2323    def __init__(self, context):
    2424        """Create a converter with context, a zope.schema.Field, as context.
     
    3131        """
    3232        raise NotImplementedError('method not implemented')
    33    
     33
    3434    def fromString(self, string=None, strict=True):
    3535        """Convert ``string`` to value according to assigned field type.
     
    8282            return '1'
    8383        return '0'
    84    
     84
    8585class TextConverter(Converter):
    8686    """A converter for zope.schema.interfaces.IText fields.
     
    109109    tokens = None
    110110    values = None
    111    
     111
    112112    def __init__(self, context):
    113113        self.context = context
     
    125125        # here. This speeds up mass operations with many conversions
    126126        # by factor 10 or more.
    127        
     127
    128128        # Mapping token -> value
    129129        self.tokens = dict([(self.context.source.factory.getToken(x), x)
     
    192192
    193193    * ``YYYY/MM/DD``
    194    
     194
    195195    * ``DD/MM/YYYY``
    196    
     196
    197197    * ``D/M/YYYY``
    198198
     
    213213    formats = ['%Y-%m-%d', '%Y/%m/%d' , '%d/%m/%Y', '%D/%M/%Y',
    214214               '%d.%m.%Y', '%D.%M.%Y']
    215    
     215
    216216    def _convertValueFromString(self, string):
    217217        orig_string = string
     
    238238    def _convertValueToString(self, value):
    239239        return datetime.date.strftime(value, '%Y-%m-%d')
     240
     241
     242from zope.interface import Interface
     243from zope.formlib import form
     244from zope.formlib.interfaces import IInputWidget
     245from zope.publisher.browser import TestRequest
     246
     247class DefaultConverter(Converter):
     248
     249    grok.context(Interface)
     250    grok.provides(ISchemaTypeConverter)
     251
     252    def __init__(self, iface):
     253        self.iface = iface
     254
     255    def applyRowData(self, data_dict, context):
     256        request = TestRequest(form={})
     257        form_fields = form.Fields(self.iface)
     258        for key, val in data_dict.items():
     259            request.form['form.%s' % key] = val
     260        widgets = form.setUpWidgets(
     261            form_fields, 'form', context, request)
     262        errors = form.getWidgetsData(widgets, 'form', data_dict)
     263        invariant_errors = form.checkInvariants(form_fields, data_dict)
     264        if not errors and not invariant_errors:
     265            changed = form.applyChanges(
     266                context, form_fields, data_dict)
     267        return errors, invariant_errors, data_dict
Note: See TracChangeset for help on using the changeset viewer.