in src/google/appengine/ext/ndb/model.py [0:0]
def _db_set_value(self, v, p, value):
if isinstance(value, six.binary_type):
v.stringValue = value
elif isinstance(value, six.text_type):
v.stringValue = six.ensure_binary(value)
if not self._indexed:
p.meaning = entity_pb2.Property.TEXT
elif isinstance(value, bool):
v.booleanValue = value
elif isinstance(value, six.integer_types):
if not (-_MAX_LONG <= value < _MAX_LONG):
raise TypeError('Property %s can only accept 64-bit integers; '
'received %s' % (self._name, value))
v.int64Value = value
elif isinstance(value, float):
v.doubleValue = value
elif isinstance(value, Key):
ref = value.reference()
rv = v.referencevalue
rv.app = ref.app
if ref.HasField('name_space'):
rv.name_space = ref.name_space
for elem in ref.path.element:
pe = rv.pathelement.add()
if elem.HasField('type'):
pe.type = elem.type
if elem.HasField('id'):
pe.id = elem.id
elif elem.HasField('name'):
pe.name = elem.name
elif isinstance(value, datetime.datetime):
if value.tzinfo is not None:
raise NotImplementedError('Property %s can only support the UTC. '
'Please derive a new Property to support '
'alternative timezones.' % self._name)
dt = value - _EPOCH
ival = dt.microseconds + 1000000 * (dt.seconds + 24 * 3600 * dt.days)
v.int64Value = ival
p.meaning = entity_pb2.Property.GD_WHEN
elif isinstance(value, GeoPt):
p.meaning = entity_pb2.Property.GEORSS_POINT
pv = v.pointvalue
pv.x = value.lat
pv.y = value.lon
elif isinstance(value, users.User):
datastore_types.PackUser(p.name, value, v)
elif isinstance(value, BlobKey):
v.stringValue = six.ensure_binary(str(value))
p.meaning = entity_pb2.Property.BLOBKEY
elif isinstance(value, Model):
set_key = value._key is not None
pb = value._to_pb(set_key=set_key)
value = pb.SerializePartialToString()
v.stringValue = value
p.meaning = entity_pb2.Property.ENTITY_PROTO
elif isinstance(value, _CompressedValue):
value = value.z_val
v.stringValue = value
p.meaning_uri = _MEANING_URI_COMPRESSED
p.meaning = entity_pb2.Property.BLOB
else:
raise NotImplementedError('Property %s does not support %s types.' %
(self._name, type(value)))