Skip to content

Commit

Permalink
feat: workflow ajax error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
nandodev-net committed Oct 5, 2023
1 parent eed5e5d commit 510e21d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 78 deletions.
2 changes: 1 addition & 1 deletion platform_plugin_turnitin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
29 changes: 19 additions & 10 deletions platform_plugin_turnitin/static/css/turnitin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions platform_plugin_turnitin/static/html/turnitin.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ <h2>File Processing Status</h2>
<div id="yellowLight1" class="light yellow off"></div>
<div id="greenLight1" class="light green off"></div>
</div>
<div class="status-text" id="statusText1"></div>
<button id="refreshBtn1" class="refresh-btn">Refresh</button>
<button id="generateReportBtn1" class="generate-btn" disabled>Generate Similarity Report</button>
</section>
Expand All @@ -55,6 +56,7 @@ <h2>Similarity Processing Status</h2>
<div id="yellowLight2" class="light yellow off"></div>
<div id="greenLight2" class="light green off"></div>
</div>
<div class="status-text" id="statusText2"></div>
<button id="refreshBtn2" class="refresh-btn" disabled>Refresh</button>
<button id="generateReportBtn2" class="generate-btn" disabled>Launch Viewer</button>
</section>
Expand Down
74 changes: 54 additions & 20 deletions platform_plugin_turnitin/static/js/src/turnitin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
});
});
Expand All @@ -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.');
}
});
});
Expand Down Expand Up @@ -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.');
}
});
});
Expand Down Expand Up @@ -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.');
}
});
});


Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -211,28 +235,38 @@ 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');
yellowLight.classList.add('off');
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');
}
}

Expand Down
91 changes: 47 additions & 44 deletions platform_plugin_turnitin/turnitin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

from datetime import datetime
import json
from django.http import JsonResponse
import pkg_resources
from django.utils import translation
from xblock.core import XBlock
from xblock.fields import Integer, Scope
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,
Expand Down Expand Up @@ -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=''):
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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=''):
Expand Down Expand Up @@ -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 = {
Expand All @@ -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}


Expand Down
Loading

0 comments on commit 510e21d

Please sign in to comment.