Skip to content

Commit 0bc3d9a

Browse files
committed
Add Database.revisions() method to iterate over document revisions (issue 99).
Thanks go to Kirk Strauser for the initial patch.
1 parent ed19c02 commit 0bc3d9a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

ChangeLog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ http://couchdb-python.googlecode.com/hg/?r=default
1616
slower) `count()` method (issue 91).
1717
* `schema.ListField` objects now have correct behavior for slicing operations
1818
and the `pop()` method (issue 92).
19+
* Added a `revisions()` method to the Database class (issue 99).
1920

2021

2122
Version 0.6

couchdb/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,26 @@ def get(self, id, default=None, **options):
479479
else:
480480
return Document(data)
481481

482+
def revisions(self, id, **options):
483+
"""Return all available revisions of the given document.
484+
485+
:param id: the document ID
486+
:return: an iterator over Document objects, each a different revision,
487+
in reverse chronological order, if any were found
488+
"""
489+
try:
490+
resp, data = self.resource.get(id, revs=True)
491+
except ResourceNotFound:
492+
return
493+
494+
startrev = data['_revisions']['start']
495+
for index, rev in enumerate(data['_revisions']['ids']):
496+
options['rev'] = '%d-%s' % (startrev - index, rev)
497+
revision = self.get(id, **options)
498+
if revision is None:
499+
return
500+
yield revision
501+
482502
def info(self):
483503
"""Return information about the database as a dictionary.
484504

couchdb/tests/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def test_doc_revs(self):
131131
old_doc = self.db.get('foo', rev=old_rev)
132132
self.assertEqual(old_rev, old_doc['_rev'])
133133

134+
revs = [i for i in self.db.revisions('foo')]
135+
self.assertEqual(revs[0]['_rev'], new_rev)
136+
self.assertEqual(revs[1]['_rev'], old_rev)
137+
134138
self.assertTrue(self.db.compact())
135139
while self.db.info()['compact_running']:
136140
pass

0 commit comments

Comments
 (0)