From 3e27e89873a65071f1f9cfcf0bef3e3b9a68f09c Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Thu, 26 Sep 2019 18:42:05 +0530 Subject: updated settings and file upload code and auth token --- R_on_Cloud/default_config.py | 6 ++++- instances.py | 27 ++++++++++++--------- website/static/website/js/cloud.js | 37 ++++++++++++++++------------- website/static/website/templates/index.html | 3 ++- website/views.py | 17 ++++++++++--- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/R_on_Cloud/default_config.py b/R_on_Cloud/default_config.py index e328b78..59e091c 100644 --- a/R_on_Cloud/default_config.py +++ b/R_on_Cloud/default_config.py @@ -13,9 +13,13 @@ DB_PORT_R_FOSSEE_IN = '' BIN = '/usr/bin/R' API_URL = "http://127.0.0.1:8001/rscript" -API_URL_UPLOAD = "http://127.0.0.1:8001/upload" +API_URL_UPLOAD = "http://127.0.0.1:8001/upload-temp-file" API_URL_PLOT = "http://127.0.0.1:8001/file" API_URL_SERVER = "http://127.0.0.1:8001/" +AUTH_KEY = 'Secret key' # Same key as in web API +API_URL_RESET = "http://127.0.0.1:8001/reset" + + UPLOADS_PATH = "TBC upload directory path" MAIN_REPO = "TBC upload directory path" diff --git a/instances.py b/instances.py index 6f97563..db3167b 100644 --- a/instances.py +++ b/instances.py @@ -16,7 +16,7 @@ from django.template.loader import render_to_string, get_template from django.core.mail import EmailMultiAlternatives # importing the local variables from R_on_Cloud.settings import PROJECT_DIR -from R_on_Cloud.config import (BIN, API_URL, TEMP_DIR) +from R_on_Cloud.config import (BIN, API_URL, TEMP_DIR, AUTH_KEY) def execute_code(code, user_id, R_file_id): @@ -43,23 +43,28 @@ def execute_code(code, user_id, R_file_id): headers = { "Content-Type": "application/json", "Accept": "application/json", + "X-Api-Key": AUTH_KEY, } jsondata = json.dumps(body) if not os.path.exists(TEMP_DIR): os.makedirs(TEMP_DIR) result = requests.post(API_URL, json=jsondata, headers=headers) output = result.json() - output_data = json.loads(json.dumps(output['data'])) - output_error = json.loads(json.dumps(output['error'])) - plot_exist = json.loads(json.dumps(output['is_plot'])) - plot_path_req = json.loads(json.dumps(output['plot_path_req'])) + output_auth_error = json.loads(json.dumps(output['auth_error'])) + if output_auth_error != '400': + output_data = json.loads(json.dumps(output['data'])) + output_error = json.loads(json.dumps(output['error'])) + plot_exist = json.loads(json.dumps(output['is_plot'])) + plot_path_req = json.loads(json.dumps(output['plot_path_req'])) - data = { - 'output': output_data, - 'error': output_error, - 'plot_exist': plot_exist, - 'plot_path': plot_path_req, - } + data = { + 'output': output_data, + 'error': output_error, + 'plot_exist': plot_exist, + 'plot_path': plot_path_req, + } + else: + data = {'error': "Invalid request, please try after some time"} return data diff --git a/website/static/website/js/cloud.js b/website/static/website/js/cloud.js index ab4efa1..01153cd 100644 --- a/website/static/website/js/cloud.js +++ b/website/static/website/js/cloud.js @@ -674,27 +674,29 @@ $(document.body).ready(function() { $("#execute-inner").html( "Execute"); ajax_loader('clear'); - result.setValue(data.output); if (data.error.length != 0) { alert(data.error); } - if(data.plot_exist =='True'){ - $plot = $(""); - $plot.attr({ - src: data.plot_path, - width: '100%' - }); - $plotbox.html($plot); - $plotbox_wrapper.modal('show'); - var dt = new Date().getTime(); - $("#plot_download").show(); - $("#plot_download").attr( - "download", dt + - '.png'); - $("#plot_download").attr( - "href", data.plot_path - ); + else{ + result.setValue(data.output); + if(data.plot_exist =='True'){ + $plot = $(""); + $plot.attr({ + src: data.plot_path, + width: '100%' + }); + $plotbox.html($plot); + $plotbox_wrapper.modal('show'); + var dt = new Date().getTime(); + $("#plot_download").show(); + $("#plot_download").attr( + "download", dt + + '.png'); + $("#plot_download").attr( + "href", data.plot_path + ); + } } }); }else{ @@ -1048,6 +1050,7 @@ function doSubmit(){ var user_id = document.getElementById("user_id"); formData.set("user_id", user_id.value) // Http Request + var request = new XMLHttpRequest(); request.open('POST', api_url_upload); request.send(formData); diff --git a/website/static/website/templates/index.html b/website/static/website/templates/index.html index a095bc3..fb0681f 100644 --- a/website/static/website/templates/index.html +++ b/website/static/website/templates/index.html @@ -219,7 +219,7 @@ Execute - + @@ -230,6 +230,7 @@