From 510e21dd68545b74a62513080fc2f30bd61e2732 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 5 Oct 2023 09:24:28 -0400 Subject: [PATCH] feat: workflow ajax error handle --- platform_plugin_turnitin/models.py | 2 +- .../static/css/turnitin.css | 29 ++++-- .../static/html/turnitin.html | 2 + .../static/js/src/turnitin.js | 74 +++++++++++---- platform_plugin_turnitin/turnitin.py | 91 ++++++++++--------- .../turnitin_client/handlers.py | 11 ++- 6 files changed, 131 insertions(+), 78 deletions(-) diff --git a/platform_plugin_turnitin/models.py b/platform_plugin_turnitin/models.py index 954e3cf..abfb41f 100644 --- a/platform_plugin_turnitin/models.py +++ b/platform_plugin_turnitin/models.py @@ -12,4 +12,4 @@ class TurnitinSubmission(models.Model): created_at = models.DateTimeField(auto_now_add=True) def __str__(self): - return f"Submission by {self.user.username} - Turnitin ID: {self.turnitin_submission_id or 'Not Set'}" + return f"Submission: {self.turnitin_submission_id or 'Not Set'} - created at: {self.created_at}" diff --git a/platform_plugin_turnitin/static/css/turnitin.css b/platform_plugin_turnitin/static/css/turnitin.css index 28f0714..6a5ec07 100644 --- a/platform_plugin_turnitin/static/css/turnitin.css +++ b/platform_plugin_turnitin/static/css/turnitin.css @@ -9,16 +9,6 @@ } - -body { - font-family: 'Arial', sans-serif; - display: flex; - justify-content: center; - align-items: center; - height: 100vh; - background-color: #eef2f7; -} - /* Estilo base para todas las secciones */ .turnitin-test-section { padding: 20px; @@ -133,6 +123,25 @@ button { } /* CSS PARA STATUS*/ +.status-text { + font-size: 16px; + margin-top: 10px; + font-weight: bold; +} + +.red-text { + color: red; +} + +.yellow-text { + color: yellow; +} + +.green-text { + color: green; +} + + .traffic-light { width: 50px; height: 150px; diff --git a/platform_plugin_turnitin/static/html/turnitin.html b/platform_plugin_turnitin/static/html/turnitin.html index a7bda65..31e2677 100644 --- a/platform_plugin_turnitin/static/html/turnitin.html +++ b/platform_plugin_turnitin/static/html/turnitin.html @@ -43,6 +43,7 @@

File Processing Status

+
@@ -55,6 +56,7 @@

Similarity Processing Status

+
diff --git a/platform_plugin_turnitin/static/js/src/turnitin.js b/platform_plugin_turnitin/static/js/src/turnitin.js index c2b6a80..1fca4c3 100644 --- a/platform_plugin_turnitin/static/js/src/turnitin.js +++ b/platform_plugin_turnitin/static/js/src/turnitin.js @@ -32,11 +32,16 @@ function TurnitinXBlock(runtime, element) { type: "POST", url: handlerUrl, data: JSON.stringify({"hello": "world"}), + success: function(response) { - showEULA(response); + if(response.status === 404) { + alert(`ERROR: ${response.status} - No EULA page for the given version was found`); + } else { + showEULA(response.html); + } }, error: function() { - alert('Error al obtener el contenido del EULA.'); + alert('Error getting EULA.'); } }); }); @@ -49,10 +54,16 @@ function TurnitinXBlock(runtime, element) { url: handlerUrl, data: JSON.stringify({"hello": "world"}), success: function(response) { - alert('EULA accepted!.'); + if(response.success === false && (response.status === 400 || response.status === 404)) { + updateTrafficLightState('1', 'ERROR'); + alert(`ERROR: ${response.status} - ${response.message}`); + } else { + alert('EULA successfully accepted!'); + } + }, error: function() { - alert('Error getting EULA content.'); + alert('Error accepting EULA.'); } }); }); @@ -83,14 +94,15 @@ function TurnitinXBlock(runtime, element) { processData: false, contentType: false, success: function(response) { - if (response.success) { - alert('Archivo subido con éxito a Turnitin.'); - } else { - alert('Hubo un error al subir el archivo a Turnitin.'); - } + if(response.success === false && (response.status === 404 || response.status === 413 || response.status === 422 || response.status === 409)) { + alert(`ERROR: ${response.status} - ${response.message}`); + } else { + alert('File successfully uploaded to Turnitin!'); + } + }, error: function() { - alert('Error comunicándose con el servidor.'); + alert('Error uploading file.'); } }); }); @@ -129,16 +141,22 @@ function TurnitinXBlock(runtime, element) { var handlerUrl = runtime.handlerUrl(element, 'get_submission_status'); $.ajax({ - type: "POST", - url: handlerUrl, - data: JSON.stringify({"hello": "world"}), - success: function(response) { - updateTrafficLightState('1', response['submission_status']); - }, - error: function() { - alert('Error al obtener status de la entrega.'); + type: "POST", + url: handlerUrl, + data: JSON.stringify({"hello": "world"}), + success: function(response) { + if(response.success === false && response.status === 404) { + updateTrafficLightState('1', 'ERROR'); + alert(`ERROR: ${response.status} - ${response.message}`); + } else { + updateTrafficLightState('1', response['status']); } - }); + + }, + error: function() { + alert('Error getting report status.'); + } + }); }); @@ -172,7 +190,13 @@ function TurnitinXBlock(runtime, element) { url: handlerUrl, data: JSON.stringify({"hello": "world"}), success: function(response) { - updateTrafficLightState('2', response['report_status']); + if(response.success === false && response.status === 404) { + updateTrafficLightState('2', 'ERROR'); + alert(`ERROR: ${response.status} - ${response.message}`); + } else { + updateTrafficLightState('2', response['status']); + } + }, error: function() { alert('Error getting report status.'); @@ -211,6 +235,7 @@ function TurnitinXBlock(runtime, element) { const yellowLight = document.getElementById(`yellowLight${semaphoreNumber}`); const greenLight = document.getElementById(`greenLight${semaphoreNumber}`); const generateReportBtn = document.getElementById(`generateReportBtn${semaphoreNumber}`); + const statusText = document.getElementById(`statusText${semaphoreNumber}`); const refreshBtn2 = document.getElementById(`refreshBtn2`); redLight.classList.add('off'); @@ -218,21 +243,30 @@ function TurnitinXBlock(runtime, element) { greenLight.classList.add('off'); generateReportBtn.disabled = true; generateReportBtn.classList.remove('enabled'); + statusText.textContent = state; + + statusText.classList.remove('red-text', 'yellow-text', 'green-text'); switch (state) { case 'ERROR': redLight.classList.remove('off'); + statusText.classList.add('red-text'); break; case 'PROCESSING': yellowLight.classList.remove('off'); + statusText.classList.add('yellow-text'); break; case 'COMPLETE': greenLight.classList.remove('off'); + statusText.classList.add('green-text'); generateReportBtn.disabled = false; generateReportBtn.classList.add('enabled'); refreshBtn2.disabled = false; refreshBtn2.classList.add('enabled'); break; + default: + redLight.classList.remove('off'); + statusText.classList.add('red-text'); } } diff --git a/platform_plugin_turnitin/turnitin.py b/platform_plugin_turnitin/turnitin.py index b645bbb..6fb8808 100644 --- a/platform_plugin_turnitin/turnitin.py +++ b/platform_plugin_turnitin/turnitin.py @@ -2,7 +2,6 @@ from datetime import datetime import json -from django.http import JsonResponse import pkg_resources from django.utils import translation from xblock.core import XBlock @@ -10,7 +9,7 @@ from xblock.fragment import Fragment from xblockutils.resources import ResourceLoader from django.contrib.auth.models import User -#from .turnitin_api.models import TurnitinSubmission +from .models import TurnitinSubmission from platform_plugin_turnitin.turnitin_client.handlers import (get_eula_page, post_accept_eula_version, post_create_submission, @@ -105,8 +104,8 @@ def increment_count(self, data, suffix=''): @XBlock.json_handler def get_eula_agreement(self, data, suffix=''): - return get_eula_page() - + response = get_eula_page() + return {'html': response.text, 'status':response.status_code} @XBlock.json_handler def accept_eula_agreement(self, data, suffix=''): @@ -116,7 +115,8 @@ def accept_eula_agreement(self, data, suffix=''): payload = { "user_id": str(user_id), "accepted_timestamp": date_now, "language": "en-US" } - return post_accept_eula_version(payload) + response = post_accept_eula_version(payload) + return response.json() def create_turnitin_submission_object(self): current_user = self.runtime.service(self, 'user').get_current_user() @@ -158,11 +158,13 @@ def upload_turnitin_submission_file(self, data, suffix=''): turnitin_submission = self.create_turnitin_submission_object() if turnitin_submission.status_code == 201: turnitin_submission_id = turnitin_submission.json()['id'] - #current_user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id'] - #current_user = User.objects.get(id=current_user_id) - #submission = TurnitinSubmission(user = current_user, turnitin_submission_id=turnitin_submission_id) - #submission.save() - #print('SUBMISSION CREATED<<<<<<<<<<<<<<<', submission) + current_user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id'] + current_user = User.objects.get(id=current_user_id) + submission = TurnitinSubmission(user = current_user, turnitin_submission_id=turnitin_submission_id) + submission.save() + print('\n\n') + print('SUBMISSION CREATED<<<<<<<<<<<<<<<', submission) + print('\n\n') myfile = data.params['myfile'].file #turnitin_submission_id='0a966646-83f9-4ce6-aa47-71e07baf4e30' response = put_upload_submission_file_content(turnitin_submission_id, myfile) @@ -171,14 +173,15 @@ def upload_turnitin_submission_file(self, data, suffix=''): @XBlock.json_handler def get_submission_status(self, data, suffix=''): - current_user = self.runtime.service(self, 'user').get_current_user() - # try: - # last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') - # except TurnitinSubmission.DoesNotExist: - # return None - # return get_submission_info(last_submission.turnitin_submission_id) - status = get_submission_info('0a966646-83f9-4ce6-aa47-71e07baf4e30') - return {'submission_status':status} + current_user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id'] + current_user = User.objects.get(id=current_user_id) + try: + last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') + except TurnitinSubmission.DoesNotExist: + return {'success':False} + # last_submission = '0a966646-83f9-4ce6-aa47-71e07baf4e30' + response = get_submission_info(last_submission.turnitin_submission_id) + return response.json() @XBlock.json_handler def generate_similarity_report(self, data, suffix=''): @@ -217,33 +220,33 @@ def generate_similarity_report(self, data, suffix=''): "exclude_submitted_works": False } } - current_user = self.runtime.service(self, 'user').get_current_user() - # try: - # last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') - # except TurnitinSubmission.DoesNotExist: - # return None - # return put_generate_similarity_report(last_submission.turnitin_submission_id) - - response = put_generate_similarity_report('0a966646-83f9-4ce6-aa47-71e07baf4e30', payload) + current_user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id'] + current_user = User.objects.get(id=current_user_id) + try: + last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') + except TurnitinSubmission.DoesNotExist: + {'success':False} + # last_submission = '0a966646-83f9-4ce6-aa47-71e07baf4e30' + response = put_generate_similarity_report(last_submission.turnitin_submission_id, payload) if response.status_code == 202: return {'success':True} return {'success':False} @XBlock.json_handler def get_similarity_report_status(self, data, suffix=''): - current_user = self.runtime.service(self, 'user').get_current_user() - # try: - # last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') - # except TurnitinSubmission.DoesNotExist: - # return None - # return get_similarity_report_info(last_submission.turnitin_submission_id) - status = get_similarity_report_info('0a966646-83f9-4ce6-aa47-71e07baf4e30') - return {'report_status':status} + current_user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id'] + current_user = User.objects.get(id=current_user_id) + try: + last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') + except TurnitinSubmission.DoesNotExist: + return {'success': False} + # last_submission = '0a966646-83f9-4ce6-aa47-71e07baf4e30' + response = get_similarity_report_info(last_submission.turnitin_submission_id) + return response.json() @XBlock.json_handler def create_similarity_viewer(self, data, suffix=''): current_user = self.runtime.service(self, 'user').get_current_user() - user_email = current_user.emails[0] user_name = current_user.full_name.split() user_id = current_user.opt_attrs['edx-platform.user_id'] payload = { @@ -266,20 +269,20 @@ def create_similarity_viewer(self, data, suffix=''): } }, "author_metadata_override": { - "family_name": "Smith", - "given_name": "John" + "family_name": ' '.join(user_name[1:]) if len(user_name) > 1 else "no_last_name", + "given_name": user_name[0] if user_name else "no_name", }, "sidebar": { "default_mode": "similarity" } } - current_user = self.runtime.service(self, 'user').get_current_user() - # try: - # last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') - # except TurnitinSubmission.DoesNotExist: - # return None - # return post_create_viewer_launch_url(last_submission.turnitin_submission_id) - url = post_create_viewer_launch_url('0a966646-83f9-4ce6-aa47-71e07baf4e30', payload) + current_user = User.objects.get(id=user_id) + try: + last_submission = TurnitinSubmission.objects.filter(user=current_user).latest('created_at') + except TurnitinSubmission.DoesNotExist: + return {'success': False} + # last_submission = '0a966646-83f9-4ce6-aa47-71e07baf4e30' + url = post_create_viewer_launch_url(last_submission.turnitin_submission_id, payload) return {'viewer_url': url} diff --git a/platform_plugin_turnitin/turnitin_client/handlers.py b/platform_plugin_turnitin/turnitin_client/handlers.py index 8edc99e..7a2c86b 100644 --- a/platform_plugin_turnitin/turnitin_client/handlers.py +++ b/platform_plugin_turnitin/turnitin_client/handlers.py @@ -97,13 +97,15 @@ def get_eula_version_info(version: str = 'latest', language: str = 'EN'): response = turnitin_api_handler('get', f'eula/{version}?lang={language}') pretty_print_response(response) +#TODO: done def get_eula_page(version: str = 'v1beta', language: str = 'en-US'): """ Returns the HTML content for a specified EULA version. """ response = turnitin_api_handler('get', f'/eula/{version}/view?lang={language}') - return response.text + return response +#TODO: done def post_accept_eula_version(payload, version: str = 'v1beta'): """ Accepts a specific EULA version. @@ -111,6 +113,7 @@ def post_accept_eula_version(payload, version: str = 'v1beta'): """ response = turnitin_api_handler('post', f'eula/{version}/accept', payload) pretty_print_response(response, 'ACCEPT EULA') + return response def get_eula_acceptance_by_user(user_id): """ @@ -142,6 +145,7 @@ def put_upload_submission_file_content(submission_id, file): pretty_print_response(response, 'UPLOAD FILE') return response +#TODO: done def get_submission_info(submission_id): """ Fetches all the information related to a specific submission. @@ -155,7 +159,7 @@ def get_submission_info(submission_id): """ response = turnitin_api_handler('get', f'submissions/{submission_id}') pretty_print_response(response, 'SUBMISSION STATUS') - return response.json()['status'] + return response def delete_submission(submission_id, is_hard_delete='false'): """ @@ -185,6 +189,7 @@ def put_generate_similarity_report(submission_id, payload): pretty_print_response(response, 'REPORT GENERATION') return response +#TODO: done def get_similarity_report_info(submission_id): """ Returns summary information about the requested Similarity Report. @@ -194,7 +199,7 @@ def get_similarity_report_info(submission_id): """ response = turnitin_api_handler('get', f'submissions/{submission_id}/similarity') pretty_print_response(response, 'REPORT STATUS') - return response.json()['status'] + return response def post_create_viewer_launch_url(submission_id, payload): """