Skip to content

Commit

Permalink
feat: complete get data from Shad as login
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Jan 13, 2025
1 parent 13f9baf commit dcc07ad
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
28 changes: 12 additions & 16 deletions apps/accounts/views/uuid_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
48 changes: 48 additions & 0 deletions proxies/Shad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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()

0 comments on commit dcc07ad

Please sign in to comment.