def __call__(self, field):
        """Create and return a new element representing the given field
        """
        data = {
            self.name_attr: field.attrib['name'],
            self.type_attr: self.field_type
        }

        for el in field:
            attr, transform = self.field_attributes.get(
                noNS(el.tag), (None, None))
            if not attr:
                continue
            data[attr] = transform(el.text)

        field_options = {}
        for el in field:
            attr, transform = self.field_options.get(noNS(el.tag),
                                                     (None, None))
            if not attr:
                continue
            field_options[attr] = transform(el.text)

        data[self.options_attr] = field_options
        self.set_extra_data(field, data)
        return data
    def __call__(self, field):
        """Create and return a new element representing the given field
        """
        data = {
            self.name_attr: field.attrib['name'],
            self.type_attr: self.field_type
        }

        for el in field:
            attr, transform = self.field_attributes.get(
                noNS(el.tag),
                (None, None)
            )
            if not attr:
                continue
            data[attr] = transform(el.text)

        field_options = {}
        for el in field:
            attr, transform = self.field_options.get(
                noNS(el.tag),
                (None, None)
            )
            if not attr:
                continue
            field_options[attr] = transform(el.text)

        data[self.options_attr] = field_options
        self.set_extra_data(field, data)
        return data
예제 #3
0
    def read(self, widget_node, params):

        # get all child node attributes in one swoop
        child_nodes = dict(
            (noNS(c.tag), c.text.strip()) for c in widget_node.iterchildren()
            if noNS(c.tag) in self.attributes
        )

        for attr, value in child_nodes.items():
            params[attr] = self.attributes[attr](json.loads(value))
예제 #4
0
 def read(self, widgetNode, params):
     for attributeName, attributeField in self.fieldAttributes.items():
         for node in widgetNode.iterchildren():
             if noNS(node.tag) == attributeName:
                 params[attributeName] = elementToValue(
                     attributeField,
                     node
                 )
예제 #5
0
    def test_baseserializer(self):
        serializer = getUtility(IModelSerializer)

        fields = {
            "fields": []
        }

        model = serializer(json.dumps(fields))

        root = parse(model).getroot()
        self.assertEqual(noNS(root.tag), 'model')
        childrens = [i for i in root.iterchildren()]
        self.assertEqual(len(childrens), 1)

        schema = childrens[0]
        self.assertEqual(noNS(schema.tag), 'schema')
        schema_childrens = [i for i in schema.iterchildren()]
        self.assertEqual(len(schema_childrens), 0)
예제 #6
0
    def read(self, element):
        """Read a field from the element and return a new instance
        """
        attributes = {}
        deferred = {}
        deferred_nonvalidated = {}

        for attribute_element in element.iterchildren(tag=etree.Element):
            parseinfo.stack.append(attribute_element)
            attribute_name = noNS(attribute_element.tag)

            if 'r' in self.filteredAttributes.get(attribute_name, ''):
                continue

            attributeField = self.fieldAttributes.get(attribute_name, None)
            if attributeField is not None:

                if attribute_name in self.fieldTypeAttributes:
                    deferred[attribute_name] = attribute_element

                elif attribute_name in self.nonValidatedfieldTypeAttributes:
                    deferred_nonvalidated[attribute_name] = attribute_element

                elif attribute_name in self.fieldInstanceAttributes:

                    attributeField_type = attribute_element.get('type')
                    handler = queryUtility(IFieldExportImportHandler, name=attributeField_type)

                    if handler is None:
                        raise NotImplementedError(u"Type %s used for %s not supported" %
                            (attributeField_type, attribute_name))

                    attributes[attribute_name] = handler.read(attribute_element)

                else:
                    attributes[attribute_name] = \
                        self.readAttribute(attribute_element, attributeField)
            parseinfo.stack.pop()

        name = element.get('name')
        if name is not None:
            name = str(name)
            attributes['__name__'] = name

        field_instance = self._constructField(attributes)

        # some fields can't validate fully until they're finished setting up
        field_instance._init_field = True

        # Handle those elements that can only be set up once the field is
        # constructed, in the preferred order.
        for attribute_name in self.fieldTypeAttributes:
            if attribute_name in deferred:
                attribute_element = deferred[attribute_name]
                parseinfo.stack.append(attribute_element)
                value = self.readAttribute(attribute_element, field_instance)
                setattr(field_instance, attribute_name, value)
                parseinfo.stack.pop()

        for attribute_name in self.nonValidatedfieldTypeAttributes:
            if attribute_name in deferred_nonvalidated:

                # this is pretty nasty: we need the field's fromUnicode(),
                # but this always validates. The missing_value field may by
                # definition be invalid. Therefore, we need to fake it.

                clone = self.klass.__new__(self.klass)
                clone.__dict__.update(field_instance.__dict__)
                clone.__dict__['validate'] = lambda value: True

                attribute_element = deferred_nonvalidated[attribute_name]
                parseinfo.stack.append(attribute_element)
                value = self.readAttribute(attribute_element, clone)
                setattr(field_instance, attribute_name, value)
                parseinfo.stack.pop()

        field_instance._init_field = True

        if field_instance.defaultFactory is not None:
            # we want to add some additional requirements for defaultFactory.
            # zope.schema will be happy with any function, we'd like to
            # restrict to those that provide IContextAwareDefaultFactory
            # or IDefaultFactory
            if not (
                IContextAwareDefaultFactory.providedBy(field_instance.defaultFactory) or
                IDefaultFactory.providedBy(field_instance.defaultFactory)
                ):
                raise ImportError(u"defaultFactory must provide "
                                           "zope.schema.interfaces.IContextAwareDefaultFactory "
                                           "or plone.supermodel.IDefaultFactory")

        return field_instance
예제 #7
0
    def read(self, element):
        """Read a field from the element and return a new instance
        """
        attributes = {}
        deferred = {}
        deferred_nonvalidated = {}

        for attribute_element in element.iterchildren(tag=etree.Element):
            parseinfo.stack.append(attribute_element)
            attribute_name = noNS(attribute_element.tag)

            if 'r' in self.filteredAttributes.get(attribute_name, ''):
                continue

            attributeField = self.fieldAttributes.get(attribute_name, None)
            if attributeField is not None:

                if attribute_name in self.fieldTypeAttributes:
                    deferred[attribute_name] = attribute_element

                elif attribute_name in self.nonValidatedfieldTypeAttributes:
                    deferred_nonvalidated[attribute_name] = attribute_element

                elif attribute_name in self.fieldInstanceAttributes:

                    attributeField_type = attribute_element.get('type')
                    handler = queryUtility(
                        IFieldExportImportHandler,
                        name=attributeField_type
                    )

                    if handler is None:
                        raise NotImplementedError(
                            u"Type %s used for %s not supported" %
                            (attributeField_type, attribute_name)
                        )

                    attributes[attribute_name] = handler.read(
                        attribute_element
                    )

                else:
                    attributes[attribute_name] = self.readAttribute(
                        attribute_element,
                        attributeField
                    )
            parseinfo.stack.pop()

        name = element.get('name')
        if name is not None:
            name = str(name)
            attributes['__name__'] = name

        field_instance = self._constructField(attributes)

        # some fields can't validate fully until they're finished setting up
        field_instance._init_field = True

        # Handle those elements that can only be set up once the field is
        # constructed, in the preferred order.
        for attribute_name in self.fieldTypeAttributes:
            if attribute_name in deferred:
                attribute_element = deferred[attribute_name]
                parseinfo.stack.append(attribute_element)
                value = self.readAttribute(attribute_element, field_instance)
                setattr(field_instance, attribute_name, value)
                parseinfo.stack.pop()

        for attribute_name in self.nonValidatedfieldTypeAttributes:
            if attribute_name in deferred_nonvalidated:

                # this is pretty nasty: we need the field's fromUnicode(),
                # but this always validates. The missing_value field may by
                # definition be invalid. Therefore, we need to fake it.

                clone = self.klass.__new__(self.klass)
                clone.__dict__.update(field_instance.__dict__)
                clone.__dict__['validate'] = lambda value: True

                attribute_element = deferred_nonvalidated[attribute_name]
                parseinfo.stack.append(attribute_element)
                value = self.readAttribute(attribute_element, clone)
                setattr(field_instance, attribute_name, value)
                parseinfo.stack.pop()

        field_instance._init_field = True

        if field_instance.defaultFactory is not None:
            # we want to add some additional requirements for defaultFactory.
            # zope.schema will be happy with any function, we'd like to
            # restrict to those that provide IContextAwareDefaultFactory
            # or IDefaultFactory
            if not (
                IContextAwareDefaultFactory.providedBy(
                    field_instance.defaultFactory
                ) or
                IDefaultFactory.providedBy(field_instance.defaultFactory)
            ):
                raise ImportError(
                    u"defaultFactory must provide "
                    u"zope.schema.interfaces.IContextAwareDefaultFactory "
                    u"or plone.supermodel.IDefaultFactory"
                )

        return field_instance
예제 #8
0
    def read(self, element):
        """Read a field from the element and return a new instance
        """
        attributes = {}
        deferred = {}
        deferred_nonvalidated = {}

        for attribute_element in element:
            attribute_name = noNS(attribute_element.tag)

            if 'r' in self.filteredAttributes.get(attribute_name, ''):
                continue

            attributeField = self.fieldAttributes.get(attribute_name, None)
            if attributeField is not None:

                if attribute_name in self.fieldTypeAttributes:
                    deferred[attribute_name] = attribute_element

                elif attribute_name in self.nonValidatedfieldTypeAttributes:
                    deferred_nonvalidated[attribute_name] = attribute_element

                elif attribute_name in self.fieldInstanceAttributes:

                    attributeField_type = attribute_element.get('type')
                    handler = queryUtility(IFieldExportImportHandler, name=attributeField_type)

                    if handler is None:
                        raise NotImplementedError(u"Type %s used for %s not supported" %
                            (attributeField_type, attribute_name))

                    attributes[attribute_name] = handler.read(attribute_element)

                else:
                    attributes[attribute_name] = \
                        self.readAttribute(attribute_element, attributeField)

        name = element.get('name')
        if name is not None:
            name = str(name)
            attributes['__name__'] = name

        field_instance = self._constructField(attributes)

        # some fields can't validate fully until they're finished setting up
        field_instance._init_field = True

        # Handle those elements that can only be set up once the field is
        # constructed, in the preferred order.
        for attribute_name in self.fieldTypeAttributes:
            if attribute_name in deferred:
                attribute_element = deferred[attribute_name]
                value = self.readAttribute(attribute_element, field_instance)
                setattr(field_instance, attribute_name, value)

        for attribute_name in self.nonValidatedfieldTypeAttributes:
            if attribute_name in deferred_nonvalidated:

                # this is pretty nasty: we need the field's fromUnicode(),
                # but this always validates. The missing_value field may by
                # definition be invalid. Therefore, we need to fake it.

                clone = self.klass.__new__(self.klass)
                clone.__dict__.update(field_instance.__dict__)
                clone.__dict__['validate'] = lambda value: True

                attribute_element = deferred_nonvalidated[attribute_name]
                value = self.readAttribute(attribute_element, clone)
                setattr(field_instance, attribute_name, value)

        field_instance._init_field = True
        return field_instance
예제 #9
0
    def read(self, element):
        """Read a field from the element and return a new instance
        """
        attributes = {}
        deferred = {}
        deferred_nonvalidated = {}

        for attribute_element in element:
            attribute_name = noNS(attribute_element.tag)

            if 'r' in self.filteredAttributes.get(attribute_name, ''):
                continue

            attributeField = self.fieldAttributes.get(attribute_name, None)
            if attributeField is not None:

                if attribute_name in self.fieldTypeAttributes:
                    deferred[attribute_name] = attribute_element

                elif attribute_name in self.nonValidatedfieldTypeAttributes:
                    deferred_nonvalidated[attribute_name] = attribute_element

                elif attribute_name in self.fieldInstanceAttributes:

                    attributeField_type = attribute_element.get('type')
                    handler = queryUtility(IFieldExportImportHandler,
                                           name=attributeField_type)

                    if handler is None:
                        raise NotImplementedError(
                            u"Type %s used for %s not supported" %
                            (attributeField_type, attribute_name))

                    attributes[attribute_name] = handler.read(
                        attribute_element)

                else:
                    attributes[attribute_name] = \
                        self.readAttribute(attribute_element, attributeField)

        name = element.get('name')
        if name is not None:
            name = str(name)
            attributes['__name__'] = name

        field_instance = self._constructField(attributes)

        # some fields can't validate fully until they're finished setting up
        field_instance._init_field = True

        # Handle those elements that can only be set up once the field is
        # constructed, in the preferred order.
        for attribute_name in self.fieldTypeAttributes:
            if attribute_name in deferred:
                attribute_element = deferred[attribute_name]
                value = self.readAttribute(attribute_element, field_instance)
                setattr(field_instance, attribute_name, value)

        for attribute_name in self.nonValidatedfieldTypeAttributes:
            if attribute_name in deferred_nonvalidated:

                # this is pretty nasty: we need the field's fromUnicode(),
                # but this always validates. The missing_value field may by
                # definition be invalid. Therefore, we need to fake it.

                clone = self.klass.__new__(self.klass)
                clone.__dict__.update(field_instance.__dict__)
                clone.__dict__['validate'] = lambda value: True

                attribute_element = deferred_nonvalidated[attribute_name]
                value = self.readAttribute(attribute_element, clone)
                setattr(field_instance, attribute_name, value)

        field_instance._init_field = True
        return field_instance