diff options
author | prashantsinalkar | 2019-09-24 16:33:59 +0530 |
---|---|---|
committer | prashantsinalkar | 2019-09-24 16:33:59 +0530 |
commit | 57a530976f576cf9b3493b9994e46242ae39b4b9 (patch) | |
tree | 3ae39634d83cd1d80773735970fd0bec08ad85e5 | |
parent | af9bfbb8e3b1f08e0df80402cf69d40ff5baf7b4 (diff) | |
download | R_on_Cloud_Web_Interface-57a530976f576cf9b3493b9994e46242ae39b4b9.tar.gz R_on_Cloud_Web_Interface-57a530976f576cf9b3493b9994e46242ae39b4b9.tar.bz2 R_on_Cloud_Web_Interface-57a530976f576cf9b3493b9994e46242ae39b4b9.zip |
updated code for flask api
-rw-r--r-- | instances.py | 37 | ||||
-rw-r--r-- | tornado_main.py | 14 | ||||
-rw-r--r-- | website/query.py | 7 | ||||
-rw-r--r-- | website/static/website/js/cloud.js | 10 | ||||
-rw-r--r-- | website/static/website/templates/index.html | 6 | ||||
-rw-r--r-- | website/views.py | 107 |
6 files changed, 42 insertions, 139 deletions
diff --git a/instances.py b/instances.py index 5996541..bbdfd1f 100644 --- a/instances.py +++ b/instances.py @@ -6,10 +6,9 @@ import time import sys import psutil import requests -import json import urllib.request import base64 - +import simplejson as json from datetime import datetime from django.template.loader import render_to_string, get_template @@ -19,8 +18,7 @@ from R_on_Cloud.settings import PROJECT_DIR from R_on_Cloud.config import (BIN, API_URL, API_URL_PLOT) -def execute_code(code, session_id, R_file_id): - #session_id = self.request.session['session_id'] +def execute_code(code, user_id, R_file_id): # Check for system commands system_commands = re.compile( @@ -38,23 +36,28 @@ def execute_code(code, session_id, R_file_id): body = { 'code': code, - 'session_id': session_id, + 'user_id': user_id, 'R_file_id': R_file_id, } - req = urllib.request.Request(API_URL) - req.add_header('Content-Type', 'application/json; charset=utf-8') + headers = { + "Content-Type": "application/json", + "Accept": "application/json", + } jsondata = json.dumps(body) - jsondataasbytes = jsondata.encode('utf-8') - req.add_header('Content-Length', len(jsondataasbytes)) - result = urllib.request.urlopen(req, jsondataasbytes) - result = result.read().decode("utf8") - output = json.loads(result)["output"] - graph_exist = json.loads(result)["graph_exist"] - graph_path = API_URL_PLOT + "?session_id=" + session_id +"&R_file_id="+ R_file_id + #jsondata = urllib.parse.urlencode(body) + #print(jsondata) + + result=requests.post(API_URL, json=jsondata, headers=headers) + output = result.json() + output_data= json.dumps(output['data']) + output_error = json.dumps(output['error']) + graph_exist = "" + graph_path = "" data = { - 'output': output, - 'graph_exist': graph_exist, - 'graph_path': graph_path, + 'output': json.loads(output_data), + 'error': json.loads(output_error) + #'graph_exist': graph_exist, + #'graph_path': graph_path, } return data diff --git a/tornado_main.py b/tornado_main.py index 7683e85..f200ad9 100644 --- a/tornado_main.py +++ b/tornado_main.py @@ -1,9 +1,5 @@ #!/usr/bin/env python -# Run this with -# PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings -# testsite/tornado_main.py - from tornado.options import options, define, parse_command_line import django.core.handlers.wsgi import tornado.httpserver @@ -11,11 +7,7 @@ import tornado.ioloop import tornado.web import tornado.wsgi import os, sys -import json as simplejson import django - -#Gist https://gist.githubusercontent.com/wonderbeyond/d38cd85243befe863cdde54b84505784/raw/ab78419248055333a6bf4a50022311cae9d6596c/graceful_shutdown_tornado_web_server.py - import time import signal import logging @@ -122,10 +114,10 @@ class ExecutionHandler(tornado.web.RequestHandler): def post(self): global request_count request_count += 1 - session_id = self.request.arguments['session_id'][0].decode('UTF-8') + user_id = self.request.arguments['user_id'][0] R_file_id = str(time.time()) - code = self.request.arguments['code'][0].decode('UTF-8') - data = yield executor.submit(execute_code, code, session_id, R_file_id) + code = self.request.arguments['code'][0] + data = yield executor.submit(execute_code, code.decode("utf-8"), user_id, R_file_id) self.write(data) request_count -= 1 diff --git a/website/query.py b/website/query.py index 06e37a7..23f4cea 100644 --- a/website/query.py +++ b/website/query.py @@ -48,13 +48,6 @@ GET_TBC_EXAMPLE_FILE_SQL = """ WHERE tcef.filetype = 'S' AND tcef.example_id = %s """ - -# GET_TBC_EXAMPLE_FILE_VIEW_SQL = """ -# SELECT id, views_count FROM textbook_companion_example_views -# WHERE example_id = %s -# """ - - GET_TBC_CONTRIBUTOR_DETAILS_SQL = """ SELECT preference.id, proposal.full_name as proposal_full_name, diff --git a/website/static/website/js/cloud.js b/website/static/website/js/cloud.js index 6c874fa..ab71be1 100644 --- a/website/static/website/js/cloud.js +++ b/website/static/website/js/cloud.js @@ -661,7 +661,7 @@ $(document.body).ready(function() { token: $( "[name='csrfmiddlewaretoken']" ).val(), - session_id: $("#session_id").val() || 0, + user_id: $("#user_id").val() || 0, R_file_id: $("#R_file_id").val() || 0, code: editor.getValue(), book_id: $("#books").val() || 0, @@ -675,6 +675,10 @@ $(document.body).ready(function() { "Execute"); ajax_loader('clear'); result.setValue(data.output); + if (data.error.length != 0) + { + alert(data.error); + } if(data.graph_exist){ $plot = $("<img>"); $plot.attr({ @@ -1044,8 +1048,8 @@ function doSubmit(){ if(fileSelect.files && fileSelect.files.length == 1){ var file = fileSelect.files[0] formData.set("file", file , file.name); - var session_id = document.getElementById("session_id"); - formData.set("session_id", session_id.value) + 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); diff --git a/website/static/website/templates/index.html b/website/static/website/templates/index.html index 3020c83..a095bc3 100644 --- a/website/static/website/templates/index.html +++ b/website/static/website/templates/index.html @@ -216,10 +216,10 @@ </div> <textarea id="code" class="form-control" rows="5" placeholder="Write a new code or select existing from above category..." wrap="hard">{{ code }}</textarea> <br> - <input type="hidden" id="session_id" name="session_id" value="{{ session_id }}"> + <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 --> @@ -267,7 +267,7 @@ </div> <textarea id="result" class="form-control" rows="5"></textarea> <br> - <a id="bug" href="" class="btn btn-dark text-white float-right" data-toggle="modal" data-target="#bug_form_wrapper">Report bug / Give Feedback</a> + <!-- <a id="bug" href="" class="btn btn-dark text-white float-right" data-toggle="modal" data-target="#bug_form_wrapper">Report bug / Give Feedback</a> --> </div> </div> </div> diff --git a/website/views.py b/website/views.py index 6948f13..d5eca56 100644 --- a/website/views.py +++ b/website/views.py @@ -3,10 +3,7 @@ from django.http import HttpResponse from django.template import loader import requests import uuid -<<<<<<< HEAD -======= from R_on_Cloud.config import (API_URL_UPLOAD) ->>>>>>> fixed conflict from website.models import * from django.db.models import Q import json as simplejson @@ -14,10 +11,7 @@ from . import utils from django.db import connections from .query import * -<<<<<<< HEAD -======= ->>>>>>> fixed conflict def dictfetchall(cursor): "Return all rows from a cursor as a dict" columns = [col[0] for col in cursor.description] @@ -26,10 +20,7 @@ def dictfetchall(cursor): for row in cursor.fetchall() ] -<<<<<<< HEAD -======= ->>>>>>> fixed conflict def catg(cat_id, all_cat): if all_cat is False: category = TextbookCompanionCategoryList.objects.using('r')\ @@ -58,15 +49,10 @@ def get_subcategories(maincat_id): return subcategories -<<<<<<< HEAD -======= - ->>>>>>> fixed conflict def get_books(category_id): with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_PREFERENCE_FROM_CATEGORY_ID_SQL, -<<<<<<< HEAD params=[category_id]) books = dictfetchall(cursor) return books @@ -77,30 +63,15 @@ def get_chapters(book_id): cursor.execute(GET_TBC_CHAPTER_SQL, params=[book_id]) chapters = dictfetchall(cursor) -======= - params=[category_id]) - books = dictfetchall(cursor) - return books -def get_chapters(book_id): - with connections['r'].cursor() as cursor: - cursor.execute(GET_TBC_CHAPTER_SQL, - params=[book_id]) - chapters = dictfetchall(cursor) ->>>>>>> fixed conflict return chapters def get_examples(chapter_id): with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_SQL, -<<<<<<< HEAD params=[chapter_id]) examples = dictfetchall(cursor) -======= - params=[chapter_id]) - examples = dictfetchall(cursor) ->>>>>>> fixed conflict return examples @@ -114,37 +85,20 @@ def get_revisions(example_id): def get_code(file_path, commit_sha): -<<<<<<< HEAD + code = utils.get_file(file_path, commit_sha, main_repo=True) -======= - code= utils.get_file(file_path, commit_sha, main_repo=True) ->>>>>>> fixed conflict return code def index(request): context = {} -<<<<<<< HEAD -======= - session_id = uuid.uuid4() - context['session_id'] = str(session_id) + user_id = uuid.uuid4() + context['user_id'] = str(user_id) context['api_url_upload'] = API_URL_UPLOAD - request.session['session_id'] = str(session_id) ->>>>>>> fixed conflict + request.session['user_id'] = str(user_id) book_id = request.GET.get('book_id') user = request.user - # if not user.is_anonymous(): - # social = user.social_auth.get(provider='google-oauth2') - # url = 'https://www.googleapis.com/plus/v1/people/me' - # params = {'access_token': social.extra_data['access_token']} - # # r = requests.get(url, params=params) - # # print(r.content) - - # context = { - # 'user': user - # } - if not (request.GET.get('eid') or request.GET.get('book_id')): catg_all = catg(None, all_cat=True) subcatg_all = subcatg(None, all_subcat=True) @@ -178,16 +132,10 @@ def index(request): context['revisions'] = get_revisions(example_id) with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_R_CLOUD_COMMENT_SQL, -<<<<<<< HEAD params=[example_id]) review = cursor.fetchone() review_url = "https://r.fossee.in/cloud_comments/" + \ str(example_id) -======= - params=[example_id]) - review = cursor.fetchone() - review_url = "https://r.fossee.in/cloud_comments/" + str(example_id) ->>>>>>> fixed conflict context['review'] = review[0] context['review_url'] = review_url @@ -202,6 +150,7 @@ def index(request): session_code = get_code( request.session['filepath'], commit_sha) context['code'] = session_code + context['user_id'] = request.session['user_id'] template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) elif book_id: @@ -275,26 +224,19 @@ def index(request): try: with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_R_CLOUD_COMMENT_SQL, -<<<<<<< HEAD params=[eid]) -======= - params=[eid]) ->>>>>>> fixed conflict + review = cursor.fetchone() review_url = "https://r.fossee.in/cloud_comments/" + str(eid) with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_CHAPTER_ID_SQL, -<<<<<<< HEAD params=[eid]) -======= - params=[eid]) ->>>>>>> fixed conflict + chapter_id = cursor.fetchone() with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_CHAPTER_DETAIL_SQL, -<<<<<<< HEAD params=[chapter_id[0]]) chapters = dictfetchall(cursor) @@ -305,42 +247,24 @@ def index(request): with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_PREFERENCE_DETAIL_CATEGORY_SQL, params=[preference_id[0]]) -======= - params=[chapter_id[0]]) - chapters = dictfetchall(cursor) - - with connections['r'].cursor() as cursor: - cursor.execute(GET_TBC_CHAPTER_PREFERENCE_ID_SQL, - params=[chapter_id[0]]) - preference_id = cursor.fetchone() - with connections['r'].cursor() as cursor: - cursor.execute(GET_TBC_PREFERENCE_DETAIL_CATEGORY_SQL, - params=[preference_id[0]]) ->>>>>>> fixed conflict books_detail = cursor.fetchone() books = get_books(books_detail[1]) maincat_id = books_detail[0] subcat_id = books_detail[1] -<<<<<<< HEAD with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_FILE_SQL, params=[eid]) -======= with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_FILE_SQL, - params=[eid]) ->>>>>>> fixed conflict + params=[eid]) + example_file = cursor.fetchone() example_file_filepath = example_file[4] + '/' + example_file[5] with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL, -<<<<<<< HEAD params=[eid]) -======= - params=[eid]) ->>>>>>> fixed conflict ex_views_count = cursor.fetchone() request.session['maincat_id'] = maincat_id @@ -399,10 +323,7 @@ def index(request): template = loader.get_template('index.html') return HttpResponse(template.render(context, request)) -<<<<<<< HEAD -======= ->>>>>>> fixed conflict def update_view_count(request): ex_id = request.GET.get('ex_id') @@ -411,7 +332,6 @@ def update_view_count(request): Example_chapter_id = cursor.fetchone() with connections['r'].cursor() as cursor: cursor.execute(INSERT_TBC_EXAMPLE_VIEW_SQL, -<<<<<<< HEAD params=[ex_id, Example_chapter_id[0]]) with connections['r'].cursor() as cursor: cursor.execute(UPDATE_TBC_EXAMPLE_VIEW_SQL, @@ -419,15 +339,6 @@ def update_view_count(request): with connections['r'].cursor() as cursor: cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL, params=[ex_id]) -======= - params=[ex_id,Example_chapter_id[0]]) - with connections['r'].cursor() as cursor: - cursor.execute(UPDATE_TBC_EXAMPLE_VIEW_SQL, - params=[ex_id]) - with connections['r'].cursor() as cursor: - cursor.execute(GET_TBC_EXAMPLE_VIEW_SQL, - params=[ex_id]) ->>>>>>> fixed conflict Example_views_count = cursor.fetchone() data = Example_views_count[0] return HttpResponse(simplejson.dumps(data), |