Python google.appengine.ext.ndb.DateTimeProperty() Examples

The following are 30 code examples of google.appengine.ext.ndb.DateTimeProperty(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.ext.ndb , or try the search function .
Example #1
Source File: serializable.py    From luci-py with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: serializable.py    From luci-py with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: serializable.py    From luci-py with Apache License 2.0 6 votes vote down vote up
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) 
Example #4
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #5
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #6
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #7
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #8
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #9
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #10
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #11
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #12
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #13
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #14
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #15
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #16
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #17
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #18
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #19
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #20
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #21
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #22
Source File: serializable.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #23
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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'}) 
Example #24
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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]})) 
Example #25
Source File: serializable_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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})) 
Example #26
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #27
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #28
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #29
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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 
Example #30
Source File: test_case.py    From luci-py with Apache License 2.0 5 votes vote down vote up
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