Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Merge 3290d23 into f6c401c
Browse files Browse the repository at this point in the history
  • Loading branch information
aguilerapy committed Apr 21, 2020
2 parents f6c401c + 3290d23 commit eb795de
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 61 deletions.
9 changes: 9 additions & 0 deletions credentials.json
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"
}
}
63 changes: 63 additions & 0 deletions default/drive_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import print_function
from django.http import HttpResponse, JsonResponse
from google.auth.exceptions import DefaultCredentialsError
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import UnknownFileType
from googleapiclient.http import MediaFileUpload
from oauthlib.oauth2 import AccessDeniedError
from ocdstoucan.settings import OCDS_TOUCAN_CREDENTIALS_DRIVE


SCOPES = 'https://www.googleapis.com/auth/drive.file'


def upload_to_drive(filename, filepath, format=None, test=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)
if not test:
credentials = flow.run_local_server(port=0)
if credentials and not credentials.valid:
raise AccessDeniedError
service = build('drive', 'v3', credentials=credentials)

except AccessDeniedError:
return HttpResponse("Access Denied", status=400)

except DefaultCredentialsError:
service = None

try:
if format == 'xlsx':
mimeType = 'application/vnd.google-apps.spreadsheet'
else:
if format == 'csv' or format is None:
mimeType = 'application/zip'
else:
mimeType = '*/*'

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, UnknownFileType):
if test:
return HttpResponse("Test", status=200)
else:
return HttpResponse("Fail Uploading", status=400)
11 changes: 11 additions & 0 deletions default/static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@
.content-wrap.ocp-content {
padding-bottom: 2.5rem;
}
.btn.btn-primary.btn-download {
line-height: 0;
color: #9baf00;
background-color: transparent;
border-color: transparent;
padding: 0px 0px;
}
.btn.btn-primary.btn-download:hover {
text-decoration-line: underline;
color: #6D812B;
}
37 changes: 35 additions & 2 deletions default/static/js/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ var app = {};
});
$.ajax($('#fileupload').attr('data-perform-action'), {data: actionParams})
.done(function (data) {
$('.response-success .file-size').html(utils.readableFileSize(data.size));
$('.response-success .download').attr('href', data.url);
$('.response-success .f-file-size').html(utils.readableFileSize(data.size));
$('.response-success .f-file').attr('href', data.url);
$('.response-success .d-drive').attr('href', data.url + '?out=drive');
$('.response-success').removeClass('hidden');
if (data.hasOwnProperty('warnings') && data.warnings.length > 0) {
$('.response-warning.action-failed').removeClass('hidden');
Expand Down Expand Up @@ -171,6 +172,32 @@ var app = {};
;
}

function download() {
window.location = $(this).attr('href');
}

function saveDrive() {
showProcessingModal();
$.ajax($('.response-success .d-drive').attr('href'), { 'dataType': 'json' })
.done(function(data) {
$('.google-drive-success .file-google-name').html(data.name);
$('.google-drive-success .file-google-id').html(data.id);
$('.google-drive-success').removeClass('hidden');
$('.response-access-drive').addClass('hidden');
$('.response-fail-drive').addClass('hidden');
})
.fail(function(jqXHR, textStatus, errorThrown){
if (jqXHR.responseText == 'Access Denied'){
$('.response-access-drive').removeClass('hidden');
} else {
$('.response-fail-drive').removeClass('hidden');
}
})
.always(function() {
hideProcessingModal()
});
}

/** plugin initialization & listeners**/

var fileupload = $('#fileupload')
Expand All @@ -183,6 +210,12 @@ var app = {};
/** upload call binding **/
$("#upload-button").click(upload);

/* click download button behaviour */
$('.f-file').click(download);

/* click save to Drive button behaviour */
$('.d-drive').click(saveDrive);

/* add warning before closing/navigating away from page */
window.onload = function () {
window.addEventListener("beforeunload", function (e) {
Expand Down
25 changes: 23 additions & 2 deletions default/templates/default/base-uploader.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,23 @@

{% block body %}
<div class="response-success alert alert-info hidden">
<b>{% trans "Success!" %}</b> result.zip(<span class="file-size"></span>)
<a href="javascript:void(0)" class="download" target="_blank">{% trans "Download" %}</a>
<b>{% trans "Success!" %}</b>
result.zip(<span class="f-file-size"></span>)
<button type="button" class="btn btn-primary btn-download f-file">
<span>{% trans "Download" %}</span>
</button>
<span> / </span>
<button type="button" class="btn btn-primary btn-download d-drive">
<span>{% trans "Save to Google Drive" %}</span>
</button>
</div>
<div class="google-drive-success alert alert-info hidden">
<b>{% trans "Uploaded to Google Drive!" %}</b>
<ul>
<li>
<span class="file-google-name"></span> (Id: <span class="file-google-id"></span>)
</li>
</ul>
</div>
<div class="response-warning action-failed alert alert-warning hidden">
<ul></ul>
Expand All @@ -23,6 +38,12 @@
{% trans "You can add more files or remove them, then click on <b>Start</b> again." %}
{% trans "Files with validation issues will be ignored by default." %}
</div>
<div class="response-access-drive alert alert-danger hidden">
<b>{% trans "Error saving to Google Drive: Access Denied" %}</b>
</div>
<div class="response-fail-drive alert alert-danger hidden">
<b>{% trans "Error saving to Google Drive: Fail Uploading" %}</b>
</div>
<div class="response-fail alert alert-danger hidden">
<b>{% trans "An error has occurred!" %}</b>
{% trans "Please verify that all your files are valid OCDS JSON, and try again in a few minutes." %}
Expand Down
57 changes: 51 additions & 6 deletions default/templates/default/to-json.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@
<b>{% trans "Success!" %}</b>
<ul>
<li>
result.zip (<span class="file-size-json"></span>)
<a href="javascript:void(0)" class="download-json">{% trans "Download" %}</a>
result.zip (<span class="f-file-size"></span>)
<button type="button" class="btn btn-primary btn-download f-file">
<span>{% trans "Download" %}</span>
</button>
<span> / </span>
<button type="button" class="btn btn-primary btn-download d-drive">
<span>{% trans "Save to Google Drive" %}</span>
</button>
</li>
</ul>
</div>
<div class="response-fail alert alert-danger hidden">
<b>{% trans "An error has occurred!" %}</b>
{% trans "Please verify that all your files are valid according to OCDS, and try again in a few minutes." %}
</div>
<div class="google-drive-success alert alert-info hidden">
<b>{% trans "Uploaded to Google Drive!" %}</b>
<ul>
<li>
<span class="file-google-name"></span> (Id: <span class="file-google-id"></span>)
</li>
</ul>
</div>
<div class="info panel panel-default">
<div class="panel-body">
<p>
Expand Down Expand Up @@ -108,19 +122,50 @@
$('#processing-modal').modal('show');
$.ajax('/to-json/go/', { 'dataType': 'json' })
.done(function(data){
$('.response-success .download-json').attr('href', data.url);
$('.response-success .file-size-json').html(utils.readableFileSize(data.size));
$('.response-success .f-file').attr('href', data.url);
$('.response-success .f-file-size').html(utils.readableFileSize(data.size));
$('.response-success .d-drive').attr('href', data.url + '?out=drive');
$('.response-success').removeClass('hidden');
})
.fail(function(){
$('.response-fail').html(
'{% trans "An error has occurred! Please verify that all your files are valid OCDS JSON, and try again in a few minutes." %}'
);
$('.response-fail').removeClass('hidden');
})
.always(function(){
$('#processing-modal').modal('hide');
});
})
});

$('.f-file').click(function () {
window.location = $(this).attr('href');
});

/* Click drive json behaviour */
$('.d-drive').click(function(){
data
.submit()
.done(function(){
$('#processing-modal').modal('show');
$.ajax($('.response-success .d-drive').attr('href'), { 'dataType': 'json' })
.done(function(data){
$('.google-drive-success .file-google-name').html(data.name);
$('.google-drive-success .file-google-id').html(data.id);
$('.google-drive-success').removeClass('hidden');
$('.response-fail').addClass('hidden');
})
.fail(function(jqXHR, textStatus, errorThrown){
$('.response-fail').html(
'{% trans "<b>Error saving to Google Drive</b>: " %}'
+ ( jqXHR.responseText || textStatus ));
$('.response-fail').removeClass('hidden');
})
.always(function(){
$('#processing-modal').modal('hide');
});
})
.always(function(){
});
});
});

Expand Down
68 changes: 60 additions & 8 deletions default/templates/default/to-spreadsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,37 @@
<b>{% trans "Success!" %}</b>
<ul>
<li>
result.xlsx (<span class="file-size-xlsx"></span>)
<a href="javascript:void(0)" class="download-xlsx">{% trans "Download" %}</a>
result.xlsx (<span class="f-size-xlsx"></span>)
<button type="button" class="btn btn-primary btn-download f-file f-xlsx">
<span>{% trans "Download" %}</span>
</button>
<span> / </span>
<button type="button" class="btn btn-primary btn-download d-drive d-xlsx">
<span>{% trans "Save to Google Drive" %}</span>
</button>
</li>
<li>
result-csv.zip (<span class="file-size-csv"></span>)
<a href="javascript:void(0)" class="download-csv">{% trans "Download" %}</a>
result-csv.zip (<span class="f-size-csv"></span>)
<button type="button" class="btn btn-primary btn-download f-file f-csv">
<span>{% trans "Download" %}</span>
</button>
<span> / </span>
<button type="button" class="btn btn-primary btn-download d-drive d-csv">
<span>{% trans "Save to Google Drive" %}</span>
</button>
</li>
</ul>
</div>
<div class="response-fail alert alert-danger hidden">
</div>
<div class="google-drive-success alert alert-info hidden">
<b>{% trans "Uploaded to Google Drive!" %}</b>
<ul>
<li>
<span class="file-google-name"></span> (Id: <span class="file-google-id"></span>)
</li>
</ul>
</div>
<div class="info panel panel-default">
<div class="panel-body">
<p>
Expand Down Expand Up @@ -104,10 +124,12 @@
$('#processing-modal').modal('show');
$.ajax('/to-spreadsheet/go/', { 'dataType': 'json' })
.done(function(data){
$('.response-success .download-xlsx').attr('href', data.xlsx.url);
$('.response-success .download-csv').attr('href', data.csv.url);
$('.response-success .file-size-xlsx').html(utils.readableFileSize(data.xlsx.size));
$('.response-success .file-size-csv').html(utils.readableFileSize(data.csv.size));
$('.response-success .f-xlsx').attr('href', data.xlsx.url);
$('.response-success .f-csv').attr('href', data.csv.url);
$('.response-success .f-size-xlsx').html(utils.readableFileSize(data.xlsx.size));
$('.response-success .f-size-csv').html(utils.readableFileSize(data.csv.size));
$('.response-success .d-xlsx').attr('href', data.xlsx.url + '?out=drive');
$('.response-success .d-csv').attr('href', data.csv.url + '?out=drive');
$('.response-success').removeClass('hidden');
})
.fail(function(){
Expand All @@ -130,6 +152,36 @@
$('.response-fail').removeClass('hidden');
});
});

$('.f-file').click(function () {
window.location = $(this).attr('href');
});

/* Click drive xlsx behaviour */
$('.d-drive').click(function(){
url = $(this).attr('href')
data
.submit()
.done(function(){
$('#processing-modal').modal('show');
$.ajax(url, { 'dataType': 'json' })
.done(function(data){
$('.google-drive-success .file-google-name').html(data.name);
$('.google-drive-success .file-google-id').html(data.id);
$('.google-drive-success').removeClass('hidden');
$('.response-fail').addClass('hidden');
})
.fail(function(jqXHR, textStatus, errorThrown){
$('.response-fail').html(
'{% trans "<b>Error saving to Google Drive</b>: " %}'
+ ( jqXHR.responseText || textStatus ));
$('.response-fail').removeClass('hidden');
})
.always(function(){
$('#processing-modal').modal('hide');
});
})
});
});

/* prevent browser's default action when dragging and dropping files */
Expand Down
9 changes: 8 additions & 1 deletion default/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from default.data_file import DataFile
from default.decorators import clear_files, published_date, require_files
from default.drive_options import upload_to_drive
from default.forms import MappingSheetOptionsForm
from default.mapping_sheet import (get_extended_mapping_sheet, get_mapping_sheet_from_uploaded_file,
get_mapping_sheet_from_url)
Expand All @@ -24,6 +25,7 @@


def retrieve_result(request, folder, id, format=None):
output = request.GET.get('out')
if format is None:
prefix = 'result'
ext = '.zip'
Expand All @@ -40,7 +42,12 @@ def retrieve_result(request, folder, id, format=None):
raise Http404('Invalid option')

file = DataFile(prefix, ext, id=str(id), folder=folder)
return FileResponse(open(file.path, 'rb'), filename=filename, as_attachment=True)

if output == 'drive':
is_test = request.GET.get('test')
return upload_to_drive(filename, file.path, format, test=is_test)
else:
return FileResponse(open(file.path, 'rb'), filename=filename, as_attachment=True)


def index(request):
Expand Down
Loading

0 comments on commit eb795de

Please sign in to comment.