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

The following are 30 code examples for showing how to use google.appengine.ext.ndb.DateTimeProperty(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

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
Project: luci-py   Author: luci   File: serializable.py    License: 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
Project: luci-py   Author: luci   File: serializable.py    License: 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
Project: luci-py   Author: luci   File: serializable.py    License: 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
Project: jbox   Author: jpush   File: ndb.py    License: MIT License 5 votes vote down vote up
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) 
Example 5
Project: RSSNewsGAE   Author: liantian-cn   File: ndb.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 6
Project: isthislegit   Author: duo-labs   File: util.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_for_dict(self, entity):
        value = super(DateTimeProperty, self)._get_for_dict(entity)
        if not value:
            return None
        return value.isoformat() 
Example 7
Project: graphene-gae   Author: graphql-python   File: test_converter.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testDateTimeProperty_shouldConvertToString(self):
        self.__assert_conversion(ndb.DateTimeProperty, DateTime) 
Example 8
Project: googleapps-message-recall   Author: google   File: ndb.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 9
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: serializable_test.py    License: 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 13
Project: luci-py   Author: luci   File: serializable_test.py    License: 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 14
Project: luci-py   Author: luci   File: serializable_test.py    License: 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 15
Project: luci-py   Author: luci   File: serializable.py    License: 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 16
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: test_case.py    License: 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 23
Project: luci-py   Author: luci   File: test_case.py    License: 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 24
Project: luci-py   Author: luci   File: test_case.py    License: 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 25
Project: luci-py   Author: luci   File: test_case.py    License: 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 26
Project: luci-py   Author: luci   File: test_case.py    License: 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
Project: luci-py   Author: luci   File: serializable_test.py    License: 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 28
Project: luci-py   Author: luci   File: serializable_test.py    License: 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 29
Project: luci-py   Author: luci   File: serializable.py    License: 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 30
Project: luci-py   Author: luci   File: test_case.py    License: 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