Skip to content

Commit

Permalink
feat: add origin for uuid-logins from shad
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Nov 13, 2024
1 parent d8474cf commit 63b0e68
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
18 changes: 18 additions & 0 deletions apps/accounts/migrations/0044_user_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.3 on 2024-11-13 12:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0043_rename_is_artificial_user_is_temporary'),
]

operations = [
migrations.AddField(
model_name='user',
name='origin',
field=models.CharField(blank=True, max_length=50, null=True),
),
]
1 change: 1 addition & 0 deletions apps/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Gender(models.TextChoices):
city = models.CharField(max_length=50, null=True, blank=True)
postal_code = models.CharField(max_length=10, null=True, blank=True)
is_temporary = models.BooleanField(default=False)
origin = models.CharField(max_length=50, blank=True, null=True)

def get_user_website(self, website):
try:
Expand Down
2 changes: 1 addition & 1 deletion apps/accounts/serializers/user_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def update(self, instance, validated_data):
class Meta:
model = User
fields = ['id', 'phone_number', 'first_name', 'last_name',
'password', 'username', 'email', 'is_temporary']
'password', 'username', 'email', 'is_temporary', 'origin']
read_only_fields = ['id']


Expand Down
42 changes: 41 additions & 1 deletion apps/accounts/views/uuid_login.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import uuid
from django.db import transaction
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response

from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils import create_or_get_user, find_user_in_website
from apps.accounts.views.base_login import BaseLoginView


import uuid
from rest_framework import status
from rest_framework.response import Response


class UserIDLoginView(BaseLoginView):
user_data_field = 'id'

Expand All @@ -20,6 +27,7 @@ class UserIDLoginView(BaseLoginView):
)
def post(self, request):
user_id = request.data.get("user_id")
origin = request.data.get("origin", "")

# Check if user_id is provided
if not user_id:
Expand All @@ -29,7 +37,8 @@ def post(self, request):
if not self.is_valid_uuid(user_id):
return Response({"error": "Invalid UserID format. Must be a valid UUID."}, status=status.HTTP_400_BAD_REQUEST)

return self.handle_post(request)
# Pass the origin to handle_post
return self.handle_post(request, origin=origin)

def get_user_identifier(self, request):
return request.data.get("user_id")
Expand All @@ -40,3 +49,34 @@ def is_valid_uuid(self, value):
return True
except ValueError:
return False

@transaction.atomic
def handle_post(self, request, origin=""):
user_identifier = self.get_user_identifier(request)
website = request.headers.get("Website")

try:
user = find_user_in_website(
user_data={self.user_data_field: user_identifier},
website=website,
raise_exception=True
)
created = False
response_status = status.HTTP_200_OK
except:
user_data = self.get_user_data(user_identifier)
user, created = create_or_get_user(user_data, website=website)

if created:
user.set_unusable_password()
user.origin = origin # Save origin for new users
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"])

self.create_login_event(user, website)
return self.generate_response(user, created, response_status)

0 comments on commit 63b0e68

Please sign in to comment.