Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit bbdd5d3

Browse files
authored
Merge pull request #435 from aogier/feature/only-one-id
Removed document._document_id logic
2 parents 1ffcd54 + e3e83d0 commit bbdd5d3

File tree

3 files changed

+11
-32
lines changed

3 files changed

+11
-32
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
uncaught exceptions being raised inside `with` block.
55
- [FIXED] Fixed parameter type of `selector` in docstring.
66
- [IMPROVED] Updated `Getting started` section with a `get_query_result` example.
7+
- [FIXED] Removed internal `Document._document_id` property to allow a safe use of
8+
dict's methods.
79

810
# 2.11.0 (2019-01-21)
911

src/cloudant/document.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ def __init__(self, database, document_id=None, **kwargs):
6363
self._database = database
6464
self._database_host = self._client.server_url
6565
self._database_name = database.database_name
66-
self._document_id = document_id
67-
if self._document_id is not None:
68-
self['_id'] = self._document_id
66+
if document_id:
67+
self['_id'] = document_id
6968
self.encoder = kwargs.get('encoder') or self._client.encoder
7069
self.decoder = kwargs.get('decoder') or json.JSONDecoder
7170

@@ -85,23 +84,23 @@ def document_url(self):
8584
8685
:returns: Document URL
8786
"""
88-
if self._document_id is None:
87+
if '_id' not in self or self['_id'] is None:
8988
return None
9089

9190
# handle design document url
92-
if self._document_id.startswith('_design/'):
91+
if self['_id'].startswith('_design/'):
9392
return '/'.join((
9493
self._database_host,
9594
url_quote_plus(self._database_name),
9695
'_design',
97-
url_quote(self._document_id[8:], safe='')
96+
url_quote(self['_id'][8:], safe='')
9897
))
9998

10099
# handle document url
101100
return '/'.join((
102101
self._database_host,
103102
url_quote_plus(self._database_name),
104-
url_quote(self._document_id, safe='')
103+
url_quote(self['_id'], safe='')
105104
))
106105

107106
def exists(self):
@@ -111,7 +110,7 @@ def exists(self):
111110
:returns: True if the document exists in the remote database,
112111
otherwise False
113112
"""
114-
if self._document_id is None:
113+
if '_id' not in self or self['_id'] is None:
115114
return False
116115

117116
resp = self.r_session.head(self.document_url)
@@ -136,8 +135,6 @@ def create(self):
136135
updates the locally cached Document object with the ``_id``
137136
and ``_rev`` returned as part of the successful response.
138137
"""
139-
if self._document_id is not None:
140-
self['_id'] = self._document_id
141138

142139
# Ensure that an existing document will not be "updated"
143140
doc = dict(self)
@@ -152,7 +149,6 @@ def create(self):
152149
)
153150
resp.raise_for_status()
154151
data = response_to_json_dict(resp)
155-
self._document_id = data['id']
156152
super(Document, self).__setitem__('_id', data['id'])
157153
super(Document, self).__setitem__('_rev', data['rev'])
158154

@@ -318,8 +314,9 @@ def delete(self):
318314
params={"rev": self["_rev"]},
319315
)
320316
del_resp.raise_for_status()
317+
_id = self['_id']
321318
self.clear()
322-
self.__setitem__('_id', self._document_id)
319+
self['_id'] = _id
323320

324321
def __enter__(self):
325322
"""
@@ -349,23 +346,6 @@ def __exit__(self, exc_type, exc_value, traceback):
349346
if exc_type is None:
350347
self.save()
351348

352-
def __setitem__(self, key, value):
353-
"""
354-
Sets the _document_id when setting the '_id' field.
355-
The _document_id is used to construct the document url.
356-
"""
357-
if key == '_id':
358-
self._document_id = value
359-
super(Document, self).__setitem__(key, value)
360-
361-
def __delitem__(self, key):
362-
"""
363-
Sets the _document_id to None when deleting the '_id' field.
364-
"""
365-
if key == '_id':
366-
self._document_id = None
367-
super(Document, self).__delitem__(key)
368-
369349
def get_attachment(
370350
self,
371351
attachment,

tests/unit/document_tests.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,8 @@ def test_setting_id(self):
680680
"""
681681
doc = Document(self.db)
682682
self.assertIsNone(doc.get('_id'))
683-
self.assertEqual(doc._document_id, None)
684683
doc['_id'] = 'julia006'
685684
self.assertEqual(doc['_id'], 'julia006')
686-
self.assertEqual(doc._document_id, 'julia006')
687685

688686
def test_removing_id(self):
689687
"""
@@ -693,7 +691,6 @@ def test_removing_id(self):
693691
doc['_id'] = 'julia006'
694692
del doc['_id']
695693
self.assertIsNone(doc.get('_id'))
696-
self.assertEqual(doc._document_id, None)
697694

698695
def test_get_text_attachment(self):
699696
"""

0 commit comments

Comments
 (0)