from rest_framework import serializers
from p_api import models

"""class HelloSerializer(serializers.Serializer):
     Serializes a name field for testing our APIView 
    name = serializers.CharField(max_length = 10)"""


class UserProfileSerializer(serializers.ModelSerializer):

    """ Serializes a user profile object """

    class Meta: 
        model = models.UserProfile
        fields = ('id', 'email', 'name', 'password')
        
        # Sadece profil oluştururken password gözüksün diye yapıyoruz
        extra_kwargs ={
            'password' : {
                'write_only': True,
                'style':{'input_type' : 'password'}
            }
        }

    def create(self, validated_data):
        """ Create and return new user """
        user = models.UserProfile.object.create_user(
            email = validated_data['email'],
            name = validated_data['name'],
            password= validated_data['password']
        )

        return user

    def update(self, instance, validated_data):
        """Handle updating user account"""
        if 'password' in validated_data:
            password = validated_data.pop('password')
            instance.set_password(password)

        return super().update(instance, validated_data)



    


    
from rest_framework import serializers
from p_api import models

"""class HelloSerializer(serializers.Serializer):
     Serializes a name field for testing our APIView 
    name = serializers.CharField(max_length = 10)"""


class UserProfileSerializer(serializers.ModelSerializer):

    """ Serializes a user profile object """

    class Meta: 
        model = models.UserProfile
        fields = ('id', 'email', 'name', 'password')
        
        # Sadece profil oluştururken password gözüksün diye yapıyoruz
        extra_kwargs ={
            'password' : {
                'write_only': True,
                'style':{'input_type' : 'password'}
            }
        }

    def create(self, validated_data):
        """ Create and return new user """
        user = models.UserProfile.object.create_user(
            email = validated_data['email'],
            name = validated_data['name'],
            password= validated_data['password']
        )

        return user

    def update(self, instance, validated_data):
        """Handle updating user account"""
        if 'password' in validated_data:
            password = validated_data.pop('password')
            instance.set_password(password)

        return super().update(instance, validated_data)



    


    
class HelloSerializer(serializers.Serializer):      Serializes a name field for testing our APIView      name = serializers.CharField(max_length = 10) Serializes a user profile object # Sadece profil oluştururken password gözüksün diye yapıyoruz Create and return new user Handle updating user account