本文整理汇总了Python中google.appengine.ext.ndb.DateTimeProperty方法的典型用法代码示例。如果您正苦于以下问题:Python ndb.DateTimeProperty方法的具体用法?Python ndb.DateTimeProperty怎么用?Python ndb.DateTimeProperty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.appengine.ext.ndb
的用法示例。
在下文中一共展示了ndb.DateTimeProperty方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_serializable_dict
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def from_serializable_dict(cls, serializable_dict, **props):
"""Makes an entity with properties from |serializable_dict| and |props|.
Properties from |serializable_dict| are converted from simple types to
rich types first (e.g. int -> DateTimeProperty). See doc string for
'convert_serializable_dict' method for more details.
Properties from |props| are passed to entity constructor as is. Values in
|props| override values from |serializable_dict|.
Raises ValueError if types or structure of |serializable_dict| doesn't match
entity schema.
"""
try:
all_props = cls.convert_serializable_dict(serializable_dict)
all_props.update(props)
return cls(**all_props)
except datastore_errors.BadValueError as e:
raise ValueError(e)
示例2: convert_DateTimeProperty
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def convert_DateTimeProperty(self, model, prop, kwargs):
"""Returns a form field for a ``ndb.DateTimeProperty``."""
if prop._auto_now or prop._auto_now_add:
return None
return f.DateTimeField(format='%Y-%m-%d %H:%M:%S', **kwargs)
示例3: _get_for_dict
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def _get_for_dict(self, entity):
value = super(DateTimeProperty, self)._get_for_dict(entity)
if not value:
return None
return value.isoformat()
示例4: testDateTimeProperty_shouldConvertToString
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def testDateTimeProperty_shouldConvertToString(self):
self.__assert_conversion(ndb.DateTimeProperty, DateTime)
示例5: mock_now
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def mock_now(test, now, seconds):
"""Mocks utcnow() and ndb properties.
In particular handles when auto_now and auto_now_add are used.
"""
now = now + datetime.timedelta(seconds=seconds)
test.mock(utils, 'utcnow', lambda: now)
test.mock(ndb.DateTimeProperty, '_now', lambda _: now)
test.mock(ndb.DateProperty, '_now', lambda _: now.date())
return now
示例6: test_datetime_properties
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def test_datetime_properties(self):
"""Test handling of DateTimeProperty."""
class Entity(ndb.Model, serializable.SerializableModelMixin):
dt = ndb.DateTimeProperty()
# Same point in time as datetime and as timestamp.
dt = datetime.datetime(2012, 1, 2, 3, 4, 5)
ts = 1325473445000000
# Datetime is serialized to a number of milliseconds since epoch.
self.assertEqual({'dt': ts}, Entity(dt=dt).to_serializable_dict())
# Reverse operation also works.
self.assertEqual({'dt': dt}, Entity.convert_serializable_dict({'dt': ts}))
示例7: test_repeated_properties
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def test_repeated_properties(self):
"""Test that properties with repeated=True are handled."""
class IntsEntity(ndb.Model, serializable.SerializableModelMixin):
ints = ndb.IntegerProperty(repeated=True)
class DatesEntity(ndb.Model, serializable.SerializableModelMixin):
dates = ndb.DateTimeProperty(repeated=True)
# Same point in time as datetime and as timestamp.
dt = datetime.datetime(2012, 1, 2, 3, 4, 5)
ts = 1325473445000000
# Repeated properties that are not set are converted to empty lists.
self.assertEqual({'ints': []}, IntsEntity().to_serializable_dict())
self.assertEqual({'dates': []}, DatesEntity().to_serializable_dict())
# List of ints works (as an example of simple repeated property).
self.assertEqual(
{'ints': [1, 2]},
IntsEntity(ints=[1, 2]).to_serializable_dict())
self.assertEqual(
{'ints': [1, 2]},
IntsEntity.convert_serializable_dict({'ints': [1, 2]}))
# List of datetimes works (as an example of not-so-simple property).
self.assertEqual(
{'dates': [ts, ts]},
DatesEntity(dates=[dt, dt]).to_serializable_dict())
self.assertEqual(
{'dates': [dt, dt]},
DatesEntity.convert_serializable_dict({'dates': [ts, ts]}))
示例8: test_bad_type_for_datetime_property
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def test_bad_type_for_datetime_property(self):
"""Trying to deserialize non-number into DateTimeProperty -> ValueError."""
class Entity(ndb.Model, serializable.SerializableModelMixin):
prop = ndb.DateTimeProperty()
# Works.
Entity.from_serializable_dict({'prop': 123})
# Doesn't.
with self.assertRaises(ValueError):
Entity.from_serializable_dict({'prop': 'abc'})
示例9: __init__
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import DateTimeProperty [as 别名]
def __init__(self, property_converters, field_mode_predicate):
"""Args:
property_converters: sequence of tuples that define how to handle various
NDB property classes.
field_mode_predicate: callable that will be used to decide what properties
to use during conversion. It is called with single integer argument
|mode| which is a value from entity.serializable_properties dictionary
that correspond to property being considered. If |field_mode_predicate|
returns True, then property will be used, otherwise it will be silently
ignored during conversion (i.e. resulting dict will not have it even
if it was present in incoming dict).
Each property converter tuple has 3 components:
* ndb.Property subclass this converter applies to.
* Boolean: True to apply converter to all subclasses or False only to
this specific class.
* Actual converter: function(property instance, from type) -> to type.
For instance when converting rich-typed dict to serializable dict, converter
for DateTimeProperty will be defined as:
(
ndb.DateTimeProperty,
False,
lambda(ndb.DateTimeProperty instance, datetime) -> integer
)
"""
self.property_converters = property_converters
self.field_mode_predicate = field_mode_predicate