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

The following are 30 code examples for showing how to use google.appengine.ext.ndb.Model(). 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: browserscope   Author: elsigh   File: input_readers.py    License: Apache License 2.0 6 votes vote down vote up
def _validate_filters_ndb(cls, filters, model_class):
    """Validate ndb.Model filters."""
    if not filters:
      return

    properties = model_class._properties

    for f in filters:
      prop, _, val = f
      if prop not in properties:
        raise errors.BadReaderParamsError(
            "Property %s is not defined for entity type %s",
            prop, model_class._get_kind())

      # Validate the value of each filter. We need to know filters have
      # valid value to carry out splits.
      try:
        properties[prop]._do_validate(val)
      except db.BadValueError, e:
        raise errors.BadReaderParamsError(e) 
Example 2
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example 3
Project: locality-sensitive-hashing   Author: singhj   File: model_datastore_input_reader.py    License: MIT License 6 votes vote down vote up
def _validate_filters_ndb(cls, filters, model_class):
    """Validate ndb.Model filters."""
    if not filters:
      return

    properties = model_class._properties

    for f in filters:
      prop, _, val = f
      if prop not in properties:
        raise errors.BadReaderParamsError(
            "Property %s is not defined for entity type %s",
            prop, model_class._get_kind())

      # Validate the value of each filter. We need to know filters have
      # valid value to carry out splits.
      try:
        properties[prop]._do_validate(val)
      except db.BadValueError, e:
        raise errors.BadReaderParamsError(e) 
Example 4
Project: locality-sensitive-hashing   Author: singhj   File: input_readers.py    License: MIT License 6 votes vote down vote up
def _validate_filters_ndb(cls, filters, model_class):
    """Validate ndb.Model filters."""
    if not filters:
      return

    properties = model_class._properties

    for f in filters:
      prop, _, val = f
      if prop not in properties:
        raise errors.BadReaderParamsError(
            "Property %s is not defined for entity type %s",
            prop, model_class._get_kind())

      # Validate the value of each filter. We need to know filters have
      # valid value to carry out splits.
      try:
        properties[prop]._do_validate(val)
      except db.BadValueError, e:
        raise errors.BadReaderParamsError(e) 
Example 5
Project: splunk-ref-pas-code   Author: splunk   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example 6
Project: sndlatr   Author: Schibum   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example 7
Project: billing-export-python   Author: googlearchive   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example 8
Project: graphene-gae   Author: graphql-python   File: types.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def is_type_of(cls, root, info):
        if isinstance(root, cls):
            return True

        if not isinstance(root, ndb.Model):
            raise Exception(('Received incompatible instance "{}".').format(root))

        # Returns True if `root` is a PolyModel subclass and `cls` is in the
        # class hierarchy of `root` which is retrieved with `_class_key`
        if (hasattr(root, '_class_key') and
            hasattr(cls._meta.model, '_class_key') and
            set(cls._meta.model._class_key()).issubset(
                set(root._class_key()))):
            return True

        return type(root) == cls._meta.model 
Example 9
Project: googleapps-message-recall   Author: google   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, model, key_name, property_name, cache=None, user=None):
    """Constructor for Storage.

    Args:
      model: db.Model or ndb.Model, model class
      key_name: string, key name for the entity that has the credentials
      property_name: string, name of the property that is a CredentialsProperty
        or CredentialsNDBProperty.
      cache: memcache, a write-through cache to put in front of the datastore.
        If the model you are using is an NDB model, using a cache will be
        redundant since the model uses an instance cache and memcache for you.
      user: users.User object, optional. Can be used to grab user ID as a
        key_name if no key name is specified.
    """
    if key_name is None:
      if user is None:
        raise ValueError('StorageByKeyName called with no key name or user.')
      key_name = user.user_id()

    self._model = model
    self._key_name = key_name
    self._property_name = property_name
    self._cache = cache 
Example 10
Project: datastore-ndb-python   Author: GoogleCloudPlatform   File: msgprop.py    License: Apache License 2.0 6 votes vote down vote up
def _from_base_type(self, ent):
    """Convert a Model instance (entity) to a Message value."""
    if ent._projection:
      # Projection query result.  Reconstitute the message from the fields.
      return _projected_entity_to_message(ent, self._message_type)

    blob = ent.blob_
    if blob is not None:
      protocol = self._protocol_impl
    else:
      # Perhaps it was written using a different protocol.
      protocol = None
      for name in _protocols_registry.names:
        key = '__%s__' % name
        if key in ent._values:
          blob = ent._values[key]
          if isinstance(blob, model._BaseValue):
            blob = blob.b_val
          protocol = _protocols_registry.lookup_by_name(name)
          break
    if blob is None or protocol is None:
      return None  # This will reveal the underlying dummy model.
    msg = protocol.decode_message(self._message_type, blob)
    return msg 
Example 11
Project: datastore-ndb-python   Author: GoogleCloudPlatform   File: msgprop.py    License: Apache License 2.0 6 votes vote down vote up
def _message_to_entity(msg, modelclass):
  """Recursive helper for _to_base_type() to convert a message to an entity.

  Args:
    msg: A Message instance.
    modelclass: A Model subclass.

  Returns:
    An instance of modelclass.
  """
  ent = modelclass()
  for prop_name, prop in modelclass._properties.iteritems():
    if prop._code_name == 'blob_':  # TODO: Devise a cleaner test.
      continue  # That's taken care of later.
    value = getattr(msg, prop_name)
    if value is not None and isinstance(prop, model.StructuredProperty):
      if prop._repeated:
        value = [_message_to_entity(v, prop._modelclass) for v in value]
      else:
        value = _message_to_entity(value, prop._modelclass)
    setattr(ent, prop_name, value)
  return ent 
Example 12
Project: python-docs-samples   Author: GoogleCloudPlatform   File: datastore_test.py    License: Apache License 2.0 6 votes vote down vote up
def testEventuallyConsistentGlobalQueryResult(self):
        class TestModel(ndb.Model):
            pass

        user_key = ndb.Key('User', 'ryan')

        # Put two entities
        ndb.put_multi([
            TestModel(parent=user_key),
            TestModel(parent=user_key)
        ])

        # Global query doesn't see the data.
        self.assertEqual(0, TestModel.query().count(3))
        # Ancestor query does see the data.
        self.assertEqual(2, TestModel.query(ancestor=user_key).count(3))
# [END HRD_example_1]

    # [START HRD_example_2] 
Example 13
Project: python-docs-samples   Author: GoogleCloudPlatform   File: datastore_test.py    License: Apache License 2.0 6 votes vote down vote up
def testDeterministicOutcome(self):
        # 50% chance to apply.
        self.policy.SetProbability(.5)
        # Use the pseudo random sequence derived from seed=2.
        self.policy.SetSeed(2)

        class TestModel(ndb.Model):
            pass

        TestModel().put()

        self.assertEqual(0, TestModel.query().count(3))
        self.assertEqual(0, TestModel.query().count(3))
        # Will always be applied before the third query.
        self.assertEqual(1, TestModel.query().count(3))
    # [END HRD_example_2]


# [START main] 
Example 14
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets.py    License: 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 15
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets.py    License: 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 16
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets.py    License: Apache License 2.0 6 votes vote down vote up
def create_entity_with_parent_keys():
    account_key = ndb.Key(Account, 'sandy@example.com')

    # Ask Datastore to allocate an ID.
    new_id = ndb.Model.allocate_ids(size=1, parent=account_key)[0]

    # Datastore returns us an integer ID that we can use to create the message
    # key
    message_key = ndb.Key('Message', new_id, parent=account_key)

    # Now we can put the message into Datastore
    initial_revision = Revision(
        message_text='Hello', id='1', parent=message_key)
    initial_revision.put()

    return initial_revision 
Example 17
Project: luci-py   Author: luci   File: appengine.py    License: Apache License 2.0 6 votes vote down vote up
def _is_ndb(self):
        """Determine whether the model of the instance is an NDB model.

        Returns:
            Boolean indicating whether or not the model is an NDB or DB model.
        """
        # issubclass will fail if one of the arguments is not a class, only
        # need worry about new-style classes since ndb and db models are
        # new-style
        if isinstance(self._model, type):
            if ndb is not None and issubclass(self._model, ndb.Model):
                return True
            elif issubclass(self._model, db.Model):
                return False

        raise TypeError('Model class not an NDB or DB model: %s.' %
                        (self._model,)) 
Example 18
Project: luci-py   Author: luci   File: monotonic_test.py    License: Apache License 2.0 6 votes vote down vote up
def test_store_new_version_extra(self):
    # Includes an unrelated entity in the PUT. It must be in the same entity
    # group.
    cls = monotonic.get_versioned_root_model('fidoula')
    parent = ndb.Key(cls, 'foo')
    class Unrelated(ndb.Model):
      b = ndb.IntegerProperty()
    unrelated = Unrelated(id='bar', parent=parent, b=42)
    actual = monotonic.store_new_version(
        EntityX(a=1, parent=parent), cls, extra=[unrelated])
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID), actual)
    actual = monotonic.store_new_version(EntityX(a=2, parent=parent), cls)
    self.assertEqual(
        ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID - 1), actual)
    self.assertEqual({'b': 42}, unrelated.key.get().to_dict()) 
Example 19
Project: luci-py   Author: luci   File: sharding.py    License: Apache License 2.0 6 votes vote down vote up
def shard_key(key, number_of_letters, root_entity_type):
  """Returns an ndb.Key to a virtual entity of type |root_entity_type|.

  This key is to be used as an entity group for database sharding. Transactions
  can be done over this group. Note that this sharding root entity doesn't have
  to ever exist in the database.

  Arguments:
    key: full key to take a subset of. It must be '[0-9a-f]+'. It is assumed
        that this key is well distributed, if not, use hashed_shard_key()
        instead. This means the available number of buckets is done in
        increments of 4 bits, e.g. 16, 256, 4096, 65536.
    number_of_letters: number of letters to use from |key|. key length must be
        encoded through an out-of-band mean and be constant.
    root_entity_type: root entity type. It can be either a reference to a
        ndb.Model class or just a string.
  """
  assert _HEX.issuperset(key), key
  assert isinstance(key, str) and len(key) >= number_of_letters, repr(key)
  # number_of_letters==10 means 1099511627776 shards, which is unreasonable.
  assert 1 <= number_of_letters < 10, number_of_letters
  assert isinstance(root_entity_type, (ndb.Model, str)) and root_entity_type, (
      root_entity_type)
  return ndb.Key(root_entity_type, key[:number_of_letters]) 
Example 20
Project: luci-py   Author: luci   File: monotonic.py    License: Apache License 2.0 6 votes vote down vote up
def get_versioned_most_recent_with_root_async(cls, root_key):
  """Returns the most recent instance of a versioned entity and the root entity.

  Getting the root entity is needed to get the current index.
  """
  # Using a cls.query(ancestor=root_key).get() would work too but is less
  # efficient since it can't be cached by ndb's cache.
  assert not ndb.in_transaction()
  assert issubclass(cls, ndb.Model), cls
  assert root_key is None or isinstance(root_key, ndb.Key), root_key

  root = root_key.get()
  if not root or not root.current:
    raise ndb.Return(None, None)
  entity = yield ndb.Key(cls, root.current, parent=root_key).get_async()
  raise ndb.Return(root, entity) 
Example 21
Project: loaner   Author: google   File: tag_model.py    License: Apache License 2.0 5 votes vote down vote up
def _delete_tags(model, tag, cursor=None, num_updated=0, batch_size=100):
  """Cleans up any entities on the given model that reference the given key.

  Args:
    model: ndb.Model, a Model with a repeated TagData property.
    tag: Tag, an instance of a Tag model.
    cursor: Optional[datastore_query.Cursor], pointing to the last result.
    num_updated: int, the number of entities that were just updated.
    batch_size: int, the number of entities to include in the batch.
  """
  entities, next_cursor, more = model.query(
      model.tags.tag == tag).fetch_page(batch_size, start_cursor=cursor)

  for entity in entities:
    entity.tags = [
        model_tag for model_tag in entity.tags if model_tag.tag != tag
    ]
  ndb.put_multi(entities)

  num_updated += len(entities)
  logging.info(
      'Destroyed %d occurrence(s) of the tag with URL safe key %r',
      len(entities), tag.key.urlsafe())
  if more:
    deferred.defer(
        _delete_tags, model, tag,
        cursor=next_cursor, num_updated=num_updated, batch_size=batch_size)
  else:
    logging.info(
        'Destroyed a total of %d occurrence(s) of the tag with URL safe key %r',
        num_updated, tag.key.urlsafe()) 
Example 22
Project: loaner   Author: google   File: bigquery_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_generate_entity_schema(self):

    class NestedTestModel(ndb.Model):
      nested_string_attribute = ndb.StringProperty()

    class TestModel(ndb.Model):
      string_attribute = ndb.StringProperty()
      integer_attribute = ndb.IntegerProperty()
      boolean_attribute = ndb.BooleanProperty()
      nested_attribute = ndb.StructuredProperty(NestedTestModel)

    schema = bigquery._generate_entity_schema(TestModel())
    expected_schema_names = _populate_schema_names(self.entity_schema)
    schema_names = _populate_schema_names(schema)
    self.assertCountEqual(expected_schema_names, schema_names) 
Example 23
Project: browserscope   Author: elsigh   File: context.py    License: Apache License 2.0 5 votes vote down vote up
def _normalize_entity(value):
  """Return an entity from an entity or model instance."""
  if ndb is not None and isinstance(value, ndb.Model):
    return None
  if getattr(value, "_populate_internal_entity", None):
    return value._populate_internal_entity()
  return value 
Example 24
Project: browserscope   Author: elsigh   File: context.py    License: Apache License 2.0 5 votes vote down vote up
def _normalize_key(value):
  """Return a key from an entity, model instance, key, or key string."""
  if ndb is not None and isinstance(value, (ndb.Model, ndb.Key)):
    return None
  if getattr(value, "key", None):
    return value.key()
  elif isinstance(value, basestring):
    return datastore.Key(value)
  else:
    return value 
Example 25
Project: browserscope   Author: elsigh   File: context.py    License: Apache License 2.0 5 votes vote down vote up
def ndb_put(self, entity):
    """Like put(), but for NDB entities."""
    assert ndb is not None and isinstance(entity, ndb.Model)
    entity_size = len(entity._to_pb().Encode())
    if (self.ndb_puts.length >= self.max_entity_count or
        (self.ndb_puts.size + entity_size) > self.max_pool_size):
      self.__flush_ndb_puts()
    self.ndb_puts.append(entity, entity_size) 
Example 26
Project: browserscope   Author: elsigh   File: context.py    License: Apache License 2.0 5 votes vote down vote up
def ndb_delete(self, entity_or_key):
    """Like delete(), but for NDB entities/keys."""
    if isinstance(entity_or_key, ndb.Model):
      key = entity_or_key.key
    else:
      key = entity_or_key
    key_size = len(key.reference().Encode())
    if (self.ndb_deletes.length >= self.max_entity_count or
        (self.ndb_deletes.size + key_size) > self.max_pool_size):
      self.__flush_ndb_deletes()
    self.ndb_deletes.append(key, key_size)

  # TODO(user): some kind of error handling/retries is needed here. 
Example 27
Project: browserscope   Author: elsigh   File: input_readers.py    License: Apache License 2.0 5 votes vote down vote up
def validate(cls, mapper_spec):
    """Inherit docs."""
    super(DatastoreInputReader, cls).validate(mapper_spec)
    params = _get_params(mapper_spec)
    entity_kind = params[cls.ENTITY_KIND_PARAM]
    # Fail fast if Model cannot be located.
    try:
      model_class = util.for_name(entity_kind)
    except ImportError, e:
      raise BadReaderParamsError("Bad entity kind: %s" % e) 
Example 28
Project: browserscope   Author: elsigh   File: input_readers.py    License: Apache License 2.0 5 votes vote down vote up
def _validate_filters(cls, filters, model_class):
    """Validate user supplied filters.

    Validate filters are on existing properties and filter values
    have valid semantics.

    Args:
      filters: user supplied filters. Each filter should be a list or tuple of
        format (<property_name_as_str>, <query_operator_as_str>,
        <value_of_certain_type>). Value type is up to the property's type.
      model_class: the db.Model class for the entity type to apply filters on.

    Raises:
      BadReaderParamsError: if any filter is invalid in any way.
    """
    if not filters:
      return

    properties = model_class.properties()

    for f in filters:
      prop, _, val = f
      if prop not in properties:
        raise errors.BadReaderParamsError(
            "Property %s is not defined for entity type %s",
            prop, model_class.kind())

      # Validate the value of each filter. We need to know filters have
      # valid value to carry out splits.
      try:
        properties[prop].validate(val)
      except db.BadValueError, e:
        raise errors.BadReaderParamsError(e) 
Example 29
Project: jbox   Author: jpush   File: ndb.py    License: MIT License 5 votes vote down vote up
def model_form(model, base_class=Form, only=None, exclude=None, field_args=None,
               converter=None):
    """
    Creates and returns a dynamic ``wtforms.Form`` class for a given
    ``ndb.Model`` class. The form class can be used as it is or serve as a base
    for extended form classes, which can then mix non-model related fields,
    subforms with other model forms, among other possibilities.

    :param model:
        The ``ndb.Model`` class to generate a form for.
    :param base_class:
        Base form class to extend from. Must be a ``wtforms.Form`` subclass.
    :param only:
        An optional iterable with the property names that should be included in
        the form. Only these properties will have fields.
    :param exclude:
        An optional iterable with the property names that should be excluded
        from the form. All other properties will have fields.
    :param field_args:
        An optional dictionary of field names mapping to keyword arguments
        used to construct each field object.
    :param converter:
        A converter to generate the fields based on the model properties. If
        not set, ``ModelConverter`` is used.
    """
    # Extract the fields from the model.
    field_dict = model_fields(model, only, exclude, field_args, converter)

    # Return a dynamically created form class, extending from base_class and
    # including the created fields as properties.
    return type(model._get_kind() + 'Form', (base_class,), field_dict) 
Example 30
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 5 votes vote down vote up
def _is_ndb(self):
    """Determine whether the model of the instance is an NDB model.

    Returns:
      Boolean indicating whether or not the model is an NDB or DB model.
    """
    # issubclass will fail if one of the arguments is not a class, only need
    # worry about new-style classes since ndb and db models are new-style
    if isinstance(self._model, type):
      if ndb is not None and issubclass(self._model, ndb.Model):
        return True
      elif issubclass(self._model, db.Model):
        return False

    raise TypeError('Model class not an NDB or DB model: %s.' % (self._model,))