def _field_to_property(cls, field_value, field_type=None, property_detail=None, initial_value=None): value = field_value # configure property's field type # from user definition _field_type = None if not property_detail else property_detail.field_type # from type value in field if not _field_type and field_type: f = [e for e in list(FieldType) if e.value == field_type] if len(f) > 0: _field_type = f[0] _field_type = _field_type if _field_type else cls._estimate_type_from_property(initial_value) if not _field_type: pass elif _field_type in (FieldType.ID, FieldType.REVISION, FieldType.RECORD_NUMBER): value = int(value) elif _field_type == FieldType.NUMBER: value = float(value) elif _field_type == FieldType.DATE: value = ks.value_to_date(value) elif _field_type == FieldType.TIME: value = ks.value_to_time(value) elif _field_type in [FieldType.DATETIME, FieldType.CREATED_TIME, FieldType.UPDATED_TIME]: value = ks.value_to_datetime(value) elif _field_type == FieldType.TIME_STAMP: value = ks.value_to_timestamp(value) elif _field_type == FieldType.USER_SELECT: value = cls.__map(value, lambda v: sf.UserSelect.deserialize(v), flatten=True) elif _field_type in [FieldType.CREATOR, FieldType.MODIFIER]: value = sf.UserSelect.deserialize(value) elif _field_type == FieldType.SUBTABLE: if property_detail and property_detail.sub_type: table = [] for r in value: row = property_detail.sub_type().record_to_model(r["value"]) row.record_id = int(r["id"]) table.append(row) value = table elif _field_type == FieldType.FILE: value = cls.__map(value, lambda v: sf.File.deserialize(v), flatten=True) elif _field_type == FieldType.STRUCTURE: cls_type = None if property_detail and property_detail.sub_type: cls_type = property_detail.sub_type elif initial_value: cls_type = cls.__get_type(initial_value) if cls_type: def deserialize(v): ins = cls_type() ds = getattr(ins, "deserialize", None) return None if not (ds and callable(ds)) else ds(v) value = cls.__map(value, lambda v: deserialize(v)) return value
def field_to_property(cls, field, property_detail=None): value = field["value"] # configure property's field type # from user definition field_type = None if not property_detail else property_detail.field_type # from type value in field if not field_type: f = [e for e in list(FieldType) if e.value == field["type"]] if len(f) > 0: field_type = f[0] if not field_type: pass elif field_type in (FieldType.ID, FieldType.REVISION, FieldType.RECORD_NUMBER): value = int(value) elif field_type == FieldType.NUMBER: value = float(value) elif field_type == FieldType.DATE: value = ks.value_to_date(value) elif field_type == FieldType.TIME: value = ks.value_to_time(value) elif field_type in [FieldType.DATETIME, FieldType.CREATED_TIME, FieldType.UPDATED_TIME]: value = ks.value_to_datetime(value) elif field_type == FieldType.USER_SELECT: value = [UserSelect(v["code"], v["name"]) for v in value] elif field_type in [FieldType.CREATOR, FieldType.MODIFIER]: value = UserSelect(value["code"], value["name"]) elif field_type == FieldType.SUBTABLE: if property_detail and property_detail.sub_type: table = [] for r in value: row = property_detail.sub_type().record_to_model(r["value"]) row.record_id = int(r["id"]) table.append(row) value = table elif field_type == FieldType.FILE: pass # todo: conversion for file return value
def _field_to_property(cls, field_value, field_type=None, property_detail=None, initial_value=None): value = field_value # configure property's field type # from user definition _field_type = None if not property_detail else property_detail.field_type # from type value in field if not _field_type and field_type: f = [e for e in list(FieldType) if e.value == field_type] if len(f) > 0: _field_type = f[0] _field_type = _field_type if _field_type else cls._estimate_type_from_property( initial_value) if not _field_type: pass elif _field_type in (FieldType.ID, FieldType.REVISION, FieldType.RECORD_NUMBER): value = int(value) elif _field_type == FieldType.NUMBER: value = float(value) elif _field_type == FieldType.DATE: value = ks.value_to_date(value) elif _field_type == FieldType.TIME: value = ks.value_to_time(value) elif _field_type in [ FieldType.DATETIME, FieldType.CREATED_TIME, FieldType.UPDATED_TIME ]: value = ks.value_to_datetime(value) elif _field_type == FieldType.TIME_STAMP: value = ks.value_to_timestamp(value) elif _field_type == FieldType.USER_SELECT: value = cls.__map(value, lambda v: sf.UserSelect.deserialize(v), flatten=True) elif _field_type in [FieldType.CREATOR, FieldType.MODIFIER]: value = sf.UserSelect.deserialize(value) elif _field_type == FieldType.SUBTABLE: if property_detail and property_detail.sub_type: table = [] for r in value: row = property_detail.sub_type().record_to_model( r["value"]) row.record_id = int(r["id"]) table.append(row) value = table elif _field_type == FieldType.FILE: value = cls.__map(value, lambda v: sf.File.deserialize(v), flatten=True) elif _field_type == FieldType.STRUCTURE: cls_type = None if property_detail and property_detail.sub_type: cls_type = property_detail.sub_type elif initial_value: cls_type = cls.__get_type(initial_value) if cls_type: def deserialize(v): ins = cls_type() ds = getattr(ins, "deserialize", None) return None if not (ds and callable(ds)) else ds(v) value = cls.__map(value, lambda v: deserialize(v)) return value