Initial updates with FE

This commit is contained in:
2026-04-10 21:41:26 -05:00
parent 562a8525d0
commit bb8af62f2d
9 changed files with 119 additions and 11 deletions

View File

@@ -1,7 +1,8 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers
from .models import VendorProfile
from .models import CustomerProfile, VendorProfile
User = get_user_model()
@@ -74,8 +75,46 @@ class VendorRegistrationSerializer(serializers.Serializer):
return user
class CustomerProfileSerializer(serializers.ModelSerializer):
class Meta:
model = CustomerProfile
fields = (
"preferred_contact_method",
"emergency_contact_name",
"emergency_contact_phone",
"created_at",
"updated_at",
)
read_only_fields = fields
class CustomerRegistrationSerializer(serializers.Serializer):
email = serializers.EmailField()
password = serializers.CharField(write_only=True, min_length=8)
first_name = serializers.CharField(required=False, allow_blank=True, max_length=150)
last_name = serializers.CharField(required=False, allow_blank=True, max_length=150)
phone_number = serializers.CharField(required=False, allow_blank=True, max_length=32)
def validate_email(self, value):
if User.objects.filter(email__iexact=value).exists():
raise serializers.ValidationError("A user with this email already exists.")
return value
def create(self, validated_data):
password = validated_data.pop("password")
user = User.objects.create_user(
password=password,
is_vendor=False,
is_customer=True,
**validated_data,
)
CustomerProfile.objects.create(user=user)
return user
class UserMeSerializer(serializers.ModelSerializer):
vendor_profile = VendorProfileSerializer(read_only=True)
customer_profile = serializers.SerializerMethodField()
class Meta:
model = User
@@ -88,4 +127,12 @@ class UserMeSerializer(serializers.ModelSerializer):
"is_vendor",
"is_customer",
"vendor_profile",
"customer_profile",
)
def get_customer_profile(self, obj):
try:
profile = obj.customer_profile
except ObjectDoesNotExist:
return None
return CustomerProfileSerializer(profile).data