Skip to content

Commit

Permalink
fix: handlers refactor and format
Browse files Browse the repository at this point in the history
  • Loading branch information
nandodev-net authored and BryanttV committed Jan 30, 2024
1 parent 3d6c9db commit d99be87
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('platform_plugin_turnitin', '0001_initial'),
("platform_plugin_turnitin", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name='turnitinsubmission',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='turnitin_submissions', to=settings.AUTH_USER_MODEL),
model_name="turnitinsubmission",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="turnitin_submissions",
to=settings.AUTH_USER_MODEL,
),
),
]
6 changes: 5 additions & 1 deletion platform_plugin_turnitin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

User = get_user_model()


class TurnitinSubmission(models.Model):
"""
Represents a submission to Turnitin.
Expand All @@ -20,7 +21,10 @@ class TurnitinSubmission(models.Model):
Methods:
- __str__: Returns a string representation of the submission, showing its ID and creation date.
"""
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="turnitin_submissions")

user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="turnitin_submissions"
)
turnitin_submission_id = models.CharField(max_length=255, blank=True, null=True)
turnitin_submission_pdf_id = models.CharField(max_length=255, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
2 changes: 1 addition & 1 deletion platform_plugin_turnitin/static/html/turnitin.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>End User License Agreement (EULA)</h2>
<h2>File Upload</h2>
<form enctype="multipart/form-data">
<div class="file-upload">
<input type="file" name="myfile" id="file" accept=".doc, .docx">
<input type="file" name="uploaded_file" id="file" accept=".doc, .docx">
<label for="file">Select File</label>
<span class="selected-filename">No file selected</span>
</div>
Expand Down
2 changes: 1 addition & 1 deletion platform_plugin_turnitin/static/js/src/turnitin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function TurnitinXBlock(runtime, element) {
}

var formData = new FormData();
formData.append('myfile', file);
formData.append('uploaded_file', file);

$.ajax({
type: 'POST',
Expand Down
37 changes: 15 additions & 22 deletions platform_plugin_turnitin/turnitin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from datetime import datetime
from http import HTTPStatus

import pkg_resources
from django.contrib.auth.models import User
Expand Down Expand Up @@ -96,10 +97,14 @@ def get_user_data(self):
"""
user_service = self.runtime.service(self, "user")
current_user = user_service.get_current_user()
user_full_name = current_user.full_name.split()
return {
"user_id": current_user.opt_attrs["edx-platform.user_id"],
"user_email": current_user.emails[0],
"user_name": current_user.full_name.split(),
"user_name": user_full_name[0] if user_full_name else "no_name",
"user_last_name": " ".join(user_full_name[1:])
if len(user_full_name) > 1
else "no_last_name",
}

@XBlock.json_handler
Expand Down Expand Up @@ -147,7 +152,6 @@ def create_turnitin_submission_object(self):
Response: The response from the Turnitin submission API.
"""
user_data = self.get_user_data()
user_name = user_data["user_name"]
date_now = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")

payload = {
Expand All @@ -161,19 +165,15 @@ def create_turnitin_submission_object(self):
"owners": [
{
"id": user_data["user_id"],
"given_name": user_name[0] if user_name else "no_name",
"family_name": " ".join(user_name[1:])
if len(user_name) > 1
else "no_last_name",
"given_name": user_data["user_name"],
"family_name": user_data["user_last_name"],
"email": user_data["user_email"],
}
],
"submitter": {
"id": user_data["user_id"],
"given_name": user_name[0] if user_name else "no_name",
"family_name": " ".join(user_name[1:])
if len(user_name) > 1
else "no_last_name",
"given_name": user_data["user_name"],
"family_name": user_data["user_last_name"],
"email": user_data["user_email"],
},
"original_submitted_time": date_now,
Expand All @@ -194,18 +194,17 @@ def upload_turnitin_submission_file(self, data, suffix=""):
Response: The response after uploading the file to Turnitin.
"""
turnitin_submission = self.create_turnitin_submission_object()
if turnitin_submission.status_code == 201:
if turnitin_submission.status_code == HTTPStatus.CREATED:
turnitin_submission_id = turnitin_submission.json()["id"]
current_user_id = self.get_user_data()["user_id"]
current_user = User.objects.get(id=current_user_id)
submission = TurnitinSubmission(
user=current_user, turnitin_submission_id=turnitin_submission_id
)
submission.save()
myfile = data.params["myfile"].file
# turnitin_submission_id='0a966646-83f9-4ce6-aa47-71e07baf4e30'
uploaded_file = data.params["uploaded_file"].file
response = put_upload_submission_file_content(
turnitin_submission_id, myfile
turnitin_submission_id, uploaded_file
)
return Response(
json.dumps(response.json()),
Expand Down Expand Up @@ -238,7 +237,6 @@ def get_submission_status(self, data, suffix=""):
).latest("created_at")
except TurnitinSubmission.DoesNotExist:
return {"success": False}
# last_submission = 'de6784c5-471f-4220-aff1-16b6b44dffcf'
response = get_submission_info(last_submission.turnitin_submission_id)
return response.json()

Expand Down Expand Up @@ -296,7 +294,6 @@ def generate_similarity_report(self, data, suffix=""):
).latest("created_at")
except TurnitinSubmission.DoesNotExist:
return {"success": False}
# last_submission = 'de6784c5-471f-4220-aff1-16b6b44dffcf'
response = put_generate_similarity_report(
last_submission.turnitin_submission_id, payload
)
Expand All @@ -322,7 +319,6 @@ def get_similarity_report_status(self, data, suffix=""):
).latest("created_at")
except TurnitinSubmission.DoesNotExist:
return {"success": False}
# last_submission = 'de6784c5-471f-4220-aff1-16b6b44dffcf'
response = get_similarity_report_info(last_submission.turnitin_submission_id)
return response.json()

Expand Down Expand Up @@ -355,10 +351,8 @@ def create_similarity_viewer(self, data, suffix=""):
"view_settings": {"save_changes": True},
},
"author_metadata_override": {
"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",
"family_name": user_data["user_last_name"],
"given_name": user_data["user_name"],
},
"sidebar": {"default_mode": "similarity"},
}
Expand All @@ -369,7 +363,6 @@ def create_similarity_viewer(self, data, suffix=""):
).latest("created_at")
except TurnitinSubmission.DoesNotExist:
return {"success": False}
# last_submission = 'de6784c5-471f-4220-aff1-16b6b44dffcf'
response = post_create_viewer_launch_url(
last_submission.turnitin_submission_id, payload
)
Expand Down
Loading

0 comments on commit d99be87

Please sign in to comment.