Changeset 6263
- Timestamp:
- 2 Jun 2011, 02:46:07 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py
r6260 r6263 240 240 241 241 242 from zope import schema 243 from zope.component import createObject 242 244 from zope.interface import Interface 243 245 from zope.formlib import form … … 248 250 249 251 def getWidgetsData(widgets, form_prefix, data): 250 # from zope.formlib.form251 252 errors = [] 252 253 form_prefix = expandPrefix(form_prefix) … … 271 272 272 273 273 class DefaultConverter(Converter): 274 class IObjectConverter(Interface): 275 def __init__(iface): 276 """Create an converter. 277 278 `iface` denotes the interface to which we want to turn any 279 passed object. 280 281 """ 282 283 def applyRowData(data_dict, context, form_fields=None): 284 """Apply data in `data_dict` to `context`. 285 286 `data_dict` is a dict containing field names as keys and an 287 object or string as `context`. 288 289 If `context` is a string, this is understood as a factory name 290 and we will try to create a proper object calling 291 ``createObject()``. 292 293 `form_fields` are by default (``None``) buildt from the given 294 `iface` but can also be passed in to override the 295 default. This might be handy if you want to omit or select 296 certains fields from the interface. 297 298 Returns a tuple ``(<ERROR_LIST, INV_ERR_LIST, OBJ>)`` where 299 ``ERROR_DICT`` is a dict of errors for single fields (if 300 happened), ``INV_ERR_LIST`` is a list of invariant errors 301 happened (errors that apply to several fields), and ``OBJ`` is 302 the created/updated object. 303 """ 304 305 class DefaultObjectConverter(grok.Adapter): 306 """An object converter can apply CSV data to objects. 307 308 Thus, in a way, it can turn CSV data into real objects. 309 """ 274 310 275 311 grok.context(Interface) 276 grok.provides(I SchemaTypeConverter)277 278 def __init__(self, iface , form_fields=None):312 grok.provides(IObjectConverter) 313 314 def __init__(self, iface): 279 315 self.iface = iface 280 self.form_fields = form_fields 281 if self.form_fields is None: 282 self.form_fields = form.Fields(iface) 316 self.form_fields = form.Fields(iface) 283 317 return 284 318 285 def applyRowData(self, data_dict, context): 319 def applyRowData(self, data_dict, context, form_fields=None): 320 if form_fields is None: 321 form_fields = self.form_fields 322 323 obj = context 324 if isinstance(context, basestring): 325 obj = createObject(context) 286 326 request = TestRequest(form={}) 287 #form_fields = form.Fields(self.iface)288 form_fields = self.form_fields289 327 for key, val in data_dict.items(): 290 328 request.form['form.%s' % key] = val 291 329 widgets = form.setUpWidgets( 292 form_fields, 'form', context, request) 293 #errors = form.getWidgetsData(widgets, 'form', data_dict) 330 form_fields, 'form', obj, request) 294 331 errors = getWidgetsData(widgets, 'form', data_dict) 332 err_messages = [] 295 333 if errors: 296 err_message = []297 334 for key, error in errors: 298 message = error [1].args[0]299 err_message .append((key, message))335 message = error.args[0] 336 err_messages.append((key, message)) 300 337 invariant_errors = form.checkInvariants(form_fields, data_dict) 338 invariant_errors = [err.message for err in invariant_errors] 301 339 if not errors and not invariant_errors: 302 340 changed = form.applyChanges( 303 context, form_fields, data_dict)304 return err ors, invariant_errors, data_dict341 obj, form_fields, data_dict) 342 return err_messages, invariant_errors, obj
Note: See TracChangeset for help on using the changeset viewer.