async def revisions(self, path, **kwargs): if path.identifier is None: raise exceptions.NotFoundError(str(path)) async with self.request( 'GET', self.build_url('files', path.identifier, 'revisions'), expects=(200, ), throws=exceptions.RevisionsError, ) as resp: data = await resp.json() if data['items']: return [ GoogleDriveRevision(item) for item in reversed(data['items']) ] metadata = await self.metadata(path, raw=True) # Use dummy ID if no revisions found return [ GoogleDriveRevision({ 'modifiedDate': metadata['modifiedDate'], 'id': data['etag'] + settings.DRIVE_IGNORE_VERSION, }) ]
async def revisions( self, path: GoogleDrivePath, # type: ignore **kwargs) -> List[GoogleDriveRevision]: """Returns list of revisions for the file at ``path``. Google Drive will not allow a user to view the revision list of a file if they only have view or commenting permissions. It will return a 403 Unathorized. If that happens, then we construct a recognizable dummy revision based off of the metadata of the current file version. Note: though we explicitly support the case where the revision list is empty, I have yet to see it in practice. The current handling is based on historical behavior. :param GoogleDrivePath path: the path of the file to fetch revisions for :rtype: `list(GoogleDriveRevision)` :return: list of `GoogleDriveRevision` objects representing revisions of the file """ if path.identifier is None: raise exceptions.NotFoundError(str(path)) async with self.request( 'GET', self.build_url('files', path.identifier, 'revisions'), expects=( 200, 403, ), throws=exceptions.RevisionsError, ) as resp: data = await resp.json() has_revisions = resp.status == 200 if has_revisions and data['items']: return [ GoogleDriveRevision(item) for item in reversed(data['items']) ] # Use dummy ID if no revisions found metadata = await self.metadata(path, raw=True) revision = { 'modifiedDate': metadata['modifiedDate'], # type: ignore 'id': metadata['etag'] + pd_settings.DRIVE_IGNORE_VERSION, # type: ignore } return [ GoogleDriveRevision(revision), ]
def test_get_revisions(self, provider): item = fixtures.list_file['items'][0] path = WaterButlerPath('/birdie.jpg', _ids=('doesntmatter', item['id'])) revisions_url = provider.build_url('files', item['id'], 'revisions') aiohttpretty.register_json_uri('GET', revisions_url, body=fixtures.revisions_list) result = yield from provider.revisions(path) expected = [ GoogleDriveRevision(each) for each in fixtures.revisions_list['items'] ] assert result == expected
def test_get_revisions_no_revisions(self, provider): item = fixtures.list_file['items'][0] metadata_url = provider.build_url('files', item['id']) revisions_url = provider.build_url('files', item['id'], 'revisions') path = WaterButlerPath('/birdie.jpg', _ids=('doesntmatter', item['id'])) aiohttpretty.register_json_uri('GET', metadata_url, body=item) aiohttpretty.register_json_uri('GET', revisions_url, body=fixtures.revisions_list_empty) result = yield from provider.revisions(path) expected = [ GoogleDriveRevision({ 'modifiedDate': item['modifiedDate'], 'id': fixtures.revisions_list_empty['etag'] + ds.DRIVE_IGNORE_VERSION, }) ] assert result == expected
def test_revision_metadata(self, revision_fixtures): item = revision_fixtures['revision_metadata'] parsed = GoogleDriveRevision(item) assert parsed.version_identifier == 'revision' assert parsed.version == item['id'] assert parsed.modified == item['modifiedDate']