Changeset 6258 for main/waeup.sirp/trunk/src/waeup/sirp/utils
- Timestamp:
- 31 May 2011, 08:39:48 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py
r5328 r6258 20 20 grok.baseclass() 21 21 grok.provides(ISchemaTypeConverter) 22 22 23 23 def __init__(self, context): 24 24 """Create a converter with context, a zope.schema.Field, as context. … … 31 31 """ 32 32 raise NotImplementedError('method not implemented') 33 33 34 34 def fromString(self, string=None, strict=True): 35 35 """Convert ``string`` to value according to assigned field type. … … 82 82 return '1' 83 83 return '0' 84 84 85 85 class TextConverter(Converter): 86 86 """A converter for zope.schema.interfaces.IText fields. … … 109 109 tokens = None 110 110 values = None 111 111 112 112 def __init__(self, context): 113 113 self.context = context … … 125 125 # here. This speeds up mass operations with many conversions 126 126 # by factor 10 or more. 127 127 128 128 # Mapping token -> value 129 129 self.tokens = dict([(self.context.source.factory.getToken(x), x) … … 192 192 193 193 * ``YYYY/MM/DD`` 194 194 195 195 * ``DD/MM/YYYY`` 196 196 197 197 * ``D/M/YYYY`` 198 198 … … 213 213 formats = ['%Y-%m-%d', '%Y/%m/%d' , '%d/%m/%Y', '%D/%M/%Y', 214 214 '%d.%m.%Y', '%D.%M.%Y'] 215 215 216 216 def _convertValueFromString(self, string): 217 217 orig_string = string … … 238 238 def _convertValueToString(self, value): 239 239 return datetime.date.strftime(value, '%Y-%m-%d') 240 241 242 from zope.interface import Interface 243 from zope.formlib import form 244 from zope.formlib.interfaces import IInputWidget 245 from zope.publisher.browser import TestRequest 246 247 class 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.