|
14 | 14 |
|
15 | 15 | """Class for representing a single entity in the Cloud Datastore.""" |
16 | 16 |
|
17 | | -from gcloud.datastore import _implicit_environ |
18 | | - |
19 | | - |
20 | | -class NoKey(RuntimeError): |
21 | | - """Exception raised by Entity methods which require a key.""" |
22 | | - |
23 | 17 |
|
24 | 18 | class Entity(dict): |
25 | 19 | """Entities are akin to rows in a relational database |
@@ -70,8 +64,8 @@ class Entity(dict): |
70 | 64 | any decoding / encoding step. |
71 | 65 |
|
72 | 66 | :type key: :class:`gcloud.datastore.key.Key` |
73 | | - :param key: Optional key to be set on entity. Required for :meth:`save()` |
74 | | - or :meth:`reload()`. |
| 67 | + :param key: Optional key to be set on entity. Required for |
| 68 | + :func:`gcloud.datastore.put()` |
75 | 69 |
|
76 | 70 | :type exclude_from_indexes: tuple of string |
77 | 71 | :param exclude_from_indexes: Names of fields whose values are not to be |
@@ -104,55 +98,6 @@ def exclude_from_indexes(self): |
104 | 98 | """ |
105 | 99 | return frozenset(self._exclude_from_indexes) |
106 | 100 |
|
107 | | - @property |
108 | | - def _must_key(self): |
109 | | - """Return our key, or raise NoKey if not set. |
110 | | -
|
111 | | - :rtype: :class:`gcloud.datastore.key.Key`. |
112 | | - :returns: The entity's key. |
113 | | - :raises: :class:`NoKey` if no key is set. |
114 | | - """ |
115 | | - if self.key is None: |
116 | | - raise NoKey() |
117 | | - return self.key |
118 | | - |
119 | | - def save(self, connection=None): |
120 | | - """Save the entity in the Cloud Datastore. |
121 | | -
|
122 | | - .. note:: |
123 | | - Any existing properties for the entity will be replaced by those |
124 | | - currently set on this instance. Already-stored properties which do |
125 | | - not correspond to keys set on this instance will be removed from |
126 | | - the datastore. |
127 | | -
|
128 | | - .. note:: |
129 | | - Property values which are "text" (``unicode`` in Python2, ``str`` in |
130 | | - Python3) map to 'string_value' in the datastore; values which are |
131 | | - "bytes" (``str`` in Python2, ``bytes`` in Python3) map to |
132 | | - 'blob_value'. |
133 | | -
|
134 | | - :type connection: :class:`gcloud.datastore.connection.Connection` |
135 | | - :param connection: Optional connection used to connect to datastore. |
136 | | - """ |
137 | | - connection = connection or _implicit_environ.CONNECTION |
138 | | - |
139 | | - key = self._must_key |
140 | | - assigned, new_id = connection.save_entity( |
141 | | - dataset_id=key.dataset_id, |
142 | | - key_pb=key.to_protobuf(), |
143 | | - properties=dict(self), |
144 | | - exclude_from_indexes=self.exclude_from_indexes) |
145 | | - |
146 | | - # If we are in a transaction and the current entity needs an |
147 | | - # automatically assigned ID, tell the transaction where to put that. |
148 | | - transaction = connection.transaction() |
149 | | - if transaction and key.is_partial: |
150 | | - transaction.add_auto_id_entity(self) |
151 | | - |
152 | | - if assigned: |
153 | | - # Update the key (which may have been altered). |
154 | | - self.key = self.key.completed_key(new_id) |
155 | | - |
156 | 101 | def __repr__(self): |
157 | 102 | if self.key: |
158 | 103 | return '<Entity%s %s>' % (self.key.path, |
|
0 commit comments