Exemple #1
0
    def _property_to_field(self, name, property_detail=None):
        value = getattr(self, name)
        if value is None:
            return None

        # configure field's type
        # from user definition
        field_type = None if not property_detail else property_detail.field_type
        field_type = field_type if field_type else self._estimate_type_from_property(
            value)

        if not field_type:
            pass
        elif field_type == FieldType.DATE:
            value = ks.date_to_value(value)
        elif field_type == FieldType.TIME:
            value = ks.time_to_value(value)
        elif field_type in [
                FieldType.DATETIME, FieldType.CREATED_TIME,
                FieldType.UPDATED_TIME, FieldType.TIME_STAMP
        ]:
            # time stamp is same as datetime format (there is no field for timestamp in kintone)
            value = ks.datetime_to_value(value)
        elif field_type == FieldType.USER_SELECT:
            value = self.__map(value, lambda u: u.serialize(), to_list=True)
        elif field_type in [FieldType.CREATOR, FieldType.MODIFIER]:
            value = value.serialize()
        elif field_type == FieldType.SUBTABLE:
            if property_detail and property_detail.sub_type:
                table = []
                for r in value:
                    values = r.to_record()
                    row = {}
                    if "id" in values:
                        _id = values.pop("id")
                        row["id"] = _id["value"]
                    row["value"] = values
                    table.append(row)
                value = table
        elif field_type == FieldType.FILE:
            value = self.__map(value, lambda v: v.serialize(), to_list=True)
        elif field_type == FieldType.STRUCTURE:

            def serialize(v):
                s = getattr(v, "serialize", None)
                return None if not (s and callable(s)) else s()

            value = self.__map(value, lambda v: serialize(v))

        return value
Exemple #2
0
    def _property_to_field(self, name, property_detail=None):
        value = getattr(self, name)
        if value is None:
            return None

        # configure field's type
        # from user definition
        field_type = None if not property_detail else property_detail.field_type
        field_type = field_type if field_type else self._estimate_type_from_property(value)

        if not field_type:
            pass
        elif field_type == FieldType.DATE:
            value = ks.date_to_value(value)
        elif field_type == FieldType.TIME:
            value = ks.time_to_value(value)
        elif field_type in [FieldType.DATETIME, FieldType.CREATED_TIME, FieldType.UPDATED_TIME, FieldType.TIME_STAMP]:
            # time stamp is same as datetime format (there is no field for timestamp in kintone)
            value = ks.datetime_to_value(value)
        elif field_type == FieldType.USER_SELECT:
            value = self.__map(value, lambda u: u.serialize(), to_list=True)
        elif field_type in [FieldType.CREATOR, FieldType.MODIFIER]:
            value = value.serialize()
        elif field_type == FieldType.SUBTABLE:
            if property_detail and property_detail.sub_type:
                table = []
                for r in value:
                    values = r.to_record()
                    row = {}
                    if "id" in values:
                        _id = values.pop("id")
                        row["id"] = _id["value"]
                    row["value"] = values
                    table.append(row)
                value = table
        elif field_type == FieldType.FILE:
            value = self.__map(value, lambda v: v.serialize(), to_list=True)
        elif field_type == FieldType.STRUCTURE:
            def serialize(v):
                s = getattr(v, "serialize", None)
                return None if not (s and callable(s)) else s()

            value = self.__map(value, lambda v: serialize(v))

        return value
Exemple #3
0
    def property_to_field(self, name, property_detail=None):
        value = getattr(self, name)
        if value is None:
            return None

        # configure field's type
        # from user definition
        field_type = None if not property_detail else property_detail.field_type
        # from property's value class
        if not field_type:
            if isinstance(value, datetime):
                field_type = FieldType.DATE

        if not field_type:
            pass
        elif field_type == FieldType.DATE:
            value = ks.date_to_value(value)
        elif field_type == FieldType.TIME:
            value = ks.time_to_value(value)
        elif field_type in [FieldType.DATETIME, FieldType.CREATED_TIME, FieldType.UPDATED_TIME]:
            value = ks.datetime_to_value(value)
        elif field_type == FieldType.USER_SELECT:
            value = [{"code": u.code} for u in value]
        elif field_type in [FieldType.CREATOR, FieldType.MODIFIER]:
            value = {"code": value.code}
        elif field_type == FieldType.SUBTABLE:
            if property_detail and property_detail.sub_type:
                table = []
                for r in value:
                    table.append({
                        "value": r.to_record()
                    })
                value = table
        elif field_type == FieldType.FILE:
            pass  # todo: conversion for file

        return value