本文整理匯總了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