From 56f3d70eda27403a4d794135f68f192ac463fc2f Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 08:40:06 -0400 Subject: [PATCH 01/23] Add api key to authenticate with server --- swiftplugin/swiftplugin/swiftplugin.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index 60c68e0..fdd8501 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -56,6 +56,12 @@ class SwiftPluginXBlock( help="Problem title", ) + api_key = String( + default="password", + scope=Scope.preferences, + help="Key to send to API", + ) + problem_description = String( default="Problem description here!", scope=Scope.settings, From c96c148e656c19ce6766aa365431dfa38c2df5bf Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 08:40:18 -0400 Subject: [PATCH 02/23] set default language to swift --- swiftplugin/swiftplugin/swiftplugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index fdd8501..91176a2 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -77,7 +77,7 @@ class SwiftPluginXBlock( # ) problem_language = String( - default="text/x-kotlin", + default="text/x-swift", scope=Scope.settings, help="Example: text/x-kotlin. Supported languages can be found at https://codemirror.net/5/mode/" ) From 4a7cf97003e33d75e6e8a80f3bbb8586122a668c Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 08:40:58 -0400 Subject: [PATCH 03/23] Add api key to headers and use json --- swiftplugin/swiftplugin/swiftplugin.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index 91176a2..b852997 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -183,6 +183,12 @@ def get_problem_title(self, data, suffix=''): 'problem_title': self.problem_title } + @XBlock.json_handler + def get_api_key(self, data, suffix=''): + return { + 'api_key': self.api_key, + } + # @XBlock.json_handler # def get_problem_solution(self, data, suffix=''): # return { @@ -214,13 +220,23 @@ def show_buttons(self, data, suffix=''): } def handle_run_request(self): - r = requests.post(get_server_url(self.api_url_run), json=self.build_request_body()) + r = requests.post(get_server_url(self.api_url_run), json=self.build_request_body(), + headers=self.build_headers()) return r.json() def handle_submit_request(self): - r = requests.post(get_server_url(self.api_url_submit), json=self.build_request_body()) + r = requests.post(get_server_url(self.api_url_submit), json=self.build_request_body(), + headers=self.build_headers()) return r.json() + def build_headers(self): + headers = { + 'Authorization': 'Bearer {}'.format(self.api_key), + 'Content-Type': "application/json", + 'Accept': 'application/json' + } + return headers + def build_request_body(self): body = { "code": self.code, From 31e7053d70b8fe11df0115038965996354da1d91 Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 08:41:14 -0400 Subject: [PATCH 04/23] remove json.dumps and use single quotes for request body --- swiftplugin/swiftplugin/swiftplugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index b852997..92a261c 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -239,10 +239,10 @@ def build_headers(self): def build_request_body(self): body = { - "code": self.code, - "language": self.problem_language + 'code': self.code, + 'language': self.problem_language } - return json.dumps(body) + return body @staticmethod def workbench_scenarios(): From 5566713744ab942e6ee8f90af597ec55ccae69d1 Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 08:41:28 -0400 Subject: [PATCH 05/23] Fix login for showing run and submit buttons --- swiftplugin/swiftplugin/swiftplugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index 92a261c..b9a00ca 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -212,8 +212,8 @@ def get_problem_language(self, data, suffix=''): @XBlock.json_handler def show_buttons(self, data, suffix=''): - show_run_button = bool(self.api_url_run and self.api_url_run.isspace()) - show_submit_button = bool(self.api_url_submit and self.api_url_submit.isspace()) + show_run_button = bool(self.api_url_run and not self.api_url_run.isspace()) + show_submit_button = bool(self.api_url_submit and not self.api_url_submit.isspace()) return { 'show_run_button': show_run_button, 'show_submit_button': show_submit_button, From 38e8a0c8e341dcc5b2e9483506e57a68e2b33bb4 Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 21:37:50 -0400 Subject: [PATCH 06/23] Add support for grading --- swiftplugin/swiftplugin/swiftplugin.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index b9a00ca..199aa9a 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -69,6 +69,9 @@ class SwiftPluginXBlock( multiline_editor=True ) + has_score = True + attempt = 1 + # problem_solution = String( # default="", # scope=Scope.settings, @@ -159,10 +162,22 @@ def get_button_handler(self, data, suffix=''): elif 'submit' in data['type']: api_respo = self.handle_submit_request() - response['status'] = "Submitted code" - response['response'] = api_respo['message'] - response['diff'] = self.calculate_diff(expected_output=api_respo['expected_output'], - actual_output=api_respo['user_output']) + response['response'] = api_respo + if api_respo['error']: + return response + response['diff'] = self.calculate_diff(expected_output=api_respo['expectedOutput'], + actual_output=api_respo['output']) + is_final_attempt = api_respo['finalAttempt'] == 'true' + success = api_respo['success'] == 'true' + if success: + self.runtime.publish(self, "grade", + {'value': 1.0, + 'max_value': 1.0}) + elif is_final_attempt: + self.runtime.publish(self, "grade", + {'value': 0.0, + 'max_value': 1.0}) + else: response["status"] = "No valid type request" From 799fee79c210eaf4425f688555410f427e44dc24 Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 21:38:37 -0400 Subject: [PATCH 07/23] update body to send required params for server --- swiftplugin/swiftplugin/swiftplugin.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index 199aa9a..84d85f7 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -19,6 +19,7 @@ def get_server_url(url: str): return "https://" + url +@XBlock.wants('user') class SwiftPluginXBlock( StudioEditableXBlockMixin, XBlock): @@ -253,9 +254,15 @@ def build_headers(self): return headers def build_request_body(self): + user_service = self.runtime.service(self, 'user') + xb_user = user_service.get_current_user() + email = xb_user.emails[0] body = { 'code': self.code, - 'language': self.problem_language + 'language': self.problem_language, + 'attempt': self.attempt, + 'referenceId': self.problem_id, + 'email': email, } return body From cc91c1eff1e7cb4f3f048c1f827c3365388c96ef Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 21:38:54 -0400 Subject: [PATCH 08/23] Remove uncessary code --- swiftplugin/swiftplugin/swiftplugin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/swiftplugin/swiftplugin/swiftplugin.py b/swiftplugin/swiftplugin/swiftplugin.py index 84d85f7..e5c0b32 100644 --- a/swiftplugin/swiftplugin/swiftplugin.py +++ b/swiftplugin/swiftplugin/swiftplugin.py @@ -158,7 +158,6 @@ def get_button_handler(self, data, suffix=''): if 'run' in data['type']: api_respo = self.handle_run_request() - response['status'] = "Executed code" response['response'] = api_respo elif 'submit' in data['type']: From 42883298586c38c211b56fa4616fb149e2391a21 Mon Sep 17 00:00:00 2001 From: adevezin Date: Sun, 18 Sep 2022 21:39:22 -0400 Subject: [PATCH 09/23] Change output title color based on error or not --- .../swiftplugin/static/js/src/swiftplugin.js | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/swiftplugin/swiftplugin/static/js/src/swiftplugin.js b/swiftplugin/swiftplugin/static/js/src/swiftplugin.js index f35ee49..0a36495 100644 --- a/swiftplugin/swiftplugin/static/js/src/swiftplugin.js +++ b/swiftplugin/swiftplugin/static/js/src/swiftplugin.js @@ -146,29 +146,38 @@ function SwiftPluginXBlock(runtime, element) { }*/ - function updateResponse(response) { + function setOutput(response) { + const compilation_response = response.response.output + let output_response; + if (response.diff) { + const diff_response = response.diff + output_response = compilation_response + '
' + diff_response + } else { + output_response = compilation_response + } + document.getElementById('response-txt').innerHTML = output_response; + document.getElementById('response-title').style.color = "#33691E"; + } + + function updateResponse(response) { + console.log(response) if (response.response.output) { - const compilation_response = response.response.output - let output_response; - if (response.diff) { - const diff_response = response.diff - output_response = compilation_response + '
' + diff_response - } else { - output_response = compilation_response - } - document.getElementById('response-txt').innerHTML = output_response; + setOutput(response); } else if (response.response.error) { - document.getElementById('response-txt').innerHTML = response.response.error; + setError(response.response.error) } } + function setError(error) { + document.getElementById('response-txt').innerHTML = error; + document.getElementById('response-title').style.color = "#B00020"; + } + function handleError(response) { - console.log("error") - console.log(response) const compilation_response = response.response const diff_response = response.diff const output_response = compilation_response + '
' + diff_response - document.getElementById('response-txt').innerHTML = output_response; + setError(output_response) } function updateProblemDescription(response) { From b90aad0d2aff9eea7e091397e149e4445b97d8eb Mon Sep 17 00:00:00 2001 From: adevezin Date: Mon, 19 Sep 2022 13:18:37 -0400 Subject: [PATCH 10/23] update css to be less generic --- .../swiftplugin/static/css/swiftplugin.css | 76 ++----------------- .../swiftplugin/static/html/swiftplugin.html | 2 +- swiftplugin/swiftplugin/swiftplugin.py | 20 ++--- 3 files changed, 18 insertions(+), 80 deletions(-) diff --git a/swiftplugin/swiftplugin/static/css/swiftplugin.css b/swiftplugin/swiftplugin/static/css/swiftplugin.css index 534c0bb..a25b427 100644 --- a/swiftplugin/swiftplugin/static/css/swiftplugin.css +++ b/swiftplugin/swiftplugin/static/css/swiftplugin.css @@ -1,11 +1,6 @@ /* CSS for SwiftPluginXBlock */ -.nav-tabs .nav-link { - margin-bottom: -1px; - background: rgba(0, 0, 0, 0.5); -} - -.nav-link { +.nav-tabs .nav-link#yoursolution2-tab, .nav-link#empowrsolution-tab { padding: 8px 16px; color: #9E9E9E!important; } @@ -14,19 +9,14 @@ --bs-gutter-x: 0!important; } -.nav-pills .nav-link.active { +.nav-pills#myTab, .nav-link.active#yoursolution2-tab, .nav-link.active#empowrsolution-tab { background-color: transparent!important; border-radius: 16px 16px 0 0!important; font-weight: bold; color: #424242!important; } -.nav-tabs .nav-item.show .nav-link, .nav-tabs .nav-link.active { - color: #fff; - background-color: #3D9C06; -} - -.nav-link { +.nav-link#yoursolution2-tab, .nav-link#empowrsolution-tab { display: block; padding: .5rem 1rem; color: white; @@ -44,23 +34,7 @@ div#code-container-area { overflow:hidden; } -.code-instructions-background { - background: #3A3A3A; -} - -.code-instructions { - color: white; -} - -.tab-content { -} -.code-input { - height: 100%; - padding-left: 0; - padding-right: 0; -} - -.code-area { +/*.code-area { width: 100%; resize: none; border: none; @@ -68,48 +42,13 @@ div#code-container-area { padding-right: 0; padding-left: 0; outline: none; -} +}*/ .response-txt { color: black; white-space: pre-wrap; } -.code-row { - width: 100%; - margin-right: 0; - margin-left: 0; - display: flex; -} - -.code-container { - max-width: 100%; - width: auto; - display: flex; - height: inherit; -} - -.submit-btn { - background: #3D9C06; - border-radius: 6px; - color: white; - min-width: 100px; - border: 0; -} - -.button-row { - padding: 8px; -} - -.run-btn { - background: #2F75EC; - border-radius: 6px; - margin-left: 16px; - color: white; - border: 0; - min-width: 100px; -} - .code-bottom { background: none; height: 30vh; @@ -119,7 +58,6 @@ div#code-container-area { .tab-pane { min-height: 100px; - } .instructions-container { @@ -135,13 +73,13 @@ div#code-container-area { } -.btn-primary { +.btn-primary#run-btn, .btn-primary#submit-btn { color: #fff !important; background-color: #5c0e81 !important; border-color: #5c0e81 !important; } -.btn-primary:hover { +.btn-primary#run-btn:hover, .btn-primary#submit-btn:hover { background-color: #7730b0 !important; border-color: #7730b0 !important; } diff --git a/swiftplugin/swiftplugin/static/html/swiftplugin.html b/swiftplugin/swiftplugin/static/html/swiftplugin.html index b3a44ba..d3dac34 100644 --- a/swiftplugin/swiftplugin/static/html/swiftplugin.html +++ b/swiftplugin/swiftplugin/static/html/swiftplugin.html @@ -5,7 +5,7 @@

Title