serialize_schema_field.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:plone.server 作者: plone 项目源码 文件源码
def __call__(self):
        result = {'type': self.field_type}
        for schema in implementedBy(self.field.__class__).flattened():
            self.field_attributes.update(getFields(schema))
        for attribute_name in sorted(self.field_attributes.keys()):
            attribute_field = self.field_attributes[attribute_name]
            if attribute_name in self.filtered_attributes:
                continue

            element_name = attribute_field.__name__
            attribute_field = attribute_field.bind(self.field)
            force = (element_name in self.forced_fields)

            value = attribute_field.get(self.field)

            # For 'default', 'missing_value' etc, we want to validate against
            # the imported field type itself, not the field type of the
            # attribute
            if element_name in self.field_type_attributes or \
                    element_name in self.non_validated_field_type_attributes:
                attribute_field = self.field

            text = None
            if isinstance(value, bytes):
                text = value.decode('utf-8')
            elif isinstance(value, str):
                text = value
            elif IField.providedBy(value):
                serializer = getMultiAdapter(
                    (value, self.field, self.request),
                    ISchemaFieldSerializeToJson)
                text = serializer()
            elif value is not None and (force or value != self.field.missing_value):
                text = IValueToJson(value)

            # handle i18n
            # if isinstance(value, Message):
            #     child.set(ns('domain', I18N_NAMESPACE), value.domain)
            #     if not value.default:
            #         child.set(ns('translate', I18N_NAMESPACE), '')
            #     else:
            #         child.set(ns('translate', I18N_NAMESPACE), child.text)
            #         child.text = converter.toUnicode(value.default)

            if text:
                if attribute_name == 'value_type':
                    attribute_name = 'items'
                result[attribute_name] = text

        if result['type'] == 'object':
            if isinstance(self.field.schema, dict):
                result['properties'] = self.field.schema
            else:
                schema_serializer = getMultiAdapter((self.field.schema, self.request),
                                                    ISchemaSerializeToJson)
                result['properties'] = schema_serializer()
        return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号