Skip to content

Commit

Permalink
fix: code upgrades
Browse files Browse the repository at this point in the history
- turnitin:get_user_data() : delete 'user_' prefix.
- turnitin:get_django_user(): method created.
- api_hander: get_request_method_func(): method created.
- api_handler: turnitin_api_handler(): changes abour args.
  • Loading branch information
nandodev-net authored and BryanttV committed Jan 30, 2024
1 parent d99be87 commit 23a207b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
44 changes: 21 additions & 23 deletions platform_plugin_turnitin/turnitin.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,23 @@ 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()
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": 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
"name": full_name[0] if full_name else "no_name",
"last_name": " ".join(full_name[1:])
if len(full_name) > 1
else "no_last_name",
}

def get_django_user(self):
"""
Returns the django user.
"""
current_user_id = self.get_user_data()["user_id"]
return User.objects.get(id=current_user_id)

@XBlock.json_handler
def get_eula_agreement(self, data, suffix=""):
"""
Expand Down Expand Up @@ -165,15 +172,15 @@ def create_turnitin_submission_object(self):
"owners": [
{
"id": user_data["user_id"],
"given_name": user_data["user_name"],
"family_name": user_data["user_last_name"],
"given_name": user_data["name"],
"family_name": user_data["last_name"],
"email": user_data["user_email"],
}
],
"submitter": {
"id": user_data["user_id"],
"given_name": user_data["user_name"],
"family_name": user_data["user_last_name"],
"given_name": user_data["name"],
"family_name": user_data["last_name"],
"email": user_data["user_email"],
},
"original_submitted_time": date_now,
Expand All @@ -196,8 +203,7 @@ def upload_turnitin_submission_file(self, data, suffix=""):
turnitin_submission = self.create_turnitin_submission_object()
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)
current_user = self.get_django_user()
submission = TurnitinSubmission(
user=current_user, turnitin_submission_id=turnitin_submission_id
)
Expand All @@ -208,13 +214,9 @@ def upload_turnitin_submission_file(self, data, suffix=""):
)
return Response(
json.dumps(response.json()),
content_type="application/json",
charset="UTF-8",
)
return Response(
json.dumps(turnitin_submission.json()),
content_type="application/json",
charset="UTF-8",
)

@XBlock.json_handler
Expand All @@ -229,8 +231,7 @@ def get_submission_status(self, data, suffix=""):
Returns:
dict: Information related to the user's latest Turnitin submission.
"""
current_user_id = self.get_user_data()["user_id"]
current_user = User.objects.get(id=current_user_id)
current_user = self.get_django_user()
try:
last_submission = TurnitinSubmission.objects.filter(
user=current_user
Expand Down Expand Up @@ -286,8 +287,7 @@ def generate_similarity_report(self, data, suffix=""):
"exclude_submitted_works": False,
},
}
current_user_id = self.get_user_data()["user_id"]
current_user = User.objects.get(id=current_user_id)
current_user = self.get_django_user()
try:
last_submission = TurnitinSubmission.objects.filter(
user=current_user
Expand All @@ -311,8 +311,7 @@ def get_similarity_report_status(self, data, suffix=""):
Returns:
dict: Information related to the status of the similarity report.
"""
current_user_id = self.get_user_data()["user_id"]
current_user = User.objects.get(id=current_user_id)
current_user = self.get_django_user()
try:
last_submission = TurnitinSubmission.objects.filter(
user=current_user
Expand All @@ -335,7 +334,6 @@ def create_similarity_viewer(self, data, suffix=""):
dict: Contains the URL for the similarity viewer.
"""
user_data = self.get_user_data()
user_name = user_data["user_name"]
payload = {
"viewer_user_id": user_data["user_id"],
"locale": "en-EN",
Expand All @@ -351,8 +349,8 @@ def create_similarity_viewer(self, data, suffix=""):
"view_settings": {"save_changes": True},
},
"author_metadata_override": {
"family_name": user_data["user_last_name"],
"given_name": user_data["user_name"],
"family_name": user_data["last_name"],
"given_name": user_data["name"],
},
"sidebar": {"default_mode": "similarity"},
}
Expand Down
57 changes: 35 additions & 22 deletions platform_plugin_turnitin/turnitin_client/handlers/api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@
TCA_API_KEY = getattr(settings, "TURNITIN_TCA_API_KEY", None)


def get_request_method_func(request_method: str):
"""
Retrieve the appropriate request method function from the `requests` library
based on the provided HTTP request method.
Parameters:
- request_method (str): The HTTP method as a string (e.g., 'GET', 'POST', 'PUT', 'PATCH', 'DELETE').
Returns:
- function: The corresponding function from the `requests` library (e.g., requests.get, requests.post).
Raises:
- ValueError: If the provided request_method is unsupported or not recognized.
"""
method_map = {
"get": requests.get,
"post": requests.post,
"put": requests.put,
"delete": requests.delete,
"patch": requests.patch,
}
method_func = method_map.get(request_method.lower())
if not method_func:
raise ValueError(f"Unsupported request method: {request_method}")
return method_func


def turnitin_api_handler(
request_method: str,
url_prefix: str = "",
Expand All @@ -28,9 +55,6 @@ def turnitin_api_handler(
Returns:
- Response: A requests.Response object containing the server's response to the request.
Raises:
- ValueError: If an unsupported request method is provided.
"""
headers = {
"X-Turnitin-Integration-Name": TCA_INTEGRATION_FAMILY,
Expand All @@ -49,27 +73,16 @@ def turnitin_api_handler(
)
return response

method_map = {
"get": requests.get,
"post": requests.post,
"put": requests.put,
"delete": requests.delete,
"patch": requests.patch,
}

method_func = method_map.get(request_method.lower())
method_func = get_request_method_func(request_method)

if not method_func:
raise ValueError(f"Unsupported request method: {request_method}")
args = {
"headers": headers,
"json"
if request_method.lower() in ["post", "put", "patch"]
else "params": data,
}

if request_method.lower() in ["post", "put", "patch"]:
response = method_func(
f"{TII_API_URL}/api/v1/{url_prefix}", headers=headers, json=data
)
else:
response = method_func(
f"{TII_API_URL}/api/v1/{url_prefix}", headers=headers, params=data
)
response = method_func(f"{TII_API_URL}/api/v1/{url_prefix}", **args)

return response

Expand Down

0 comments on commit 23a207b

Please sign in to comment.