Skip to content

Commit c473d63

Browse files
rasashcheklein
andauthored
Add .GetRevisions() option (#297)
* Add .GetRevisions() option * Reformat GetRevisions() comments --------- Co-authored-by: Ivan Shcheklein <[email protected]>
1 parent 9df9761 commit c473d63

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

docs/filemanagement.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ a list of `GoogleDriveFile`_ instances.
192192
# Get list of files that match against the query
193193
files = drive.ListFile(query).GetList()
194194
195+
List revisions
196+
________________
197+
198+
Revisions can be fetched using the ``GetRevisions()`` function of a
199+
``GoogleDriveFile``, and can be used like so:
200+
201+
.. code-block:: python
202+
203+
# Create a new file
204+
file1 = drive.CreateFile()
205+
# Fetch revisions.
206+
revisions = file1.GetRevisions()
207+
print(revisions)
208+
209+
Not all files objects have revisions. If GetRevisions is called on a
210+
file object that does not have revisions, an exception will be raised.
211+
195212
Upload and update file content
196213
------------------------------
197214

pydrive2/files.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,35 @@ def DeletePermission(self, permission_id):
650650
"""
651651
return self._DeletePermission(permission_id)
652652

653+
@LoadAuth
654+
def GetRevisions(self):
655+
"""Get file's or shared drive's revisions.
656+
657+
For files in a shared drive, at most 100 results will be returned.
658+
It doesn't paginate and collect all results.
659+
660+
:return: A list of the revision objects.
661+
:rtype: object[]
662+
"""
663+
file_id = self.metadata.get("id") or self.get("id")
664+
665+
# We can't do FetchMetada call (which would nicely update
666+
# local metada cache, etc) here since it doesn't return
667+
# revisions for the team drive use case.
668+
revisions = (
669+
self.auth.service.revisions()
670+
.list(
671+
fileId=file_id,
672+
)
673+
.execute(http=self.http)
674+
).get("items")
675+
676+
if revisions:
677+
self["revisions"] = revisions
678+
self.metadata["revisions"] = revisions
679+
680+
return revisions
681+
653682
def _WrapRequest(self, request):
654683
"""Replaces request.http with self.http.
655684

pydrive2/test/test_file.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,19 @@ def test_Files_Delete_Permission_Invalid(self):
598598

599599
pydrive_retry(file1.Delete)
600600

601+
def test_Files_Get_Revisions(self):
602+
drive = GoogleDrive(self.ga)
603+
file1 = drive.CreateFile()
604+
pydrive_retry(file1.Upload)
605+
606+
self.assertFalse("revisions" in file1)
607+
608+
revisions = pydrive_retry(file1.GetRevisions)
609+
self.assertTrue(revisions is not None)
610+
self.assertTrue("revisions" in file1)
611+
612+
pydrive_retry(file1.Delete)
613+
601614
def test_ApiRequestError_HttpError_Propagation(self):
602615
file = GoogleDrive(self.ga).CreateFile()
603616
pydrive_retry(file.Upload)

0 commit comments

Comments
 (0)