<reponame>pawan0410/recipe-app-api
from django.contrib.auth import get_user_model, authenticate
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
    """Serializer for the user object"""
    class Meta:
        model = get_user_model()
        # Fields which are going to be converted to and
        # from json when we will make Http post or get requests
        # and then we retrieve them
        # in our views and save them in our base models.
        # Basically all the fields which
        # we want to be use in API end points.
        # Serializers are similar to django forms.
        fields = ('email', 'password', 'name')
        extra_kwargs = {'password': {'write_only': True, 'min_length': 5}}

    def create(self, validated_data):
        # validated_data is a json data which is passed to our serializer
        #  with POST req
        """create a new user with encrypted password and return it"""
        return get_user_model().objects.create_user(**validated_data)


class AuthTokenSerializer(serializers.Serializer):
    """Serializer for the user authentication object"""
    email = serializers.CharField()
    password = serializers.CharField(
        style={'input_type': 'password'},
        trim_whitespace=False
    )

    def validate(self, attrs):
        """Validate and authenticate the user"""
        email = attrs.get('email')
        password = attrs.get('password')

        user = authenticate(
            request=self.context.get('request'),
            username=email,
            password=password
        )

        if not user:
            msg = _('Unable to authenticate with provided credentials')
            raise serializers.ValidationError(msg, code='authentication')

        attrs['user'] = user
        return attrs

from django.contrib.auth import get_user_model, authenticate
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
    """Serializer for the user object"""
    class Meta:
        model = get_user_model()
        # Fields which are going to be converted to and
        # from json when we will make Http post or get requests
        # and then we retrieve them
        # in our views and save them in our base models.
        # Basically all the fields which
        # we want to be use in API end points.
        # Serializers are similar to django forms.
        fields = ('email', 'password', 'name')
        extra_kwargs = {'password': {'write_only': True, 'min_length': 5}}

    def create(self, validated_data):
        # validated_data is a json data which is passed to our serializer
        #  with POST req
        """create a new user with encrypted password and return it"""
        return get_user_model().objects.create_user(**validated_data)


class AuthTokenSerializer(serializers.Serializer):
    """Serializer for the user authentication object"""
    email = serializers.CharField()
    password = serializers.CharField(
        style={'input_type': 'password'},
        trim_whitespace=False
    )

    def validate(self, attrs):
        """Validate and authenticate the user"""
        email = attrs.get('email')
        password = attrs.get('password')

        user = authenticate(
            request=self.context.get('request'),
            username=email,
            password=password
        )

        if not user:
            msg = _('Unable to authenticate with provided credentials')
            raise serializers.ValidationError(msg, code='authentication')

        attrs['user'] = user
        return attrs
Serializer for the user object # Fields which are going to be converted to and # from json when we will make Http post or get requests # and then we retrieve them # in our views and save them in our base models. # Basically all the fields which # we want to be use in API end points. # Serializers are similar to django forms. # validated_data is a json data which is passed to our serializer #  with POST req create a new user with encrypted password and return it Serializer for the user authentication object Validate and authenticate the user