diff options
author | prashantsinalkar | 2019-09-26 18:42:05 +0530 |
---|---|---|
committer | prashantsinalkar | 2019-09-26 18:42:05 +0530 |
commit | 3e27e89873a65071f1f9cfcf0bef3e3b9a68f09c (patch) | |
tree | 00649ccd686e21bb7d3e30e2aa639c8f3c5b8b6e | |
parent | da5988d90055b9cc7deea8dbba70cfba553cd77c (diff) | |
download | R_on_Cloud_Web_Interface-3e27e89873a65071f1f9cfcf0bef3e3b9a68f09c.tar.gz R_on_Cloud_Web_Interface-3e27e89873a65071f1f9cfcf0bef3e3b9a68f09c.tar.bz2 R_on_Cloud_Web_Interface-3e27e89873a65071f1f9cfcf0bef3e3b9a68f09c.zip |
updated settings and file upload code and auth token
-rw-r--r-- | R_on_Cloud/default_config.py | 6 | ||||
-rw-r--r-- | instances.py | 27 | ||||
-rw-r--r-- | website/static/website/js/cloud.js | 37 | ||||
-rw-r--r-- | website/static/website/templates/index.html | 3 | ||||
-rw-r--r-- | 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 = $("<img>"); - $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 = $("<img>"); + $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 @@ <input type="hidden" id="user_id" name="user_id" value="{{ user_id }}"> <a id="execute" class="btn btn-dark text-white"><span id="execute-inner">Execute</span></a> <!-- Trigger the modal with a button --> - <!-- <button id="uploaddataset" type="button" class="btn btn-dark text-white" data-toggle="modal" data-target="#uploaddatasetModal">Upload Dataset</button> --> + <button id="uploaddataset" type="button" class="btn btn-dark text-white" data-toggle="modal" data-target="#uploaddatasetModal">Upload Dataset</button> <button id="reset" type="button" class="btn btn-dark text-white" data-dismiss="modal">Reset</button> <!-- Modal --> @@ -230,6 +230,7 @@ <div class="modal-header"> </div> <div class="modal-body"> + <p>Upload your CSV dataset file</p> <form> <input type="file" id="fileSelect" accept="text/csv" /> <button id="fileuploadsubmit" type="button" >Upload</button> diff --git a/website/views.py b/website/views.py index 01e0cc6..4d1af16 100644 --- a/website/views.py +++ b/website/views.py @@ -89,13 +89,15 @@ def get_code(file_path, commit_sha): code = utils.get_file(file_path, commit_sha, main_repo=True) return code + def index(request): context = {} user_id = uuid.uuid4() context['api_url_upload'] = API_URL_UPLOAD + context['reset_req_url'] = API_URL_RESET book_id = request.GET.get('book_id') user = request.user - if(request.session['user_id']): + if 'user_id' in request.session: context['user_id'] = request.session['user_id'] else: context['user_id'] = str(user_id) @@ -153,6 +155,8 @@ def index(request): request.session['filepath'], commit_sha) context['code'] = session_code context['user_id'] = request.session['user_id'] + context['reset_req_url'] = API_URL_RESET + template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) elif book_id: @@ -182,7 +186,8 @@ def index(request): 'err_msg': """This book is not supported by Scilab on Cloud.""" """ You are redirected to home page.""" } - + context['api_url_upload'] = API_URL_UPLOAD + context['reset_req_url'] = API_URL_RESET template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) @@ -208,6 +213,8 @@ def index(request): 'book_id': int(book_id), } + context['api_url_upload'] = API_URL_UPLOAD + context['reset_req_url'] = API_URL_RESET template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) else: @@ -219,6 +226,8 @@ def index(request): 'err_msg': """This example is currently not available on """ """scilab on cloud.""" } + context['api_url_upload'] = API_URL_UPLOAD + context['reset_req_url'] = API_URL_RESET template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) @@ -290,6 +299,7 @@ def index(request): 'err_msg': """This example is currently not available on""" """ scilab on cloud.""" } + context['api_url_upload'] = API_URL_UPLOAD template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) subcateg_all = TextbookCompanionSubCategoryList.objects\ @@ -322,7 +332,8 @@ def index(request): # if not user.is_anonymous(): # context['user'] = user - + context['api_url_upload'] = API_URL_UPLOAD + context['reset_req_url'] = API_URL_RESET template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) |