Exemplo n.º 1
0
    def create_folder(self):
        body = {
            'title': self.name,
            'mimeType': 'application/vnd.google-apps.folder'
        }
        drive = google.drive_api(creds=self.author.google_creds)
        resp = drive.files().insert(body=body).execute()
        folder_id = resp['id']

        # Add permissions for all of this project's users.
        for user in self.users:
            # Exclude the author and the default user.
            if user is not self.author and user.google_id != '0':
                permission = {
                    'value': user.email,
                    'type': 'user',
                    'role': 'writer'
                }
                drive.permissions().insert(
                        fileId=folder_id,
                        sendNotificationEmails=False,
                        body=permission
                ).execute()

        self.folder_id = folder_id
        self.save()
Exemplo n.º 2
0
    def delete_folder(self):
        drive = google.drive_api(creds=self.author.google_creds)
        children = drive.children().list(folderId=self.folder_id).execute()

        # Only delete the folder if it is empty.
        if len(children.get('items', [])) == 0:
            drive.files().delete(fileId=self.folder_id).execute()
Exemplo n.º 3
0
 def url(self):
     drive = google.drive_api(creds=self.project.author.google_creds)
     file = drive.files().get(fileId=self.file_id).execute()
     return {
         'full': file.get('webContentLink'),
         'thumb': file.get('thumbnailLink')
     }
Exemplo n.º 4
0
    def pre_delete(cls, sender, document, **kwargs):
        drive = google.drive_api(creds=document.project.author.google_creds)

        document.project.attachments.remove(document)
        document.project.save()

        document.parent.attachments.remove(document)
        document.parent.save()

        drive.files().delete(fileId=document.file_id).execute()
Exemplo n.º 5
0
    def upload_file(self, filename):
        drive = google.drive_api(creds=self.author.google_creds)
        media_body = MediaFileUpload(filename, resumable=True)

        # If there's no attachment folder, create one.
        if not self.attachment_folder_id:
            body = {
                'title': 'uploads',
                'mimeType': 'application/vnd.google-apps.folder'
            }
            drive = google.drive_api(creds=self.author.google_creds)
            resp = drive.files().insert(body=body).execute()
            self.attachment_folder_id = resp['id']
            self.save()

        body = {
                'title': filename.split('/')[-1],
                'parents': [{'id': self.attachment_folder_id}]
        }
        file = drive.files().insert(body=body, media_body=media_body).execute()
        return file['id']