-
Couldn't load subscription status.
- Fork 3
Create new copydoc file if webpage is new #60
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
24bd073
Create new copydoc file if webpage is new
samhotep 1cdd670
conditionally create a new folder
samhotep d5a58aa
Fixed linter issues
samhotep dc95eea
Updated requirements
samhotep f09f131
Put copydoc in subfolder
samhotep 5488028
Use hardcoded values for the google drive folder and copydoc template
samhotep db773b6
Store credentials in a temp file
samhotep afaee11
Moved google creds to env variable
samhotep c8246a6
Use demo database URL
samhotep 4311ff9
Reformat site.yaml
samhotep faf111b
Point production to dedicated db
samhotep ee921a3
Removed secret key definition
samhotep 02682bb
Updated demo secrets
samhotep 33a37be
Moved drive env variables to settings
samhotep 3f22c42
Removed redundant code
samhotep 1ee286d
Updated google drive module
samhotep a0047b2
Use physical creds
samhotep 13eb0f5
Removed service account env variable
samhotep c1ce393
Pass only private key as env
samhotep 471bd36
declared env variables explicitly for python ci
samhotep c401f38
dont duplicate subfolders
samhotep ce84b45
Added private key to demo
samhotep 39db5be
Returned file url after copydoc creation
samhotep 99410c0
Use explicit naming for private keys
samhotep e410d2b
Merge branch 'main' into create-copydocs-endpoint
samhotep 3447d26
Update CI.yml
samhotep 1642cbe
Added env variables to playwright
samhotep 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
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,123 @@ | ||
| import base64 | ||
| import os | ||
| import tempfile | ||
|
|
||
| from google.oauth2 import service_account | ||
| from googleapiclient.discovery import build | ||
| from googleapiclient.errors import HttpError | ||
|
|
||
|
|
||
| class GoogleDriveClient: | ||
| # If modifying these scopes, delete the file token.json. | ||
| SCOPES = [ | ||
| "https://www.googleapis.com/auth/drive.metadata.readonly", | ||
| "https://www.googleapis.com/auth/drive.file", | ||
| ] | ||
| GOOGLE_DRIVE_FOLDER_ID = ( | ||
| "0B4s80tIYQW4BMjNiMGFmNzQtNDkxZC00YmQ0LWJiZWUtNTk2YThlY2MzZmJh" | ||
| ) | ||
| COPYD0C_TEMPLATE_ID = "1EPA_Ea8ShIvyftAc9oVxZYUIMHfAPFF6S5x6FOvLkwM" | ||
|
|
||
| def __init__(self): | ||
| self.credentials = self._get_credentials() | ||
| self.service = self._build_service() | ||
|
|
||
| def _get_credentials(self): | ||
| """ | ||
| Load credentials from a base64 encoded environment variable. | ||
| """ | ||
| credentials_text = os.getenv("GOOGLE_SERVICE_ACCOUNT") | ||
| with tempfile.NamedTemporaryFile(delete_on_close=False) as f: | ||
| f.write(base64.decode(credentials_text)) | ||
| f.close() | ||
|
|
||
| return service_account.Credentials.from_service_account_file( | ||
| f.name, | ||
| scopes=self.SCOPES, | ||
| ) | ||
|
|
||
| def _build_service(self): | ||
| return build("drive", "v3", credentials=self.credentials) | ||
|
|
||
| def create_copydoc_from_template(self, webpage): | ||
| """ | ||
| Create a copydoc from a template. The document is created in the folder | ||
| for the webpage project. | ||
| """ | ||
| parents = [self.GOOGLE_DRIVE_FOLDER_ID] | ||
| # Create a folder if it does not exist | ||
| if not self.find_folder(webpage.project.name): | ||
| parents = [self.create_folder(webpage.project.name)] | ||
| try: | ||
| copy_metadata = { | ||
| "name": webpage.url, | ||
| "parents": parents, | ||
| } | ||
| copy = ( | ||
| self.service.files() | ||
| .copy( | ||
| fileId=self.COPYD0C_TEMPLATE_ID, | ||
| body=copy_metadata, | ||
| ) | ||
| .execute() | ||
| ) | ||
| return copy | ||
| except HttpError as error: | ||
| print(f"An error occurred: {error}") | ||
| return None | ||
|
|
||
| def create_folder(self, name): | ||
| """ | ||
| Create a folder in the Google Drive. | ||
| """ | ||
| try: | ||
| folder_metadata = { | ||
| "name": name, | ||
| "mimeType": "application/vnd.google-apps.folder", | ||
| "parents": [self.GOOGLE_DRIVE_FOLDER_ID], | ||
| } | ||
| folder = ( | ||
| self.service.files() | ||
| .create(body=folder_metadata, fields="id") | ||
| .execute() | ||
| ) | ||
| return folder | ||
| except HttpError as error: | ||
| print(f"An error occurred: {error}") | ||
| return None | ||
|
|
||
| def find_folder(service, folder_name): | ||
| """ | ||
| Check if a folder with the given name exists in Google Drive, to | ||
| prevent creating duplicate folders. | ||
| """ | ||
| query = ( | ||
| f"name = '{folder_name}' and mimeType = " | ||
| "'application/vnd.google-apps.folder' and trashed = false" | ||
| ) | ||
| results = ( | ||
| service.files() | ||
| .list( | ||
| q=query, spaces="drive", fields="files(id, name)", pageSize=10 | ||
| ) | ||
| .execute() | ||
| ) | ||
|
|
||
| items = results.get("files", []) | ||
| return items | ||
|
|
||
| def list_files(self, page_size=10): | ||
| try: | ||
| results = ( | ||
| self.service.files() | ||
| .list( | ||
| pageSize=page_size, | ||
| fields="nextPageToken, files(id, name)", | ||
| ) | ||
| .execute() | ||
| ) | ||
| items = results.get("files", []) | ||
| return items | ||
| except HttpError as error: | ||
| print(f"An error occurred: {error}") | ||
| return None | ||
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
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.