def toFieldValue(self, value):
        """field value is an Object type, that provides schema"""
        if value is NO_VALUE:
            return self.field.missing_value

        if self.widget.subform is None:
            #creepy situation when the widget is hanging in nowhere
            obj = self.createObject(value)
        else:
            if self.widget.subform.ignoreContext:
                obj = self.createObject(value)
            else:
                dm = zope.component.getMultiAdapter(
                    (self.widget.context, self.field), IDataManager)
                try:
                    obj = dm.get()
                except KeyError:
                    obj = {} # self.createObject(value)
                except AttributeError:
                    obj = {} # self.createObject(value)

        if obj is None or obj == self.field.missing_value:
            #if still None create one, otherwise following will burp
            obj = {}

        # don't need to adapt our schema since the dictionary converter
        # will deal with that (never mind how dict do not get adapted).

        # obj = self.schema(obj)

        names = []
        for name in zope.schema.getFieldNames(self.schema):
            try:
                dm = zope.component.getMultiAdapter(
                    (obj, self.schema[name]), IDataManager)
                oldval = dm.query()
                if (oldval != value[name]
                    or zope.schema.interfaces.IObject.providedBy(
                        self.schema[name])
                    ):
                    dm.set(value[name])
                    names.append(name)
            except KeyError:
                pass

        # XXX workaround for JSON import
        try:
            obj = scrub_json_unicode_to_string(obj)
        except:
            # Not really our problem... although it will be nice if
            # we can reset this and then notify end user.
            pass

        if names:
            zope.event.notify(
                zope.lifecycleevent.ObjectModifiedEvent(obj,
                    zope.lifecycleevent.Attributes(self.schema, *names)))
        return obj
예제 #2
0
 def _annotate(self, data):
     # XXX wholesale reimplementation of core method.  Once a proper
     # filter is implemented by parent class we can avoid doing this.
     note = self.note
     try:
         for a, v in data:
             # XXX figure out how to gracefully handle schema errors
             # (such as missing values).
             if a == 'flags':
                 v = scrub_json_unicode_to_string(v)
             setattr(note, a, v)
     except TypeError:
         raise TypeError('%s.generate failed to return a list of ' \
                         'tuple(key, value)' % self.__class__)
     except ValueError:
         raise ValueError('%s.generate returned invalid values (not ' \
                          'list of tuple(key, value)' % self.__class__)