Ejemplo n.º 1
0
    def listing(self, file_id=None, **kwargs):
        """
        Lists contents of a folder or details of a file.

        Args:
            file_id: The type/id of the Google Drive Object. This should be formatted {type}/{id}
            where {type} is one of ['folder', 'file'] and {id} is the numeric Google Drive ID for
            the object.

        Returns:
            Dictionary with two keys;
              - resource: File System resource that we're listing
              - files: Array of Api file-like objects
        """

        default_pems = 'ALL'

        try:
            if file_id == '/':
                # top level dir
                file_id = 'root'

            file_type, file_id = self.parse_file_id(file_id)
            fields = "mimeType, name, id, modifiedTime, fileExtension, size, parents"
            googledrive_item = self.googledrive_api.files().get(fileId=file_id, fields=fields).execute()

            child_results = self.googledrive_api.files().list(q="'{}' in parents and trashed=False".format(file_id), fields="files({})".format(fields)).execute()
            if file_type == 'folder':

                children = [GoogleDriveFile(item, parent=googledrive_item, drive=self.googledrive_api).to_dict(default_pems=default_pems)
                            for item in child_results['files']]
                child_folders = sorted([item for item in children if item['type'] == 'folder'], key=lambda k: os.path.splitext(k['name'])[0])
                child_files = sorted([item for item in children if item['type'] == 'file'], key=lambda k: os.path.splitext(k['name'])[0])
                children = child_folders + child_files
            else:
                children = None

            list_data = GoogleDriveFile(googledrive_item, drive=self.googledrive_api).to_dict(default_pems=default_pems)

            if children:
                list_data['children'] = children

            return list_data

        except AssertionError:
            raise ApiException(status=404, message='The file you requested does not exist.')

        except Exception as e:
            if 'invalid_grant' in str(e):
                message = 'While you previously granted this application access to Google Drive, ' \
                    'that grant appears to be no longer valid. Please ' \
                    '<a href="{}">disconnect and reconnect your Google Drive account</a> ' \
                    'to continue using Google Drive data.'.format(reverse('googledrive_integration:index'))
                raise ApiException(status=401, message=message)

            message = 'Unable to communicate with Google Drive: {}'.format(e)
            raise ApiException(status=500, message=message)
Ejemplo n.º 2
0
    def parse_file_id(self, file_id):
        if file_id is not None:
            file_id = file_id.strip('/')
            try:
                file_type, file_id = GoogleDriveFile.parse_file_id(file_id)
            except AssertionError:
                # file path is hierarchical; need to find the GoogleDriveObject here
                logger.debug('parse_file_id, file_id:{}'.format(file_id))
                fields = "mimeType, name, id, modifiedTime, fileExtension, size, parents"
                googledrive_item = GoogleDriveFile(self.googledrive_api.files().get(fileId=file_id, fields=fields).execute(), drive=self.googledrive_api)

                file_type = googledrive_item.type
        else:
            file_type, file_id = 'folder', 'root'

        return file_type, file_id