Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix issue #82
  • Loading branch information
bryanyang0528 committed Dec 7, 2014
commit 7b8b6814c1c084e0f0e89ef24bde476d32f14652
33 changes: 28 additions & 5 deletions gcloud/datastore/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from gcloud.datastore.entity import Entity
from gcloud.datastore.query import Query
from gcloud.datastore.transaction import Transaction
from gcloud.datastore.key import Key


class Dataset(object):
Expand Down Expand Up @@ -78,12 +79,17 @@ def query(self, *args, **kwargs):
kwargs['dataset'] = self
return Query(*args, **kwargs)

def entity(self, kind, exclude_from_indexes=()):
def entity(self, kind_key_path, exclude_from_indexes=()):
"""Create an entity bound to this dataset.

:type kind: string
:type kind_key_path: string, :class:`glcouddatastore.key.Key`, or path
:param kind: the "kind" of the new entity (see
https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Kinds_and_identifiers)
or put the "key" you want to set on the entity or the
"sequence" of even length, where the first of each pair
is a string representing the 'kind' of the path element,
and the second of the pair is either a string (for the
path element's name) or an integer (for its id).

:param exclude_from_indexes: names of fields whose values are not to
be indexed.
Expand All @@ -92,9 +98,26 @@ def entity(self, kind, exclude_from_indexes=()):
:returns: a new Entity instance, bound to this dataset.
"""

if isinstance(kind, basestring):
return Entity(dataset=self, kind=kind,
exclude_from_indexes=exclude_from_indexes)
if isinstance(kind_key_path, basestring):
return Entity(
dataset=self, kind=kind_key_path,
exclude_from_indexes=exclude_from_indexes)

elif isinstance(kind_key_path, Key):
kind = kind_key_path.kind()
return Entity(
dataset=self, kind=kind,
exclude_from_indexes=exclude_from_indexes).key(kind_key_path)

elif isinstance(kind_key_path, list):
key = Key.from_path(*kind_key_path)
kind = key.kind()
return Entity(
dataset=self, kind=kind,
exclude_from_indexes=exclude_from_indexes).key(key)

else:
raise ValueError('Must pass a kind, key or path.')

def transaction(self, *args, **kwargs):
"""Create a transaction bound to this dataset.
Expand Down