From dcc07ad782711849cc01c177c95e681e1b32c4ad Mon Sep 17 00:00:00 2001 From: Seyed Alireza Hashemi Date: Mon, 13 Jan 2025 17:11:47 +0330 Subject: [PATCH] feat: complete get data from Shad as login --- apps/accounts/views/uuid_login.py | 28 ++++++++---------- proxies/Shad.py | 48 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/apps/accounts/views/uuid_login.py b/apps/accounts/views/uuid_login.py index 43b64c6b..d9c557f8 100644 --- a/apps/accounts/views/uuid_login.py +++ b/apps/accounts/views/uuid_login.py @@ -13,7 +13,7 @@ from rest_framework import status from rest_framework.response import Response -from proxies.Shad import get_user_data_from_shad +from proxies.Shad import get_user_data_from_shad, update_user_info_by_shad_data class UserIDLoginView(BaseLoginView): @@ -48,22 +48,14 @@ def get_user_identifier(self, request): def is_valid_uuid(self, value): try: uuid.UUID(str(value)) + return True except ValueError: return False - try: - user_data = get_user_data_from_shad( - user_uuid=value, - landing_id=284 - ) - except ValueError as e: - return False - - return True - @transaction.atomic def handle_post(self, request, origin=""): user_identifier = self.get_user_identifier(request) + landing_id = request.data.get('landing_id') website = request.headers.get("Website") try: @@ -80,14 +72,18 @@ def handle_post(self, request, origin=""): if created: user.set_unusable_password() - user.origin = origin # Save origin for new users + user.origin = origin user.save() response_status = status.HTTP_201_CREATED - # Update origin for existing users - if not created and origin: - user.origin = origin - user.save(update_fields=["origin"]) + try: + user_data = get_user_data_from_shad( + user_uuid=user_identifier, + landing_id=landing_id, + ) + update_user_info_by_shad_data(user, user_data) + except ValueError as e: + raise Exception('cant get data from Shad') self.create_login_event(user, website) return self.generate_response(user, created, response_status) diff --git a/proxies/Shad.py b/proxies/Shad.py index 9dbaa402..321e7e0e 100644 --- a/proxies/Shad.py +++ b/proxies/Shad.py @@ -54,6 +54,9 @@ def get_user_data_from_shad(user_uuid, landing_id): Raises: ValueError: If UUID is invalid, authentication fails, or response contains errors """ + # Format uuid as Shad uuids format + user_uuid = _format_uuid_to_shad_format(user_uuid) + # First ensure we have a valid UUID try: uuid.UUID(str(user_uuid)) @@ -93,3 +96,48 @@ def get_user_data_from_shad(user_uuid, landing_id): raise ValueError(f"API Error: {error_message} (Status: {status})") return response + + +def _format_uuid_to_shad_format(input_uuid): + """ + Formats a UUID string into the desired format: E7-17-70-D6-D0-37-26-FE-4F-74-A7-ED-89-23-46-FC. + + Args: + input_uuid (str): A UUID string (with or without dashes). + + Returns: + str: The formatted UUID string. + """ + # Remove any existing dashes and convert to uppercase + clean_uuid = input_uuid.replace('-', '').upper() + + # Split into groups of two characters and join with dashes + formatted_uuid = '-'.join(clean_uuid[i:i+2] + for i in range(0, len(clean_uuid), 2)) + + return formatted_uuid + + +def update_user_info_by_shad_data(user_instance, user_data): + """ + Updates a user instance with the provided user_data dictionary. + + Args: + user_instance: The user instance to update. + user_data (dict): The data containing user information. + + Returns: + The updated user instance. + """ + # Extracting relevant data from user_data + data = user_data.get("data", {}) + + # Mapping user_data fields to the user instance fields + user_instance.first_name = data.get("name") or user_instance.first_name + user_instance.last_name = data.get("family") or user_instance.last_name + user_instance.phone_number = data.get( + "mobile") or user_instance.phone_number + user_instance.gender = data.get("gender") or user_instance.gender + user_instance.province = data.get("provinceName") or user_instance.province + + user_instance.save()