-
Notifications
You must be signed in to change notification settings - Fork 134
Google Slides & Google Drive #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5066e21
initial commit
baadc24
working code
8e1de17
tests and more
587b90f
updates
1fa27f0
remove bin
30a4f9a
shauna comments
a001133
docs
3c81c14
title
4dd1053
api
61014fc
goodbye flake8
a993d58
Merge branch 'main' into google-slides
shaunagm c93dbb7
Merge branch 'main' into google-slides
elyse-weiss 68c3685
getting tests to work
e663a1e
Merge branch 'main' into google-slides
elyse-weiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,4 +125,4 @@ bill_com_credentials.* | |
|
|
||
| docs/html | ||
| docs/dirhtml | ||
| *.sw* | ||
| *.sw* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import os | ||
| import json | ||
| import logging | ||
|
|
||
| from parsons.google.utitities import setup_google_application_credentials | ||
| from googleapiclient.discovery import build | ||
| from google.oauth2.service_account import Credentials | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class GoogleDrive: | ||
| """ | ||
| A connector for Google Drive, largely handling permissions. | ||
|
|
||
| `Args:` | ||
| google_keyfile_dict: dict | ||
| A dictionary of Google Drive API credentials, parsed from JSON provided | ||
| by the Google Developer Console. Required if env variable | ||
| ``GOOGLE_DRIVE_CREDENTIALS`` is not populated. | ||
| subject: string | ||
| In order to use account impersonation, pass in the email address of the account to be | ||
| impersonated as a string. | ||
| """ | ||
|
|
||
| def __init__(self, google_keyfile_dict=None, subject=None): | ||
|
|
||
| scope = ["https://www.googleapis.com/auth/drive"] | ||
|
|
||
| setup_google_application_credentials( | ||
| google_keyfile_dict, "GOOGLE_DRIVE_CREDENTIALS" | ||
| ) | ||
| google_credential_file = open(os.environ["GOOGLE_DRIVE_CREDENTIALS"]) | ||
| credentials_dict = json.load(google_credential_file) | ||
|
|
||
| credentials = Credentials.from_service_account_info( | ||
| credentials_dict, scopes=scope, subject=subject | ||
| ) | ||
|
Comment on lines
+30
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @austinweisgrau I previously had: If I follow your code from the other PR, will the following work too: |
||
|
|
||
| self.client = build("drive", "v3", credentials=credentials) | ||
|
|
||
| def get_permissions(self, file_id): | ||
| """ | ||
| `Args:` | ||
| file_id: str | ||
| this is the ID of the object you are hoping to share | ||
| `Returns:` | ||
| permission dict | ||
| """ | ||
|
|
||
| p = self.client.permissions().list(fileId=file_id).execute() | ||
|
|
||
| return p | ||
|
|
||
| def _share_object(self, file_id, permission_dict): | ||
|
|
||
| # Send the request to share the file | ||
| p = ( | ||
| self.client.permissions() | ||
| .create(fileId=file_id, body=permission_dict) | ||
| .execute() | ||
| ) | ||
|
|
||
| return p | ||
|
|
||
| def share_object(self, file_id, email_addresses=None, role="reader", type="user"): | ||
| """ | ||
| `Args:` | ||
| file_id: str | ||
| this is the ID of the object you are hoping to share | ||
| email_addresses: list | ||
| this is the list of the email addresses you want to share; | ||
| set to a list of domains like `['domain']` if you choose `type='domain'`; | ||
| set to `None` if you choose `type='anyone'` | ||
| role: str | ||
| Options are -- owner, organizer, fileOrganizer, writer, commenter, reader | ||
| https://developers.google.com/drive/api/guides/ref-roles | ||
| type: str | ||
| Options are -- user, group, domain, anyone | ||
| `Returns:` | ||
| List of permission objects | ||
| """ | ||
| if role not in [ | ||
| "owner", | ||
| "organizer", | ||
| "fileOrganizer", | ||
| "writer", | ||
| "commenter", | ||
| "reader", | ||
| ]: | ||
| raise Exception( | ||
| f"{role} not from the allowed list of: \ | ||
| owner, organizer, fileOrganizer, writer, commenter, reader" | ||
| ) | ||
|
|
||
| if type not in ["user", "group", "domain", "anyone"]: | ||
| raise Exception( | ||
| f"{type} not from the allowed list of: \ | ||
| user, group, domain, anyone" | ||
| ) | ||
|
|
||
| if type == "domain": | ||
| permissions = [ | ||
| {"type": type, "role": role, "domain": email} | ||
| for email in email_addresses | ||
| ] | ||
| else: | ||
| permissions = [ | ||
| {"type": type, "role": role, "emailAddress": email} | ||
| for email in email_addresses | ||
| ] | ||
|
|
||
| new_permissions = [] | ||
| for permission in permissions: | ||
| p = self._share_object(file_id, permission) | ||
| new_permissions.append(p) | ||
|
|
||
| return new_permissions | ||
|
|
||
| def transfer_ownership(self, file_id, new_owner_email): | ||
| """ | ||
| `Args:` | ||
| file_id: str | ||
| this is the ID of the object you are hoping to share | ||
| new_owner_email: str | ||
| the email address of the intended new owner | ||
| `Returns:` | ||
| None | ||
| """ | ||
| permissions = self.client.permissions().list(fileId=file_id).execute() | ||
|
|
||
| # Find the current owner | ||
| current_owner_permission = next( | ||
| (p for p in permissions.get("permissions", []) if "owner" in p), None | ||
| ) | ||
|
|
||
| if current_owner_permission: | ||
| # Update the permission to transfer ownership | ||
| new_owner_permission = { | ||
| "type": "user", | ||
| "role": "owner", | ||
| "emailAddress": new_owner_email, | ||
| } | ||
| self.client.permissions().update( | ||
| fileId=file_id, | ||
| permissionId=current_owner_permission["id"], | ||
| body=new_owner_permission, | ||
| ).execute() | ||
| logger.info(f"Ownership transferred successfully to {new_owner_email}.") | ||
| else: | ||
| logger.info("File does not have a current owner.") | ||
|
|
||
| return None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.