Ignore:
Timestamp:
2 Jun 2011, 02:46:07 (13 years ago)
Author:
uli
Message:

Extend converter and create own interface for it.

File:
1 edited

Legend:

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

    r6260 r6263  
    240240
    241241
     242from zope import schema
     243from zope.component import createObject
    242244from zope.interface import Interface
    243245from zope.formlib import form
     
    248250
    249251def getWidgetsData(widgets, form_prefix, data):
    250     # from zope.formlib.form
    251252    errors = []
    252253    form_prefix = expandPrefix(form_prefix)
     
    271272
    272273
    273 class DefaultConverter(Converter):
     274class 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
     305class 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    """
    274310
    275311    grok.context(Interface)
    276     grok.provides(ISchemaTypeConverter)
    277 
    278     def __init__(self, iface, form_fields=None):
     312    grok.provides(IObjectConverter)
     313
     314    def __init__(self, iface):
    279315        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)
    283317        return
    284318
    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)
    286326        request = TestRequest(form={})
    287         #form_fields = form.Fields(self.iface)
    288         form_fields = self.form_fields
    289327        for key, val in data_dict.items():
    290328            request.form['form.%s' % key] = val
    291329        widgets = form.setUpWidgets(
    292             form_fields, 'form', context, request)
    293         #errors = form.getWidgetsData(widgets, 'form', data_dict)
     330            form_fields, 'form', obj, request)
    294331        errors = getWidgetsData(widgets, 'form', data_dict)
     332        err_messages = []
    295333        if errors:
    296             err_message = []
    297334            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))
    300337        invariant_errors = form.checkInvariants(form_fields, data_dict)
     338        invariant_errors = [err.message for err in invariant_errors]
    301339        if not errors and not invariant_errors:
    302340            changed = form.applyChanges(
    303                 context, form_fields, data_dict)
    304         return errors, invariant_errors, data_dict
     341                obj, form_fields, data_dict)
     342        return err_messages, invariant_errors, obj
Note: See TracChangeset for help on using the changeset viewer.