From c08dde367917e23c43253f5031f1eacdfef219b4 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 10 Oct 2023 12:45:16 -0400 Subject: [PATCH] fix: code upgrades - 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. --- platform_plugin_turnitin/turnitin.py | 44 +++++++------- .../turnitin_client/handlers/api_handler.py | 57 ++++++++++++------- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/platform_plugin_turnitin/turnitin.py b/platform_plugin_turnitin/turnitin.py index adc2702..0e7b69c 100644 --- a/platform_plugin_turnitin/turnitin.py +++ b/platform_plugin_turnitin/turnitin.py @@ -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=""): """ @@ -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, @@ -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 ) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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", @@ -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"}, } diff --git a/platform_plugin_turnitin/turnitin_client/handlers/api_handler.py b/platform_plugin_turnitin/turnitin_client/handlers/api_handler.py index 5cbd94f..b37b1fb 100644 --- a/platform_plugin_turnitin/turnitin_client/handlers/api_handler.py +++ b/platform_plugin_turnitin/turnitin_client/handlers/api_handler.py @@ -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 = "", @@ -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, @@ -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