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

The following are 30 code examples of google.appengine.ext.ndb.StringProperty(). 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: handler_utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testQueryModel_TranslatePropertyQuery(self):
    class Foo(ndb.Model):
      foo = ndb.StringProperty()

      @classmethod
      def TranslatePropertyQuery(cls, field, term):
        return 'foo', 'bar'

    class FooQueryHandler(handler_utils.UserFacingQueryHandler):
      MODEL_CLASS = Foo

    # Request a nonsense query to be ignored by TranslatePropertyQuery.
    with self.LoggedInUser():
      q = FooQueryHandler()._QueryModel({'bar': 'baz'})
    # Create an entity satisfying the translated query.
    Foo(foo='bar').put()
    # Ensure the translated query finds the created entity.
    self.assertIsNotNone(q.get()) 
Example #2
Source File: config_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_expiration(self):
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6))

    class Config(config.GlobalConfig):
      param = ndb.StringProperty(default='default')

    # Bootstrap the config.
    Config.cached()

    # fetch-update cycle, necessary to avoid modifying cached copy in-place.
    conf = Config.fetch()
    conf.param = 'new-value'
    conf.store(updated_by='someone')

    # Right before expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 59)
    self.assertEqual('default', Config.cached().param)

    # After expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 61)
    self.assertEqual('new-value', Config.cached().param) 
Example #3
Source File: snippets.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def query_purchase_with_ancestor_key():
    # [START purchase_with_ancestor_key_models]
    class Customer(ndb.Model):
        name = ndb.StringProperty()

    class Purchase(ndb.Model):
        price = ndb.IntegerProperty()
    # [END purchase_with_ancestor_key_models]

    def create_purchase_for_customer_with_ancestor(customer_entity):
        purchase = Purchase(parent=customer_entity.key)
        return purchase

    def query_for_purchases_of_customer_with_ancestor(customer_entity):
        purchases = Purchase.query(ancestor=customer_entity.key).fetch()
        return purchases

    return (Customer, Purchase,
            create_purchase_for_customer_with_ancestor,
            query_for_purchases_of_customer_with_ancestor) 
Example #4
Source File: snippets.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def query_purchase_with_customer_key():
    # [START purchase_with_customer_key_models]
    class Customer(ndb.Model):
        name = ndb.StringProperty()

    class Purchase(ndb.Model):
        customer = ndb.KeyProperty(kind=Customer)
        price = ndb.IntegerProperty()
    # [END purchase_with_customer_key_models]

    def query_purchases_for_customer_via_key(customer_entity):
        purchases = Purchase.query(
            Purchase.customer == customer_entity.key).fetch()
        return purchases

    return Customer, Purchase, query_purchases_for_customer_via_key 
Example #5
Source File: config_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_expiration(self):
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6))

    class Config(config.GlobalConfig):
      param = ndb.StringProperty(default='default')

    # Bootstrap the config.
    Config.cached()

    # fetch-update cycle, necessary to avoid modifying cached copy in-place.
    conf = Config.fetch()
    conf.param = 'new-value'
    conf.store(updated_by='someone')

    # Right before expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 59)
    self.assertEqual('default', Config.cached().param)

    # After expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 61)
    self.assertEqual('new-value', Config.cached().param) 
Example #6
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testSameSchema(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty()

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeleteProperty(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is gone :)
    self.assertIsNone(inst.b) 
Example #7
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testSameSchema_DoesntDeleteProperty(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty()

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    # Delete the property and save the entity
    datastore_utils.DeleteProperty(inst, 'b')
    inst.put()

    # Create a new instance and verify that the 'b' hasn't disappeared
    new = A(a='abc', b='def')
    new.put()
    self.assertTrue(datastore_utils.HasProperty(new, 'b')) 
Example #8
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testSameSchema_RepeatedProperty(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty(repeated=True)

    # Create an entity using the initial schema
    inst = A(a='abc', b=['def'])
    inst.put()

    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeleteProperty(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is...kinda gone :|
    self.assertEqual([], inst.b) 
Example #9
Source File: recall_task.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def SetTaskState(cls, task_key_id, new_state, is_aborted=True):
    """Utility method to update the state of the master task record.

    Args:
      task_key_id: String (serializable) unique id of the task record.
      new_state: String update for the ndb StringProperty field.
      is_aborted: Boolean; False when performing final update.
    """
    task = cls.GetTaskByKey(task_key_id)
    if task:
      if task.task_state != TASK_DONE:
        task.task_state = new_state
        if new_state == TASK_DONE:
          _LOG.warning('RecallTaskModel id=%s Done.', task_key_id)
      task.is_aborted = is_aborted
      task.put() 
Example #10
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testDatetimeAutoNowAdd(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.DateTimeProperty(auto_now_add=True)

    # Create an entity using the initial schema
    inst = A(a='abc')
    inst.put()

    # Delete the property and save the entity
    datastore_utils.DeletePropertyValue(inst, 'b')
    inst.put()

    self.assertTrue(datastore_utils.HasProperty(inst, 'b'))
    self.assertIsNotNone(inst.b) 
Example #11
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testRepeatedProperty(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty(repeated=True)

    # Create an entity using the initial schema
    inst = A(a='abc', b=['def'])
    inst.put()

    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeletePropertyValue(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is gone
    self.assertEqual([], inst.b) 
Example #12
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testRequiredField(self):

    # Initial schema but this time with a required property
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty(required=True)

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    # Delete the property and save the entity
    datastore_utils.DeletePropertyValue(inst, 'b')
    # Property required but no longer has a value.
    with self.assertRaises(Exception):
      inst.put() 
Example #13
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testHasValue(self):

    class Foo(ndb.Model):
      a = ndb.ComputedProperty(lambda self: 'a')
      b = ndb.StringProperty()

    foo = Foo()
    self.assertFalse(datastore_utils.HasValue(foo, 'a'))
    self.assertFalse(datastore_utils.HasValue(foo, 'b'))

    foo.b = 'b'
    self.assertFalse(datastore_utils.HasValue(foo, 'a'))
    self.assertTrue(datastore_utils.HasValue(foo, 'b'))

    foo.put()
    self.assertTrue(datastore_utils.HasValue(foo, 'a'))
    self.assertTrue(datastore_utils.HasValue(foo, 'b')) 
Example #14
Source File: config_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_expiration(self):
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6))

    class Config(config.GlobalConfig):
      param = ndb.StringProperty(default='default')

    # Bootstrap the config.
    Config.cached()

    # fetch-update cycle, necessary to avoid modifying cached copy in-place.
    conf = Config.fetch()
    conf.param = 'new-value'
    conf.store(updated_by='someone')

    # Right before expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 59)
    self.assertEqual('default', Config.cached().param)

    # After expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 61)
    self.assertEqual('new-value', Config.cached().param) 
Example #15
Source File: config_test.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def test_expiration(self):
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6))

    class Config(config.GlobalConfig):
      param = ndb.StringProperty(default='default')

    # Bootstrap the config.
    Config.cached()

    # fetch-update cycle, necessary to avoid modifying cached copy in-place.
    conf = Config.fetch()
    conf.param = 'new-value'
    conf.store(updated_by='someone')

    # Right before expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 59)
    self.assertEqual('default', Config.cached().param)

    # After expiration.
    self.mock_now(datetime.datetime(2014, 1, 2, 3, 4, 5, 6), 61)
    self.assertEqual('new-value', Config.cached().param) 
Example #16
Source File: utils_test.py    From upvote with Apache License 2.0 6 votes vote down vote up
def testDeleteValue(self):

    # Initial schema
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty()

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeletePropertyValue(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is gone :)
    self.assertIsNone(inst.b) 
Example #17
Source File: ndb.py    From jbox with MIT License 5 votes vote down vote up
def get_TextField(kwargs):
    """
    Returns a ``TextField``, applying the ``ndb.StringProperty`` length limit
    of 500 bytes.
    """
    kwargs['validators'].append(validators.length(max=500))
    return f.TextField(**kwargs) 
Example #18
Source File: utils_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def testPolyModel_NoClass(self):
    class A(datastore_utils.polymodel.PolyModel):
      a = ndb.StringProperty()

    class B(A):
      pass

    inst = B(a='abc')
    a_copy = datastore_utils.CopyEntity(inst, a='xyz')
    a_copy.put()
    inst.put()

    self.assertEqual('xyz', a_copy.a)
    self.assertEqual('abc', inst.a) 
Example #19
Source File: utils_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def testChangeSchema_PolyModel(self):

    # Initial schema
    class Base(polymodel.PolyModel):
      a = ndb.StringProperty()
      b = ndb.StringProperty(required=True)

    class A(Base):
      pass

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    # Revised schema
    class Base(polymodel.PolyModel):  # pylint: disable=function-redefined
      a = ndb.StringProperty()

    class A(Base):  # pylint: disable=function-redefined
      pass

    # Retrieve and save the old instance
    inst = A.get_by_id(inst.key.id())
    inst.put()

    # The old data is still there :(
    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeleteProperty(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is gone :)
    self.assertIsNone(inst.b) 
Example #20
Source File: utils_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(GetLocalComputedPropertyValueTest, self).setUp()

    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.ComputedProperty(lambda self: self.a[0])

    self.inst = A(a='xyz') 
Example #21
Source File: singleton_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def testGetAndSet(self):

    class A(singleton.Singleton):
      a = ndb.StringProperty()

    self.assertIsNone(A.GetInstance())

    inst = A.SetInstance(a='abcd')
    self.assertEqual('abcd', inst.a)

    inst = A.GetInstance()
    self.assertEqual('abcd', inst.a)
    self.assertEqual('A', A.GetInstance().key.id()) 
Example #22
Source File: singleton_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def testOverrideGetId(self):

    class A(singleton.Singleton):
      a = ndb.StringProperty()

      @classmethod
      def _GetId(cls):
        return '1'

    inst = A.SetInstance(a='abcd')
    self.assertEqual('1', inst.key.id()) 
Example #23
Source File: utils_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def testChangeSchema_RequiredField(self):

    # Initial schema but this time with a required property
    class A(ndb.Model):
      a = ndb.StringProperty()
      b = ndb.StringProperty(required=True)

    # Create an entity using the initial schema
    inst = A(a='abc', b='def')
    inst.put()

    # Revised schema without the required property
    class A(ndb.Model):  # pylint: disable=function-redefined
      a = ndb.StringProperty()

    # Retrieve and save the old instance
    inst = A.get_by_id(inst.key.id())
    inst.put()

    # The old data is still there :(
    self.assertIsNotNone(inst.b)

    # Delete the property and save the entity
    datastore_utils.DeleteProperty(inst, 'b')
    inst.put()
    inst = A.get_by_id(inst.key.id())

    # The old data is gone :)
    self.assertIsNone(inst.b) 
Example #24
Source File: config_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_bootstrap(self):
    class Config(config.GlobalConfig):
      param = ndb.StringProperty()
      def set_defaults(self):
        self.param = 'abc'
    conf = Config.cached()
    self.assertEqual('abc', conf.param)
    self.assertEqual(conf.to_dict(), conf.fetch().to_dict()) 
Example #25
Source File: utils_test.py    From upvote with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(CopyEntityTest, self).setUp()

    class A(ndb.Model):
      a = ndb.StringProperty()

    self.default_model = A 
Example #26
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def _to_default_reques_value(descriptor, name, index):
    if isinstance(descriptor, (StringProperty, TextProperty)):
        return "'%s_string'" % name
    if isinstance(descriptor, DateProperty):
        return "'1/%s/2014'" % (index + 1)
    if isinstance(descriptor, DateTimeProperty):
        return "'1/1/2014 01:%s:0'" % (index + 1)
    if isinstance(descriptor, (SimpleCurrency, SimpleDecimal)):
        return "'1.%s'" % (index + 1 if index >= 9 else '0%s' % (index + 1))
    if isinstance(descriptor, IntegerProperty):
        return "'%s'" % (index + 1)
    if isinstance(descriptor, FloatProperty):
        return "'1.%s'" % (index + 1)
    if isinstance(descriptor, BooleanProperty):
        return "'True'" 
Example #27
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def _to_default_model_value(descriptor, name, index):
    if isinstance(descriptor, (StringProperty, TextProperty)):
        return "'%s_string'" % name
    if isinstance(descriptor, DateProperty):
        return "date(2014, 1, %s)" % (index + 1)
    if isinstance(descriptor, DateTimeProperty):
        return "datetime(2014, 1, 1, 1, %s, 0)" % (index + 1)
    if isinstance(descriptor, (SimpleCurrency, SimpleDecimal)):
        return "Decimal('1.%s')" % (index + 1 if index >= 9 else '0%s' % (index + 1))
    if isinstance(descriptor, IntegerProperty):
        return "%s" % (index + 1)
    if isinstance(descriptor, FloatProperty):
        return "1.%s" % (index + 1)
    if isinstance(descriptor, BooleanProperty):
        return "True" 
Example #28
Source File: manager.py    From tekton with MIT License 5 votes vote down vote up
def parse_property(p):
    name, type_alias = p.split(':')
    types = {'string': 'ndb.StringProperty(required=True)',
             'date': 'ndb.DateProperty(required=True)',
             'datetime': 'ndb.DateTimeProperty(required=True)',
             'int': 'ndb.IntegerProperty(required=True)',
             'float': 'ndb.FloatProperty(required=True)',
             'decimal': 'property.SimpleDecimal(required=True)',
             'currency': 'property.SimpleCurrency(required=True)',
             'bool': 'ndb.BooleanProperty(required=True)'}
    return '    %s = %s' % (name, types[type_alias]) 
Example #29
Source File: config_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_fetch_store(self):
    class Config(config.GlobalConfig):
      param = ndb.StringProperty()
    conf = Config.fetch()
    self.assertIsNone(conf)
    conf = Config.cached()
    self.assertIsNotNone(conf)
    conf.param = '1234'
    now = self.mock_now(datetime.datetime(2010, 1, 1))
    conf.store(updated_by='someone')
    self.mock_now(datetime.datetime(2010, 1, 1), 100)
    conf = Config.fetch()
    self.assertEqual('1234', conf.param)
    self.assertEqual(now, conf.updated_ts) 
Example #30
Source File: config_test.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def test_bootstrap(self):
    class Config(config.GlobalConfig):
      param = ndb.StringProperty()
      def set_defaults(self):
        self.param = 'abc'
    conf = Config.cached()
    self.assertEqual('abc', conf.param)
    self.assertEqual(conf.to_dict(), conf.fetch().to_dict())