customerwebsitebackend/api/serializers.py
vickytechkey 4ca070ad8c
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 35s
feat: implement customer registration, profile management, and auth tests
2026-11-01 20:53:36 +05:30

37 lines
1.6 KiB
Python

from rest_framework import serializers
from django.contrib.auth.models import User
from orders.models import CustomerProfile
class UserRegisterSerializer(serializers.ModelSerializer):
phone = serializers.CharField(write_only=True, required=False, allow_blank=True)
address = serializers.CharField(write_only=True, required=False, allow_blank=True)
password = serializers.CharField(write_only=True, required=False, default='TradhoxOtpUser2026!')
first_name = serializers.CharField(required=False, allow_blank=True)
last_name = serializers.CharField(required=False, allow_blank=True)
email = serializers.EmailField(required=False, allow_blank=True)
class Meta:
model = User
fields = ['username', 'email', 'password', 'first_name', 'last_name', 'phone', 'address']
def create(self, validated_data):
phone = validated_data.pop('phone', '') or validated_data.get('username')
address = validated_data.pop('address', '')
password = validated_data.pop('password', 'TradhoxOtpUser2026!')
user = User.objects.create_user(
username=validated_data['username'],
email=validated_data.get('email', ''),
first_name=validated_data.get('first_name', ''),
last_name=validated_data.get('last_name', ''),
password=password
)
CustomerProfile.objects.create(
user=user,
phone=phone,
default_address=address,
gender='Prefer not to say',
wallet_balance=0.00,
loyalty_coins=200
)
return user