This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
409 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
{ | ||
"installed": { | ||
"client_id": "example.com", | ||
"project_id": "example", | ||
"auth_uri": "https://example.com", | ||
"token_uri": "https://example.com/token", | ||
"client_secret": "example" | ||
} | ||
} |
This file contains 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,50 @@ | ||
import logging | ||
|
||
from django.http import HttpResponse, JsonResponse | ||
from google.auth.transport.requests import Request | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
from googleapiclient.discovery import build | ||
from googleapiclient.http import MediaFileUpload | ||
from oauthlib.oauth2 import AccessDeniedError | ||
from ocdstoucan.settings import OCDS_TOUCAN_CREDENTIALS_DRIVE | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SCOPES = 'https://www.googleapis.com/auth/drive.file' | ||
|
||
|
||
def upload_to_drive(filename, filepath, format=None, credentials=None): | ||
try: | ||
if not credentials or not credentials.valid: | ||
if credentials and credentials.expired and credentials.refresh_token: | ||
credentials.refresh(Request()) | ||
else: | ||
flow = InstalledAppFlow.from_client_secrets_file(OCDS_TOUCAN_CREDENTIALS_DRIVE, SCOPES) | ||
credentials = flow.run_local_server(port=0) | ||
service = build('drive', 'v3', credentials=credentials) | ||
|
||
except AccessDeniedError: | ||
return HttpResponse("AccessDeniedError", status=400) | ||
|
||
try: | ||
if format == 'xlsx': | ||
mimeType = 'application/vnd.google-apps.spreadsheet' | ||
else: | ||
mimeType = 'application/zip' | ||
|
||
file_metadata = { | ||
'name': filename, | ||
'mimeType': mimeType | ||
} | ||
media = MediaFileUpload(filepath, | ||
mimetype=mimeType, | ||
resumable=True) | ||
results = service.files().create(body=file_metadata, media_body=media, fields='id').execute() | ||
|
||
return JsonResponse({ | ||
'name': filename, | ||
'id': results["id"] | ||
}) | ||
|
||
except (TypeError, Exception, IOError): | ||
return HttpResponse("UploadError", status=400) |
This file contains 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 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 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 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 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 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.