from rest_framework import serializers
from django.contrib.auth.models import User
from main_app.models import *
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password


class GenreSerializer(serializers.ModelSerializer):

    class Meta:
        model = Genre
        fields = "__all__"


class ArtistSerializer(serializers.ModelSerializer):

    class Meta:
        model = Artist
        fields = "__all__"


class TrackSerializer(serializers.ModelSerializer):

    class Meta:
        model = Track
        fields = "__all__"


class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User
        fields = ["url", "username", "email", "last_login", "is_superuser", "is_staff"]


UserModel = get_user_model()


class UserRegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(
        write_only=True,
        required=True,
        # help_text='Leave empty if no change needed',
        style={"input_type": "password", "placeholder": "Password"}
    )

    class Meta:
        model = User
        fields = ("url", "username", "email", "first_name", "last_name", "password")

    def create(self, validated_data):
        validated_data["password"] = make_password(validated_data.get("password"))
        return super(UserRegisterSerializer, self).create(validated_data)

from rest_framework import serializers
from django.contrib.auth.models import User
from main_app.models import *
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password


class GenreSerializer(serializers.ModelSerializer):

    class Meta:
        model = Genre
        fields = "__all__"


class ArtistSerializer(serializers.ModelSerializer):

    class Meta:
        model = Artist
        fields = "__all__"


class TrackSerializer(serializers.ModelSerializer):

    class Meta:
        model = Track
        fields = "__all__"


class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User
        fields = ["url", "username", "email", "last_login", "is_superuser", "is_staff"]


UserModel = get_user_model()


class UserRegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(
        write_only=True,
        required=True,
        # help_text='Leave empty if no change needed',
        style={"input_type": "password", "placeholder": "Password"}
    )

    class Meta:
        model = User
        fields = ("url", "username", "email", "first_name", "last_name", "password")

    def create(self, validated_data):
        validated_data["password"] = make_password(validated_data.get("password"))
        return super(UserRegisterSerializer, self).create(validated_data)

# help_text='Leave empty if no change needed',