from django.contrib.auth import password_validation
from rest_framework import exceptions, serializers

from lego.apps.users.models import User
from lego.utils.functions import verify_captcha


class RegistrationSerializer(serializers.ModelSerializer):
    captcha_response = serializers.CharField(required=True)

    def validate_captcha_response(self, captcha_response):
        if not verify_captcha(captcha_response):
            raise exceptions.ValidationError("invalid_captcha")
        return captcha_response

    class Meta:
        model = User
        fields = ("email", "captcha_response")


class RegistrationConfirmationSerializer(serializers.ModelSerializer):

    password = serializers.CharField(required=True, write_only=True)

    def validate_username(self, username):
        username_exists = User.objects.filter(username__iexact=username).exists()
        if username_exists:
            raise exceptions.ValidationError("Username exists")
        return username

    def validate_password(self, password):
        password_validation.validate_password(password)
        return password

    class Meta:
        model = User
        fields = (
            "username",
            "first_name",
            "last_name",
            "gender",
            "password",
            "allergies",
        )

lego/apps/users/serializers/registration.py
from django.contrib.auth import password_validation
from rest_framework import exceptions, serializers

from lego.apps.users.models import User
from lego.utils.functions import verify_captcha


class RegistrationSerializer(serializers.ModelSerializer):
    captcha_response = serializers.CharField(required=True)

    def validate_captcha_response(self, captcha_response):
        if not verify_captcha(captcha_response):
            raise exceptions.ValidationError("invalid_captcha")
        return captcha_response

    class Meta:
        model = User
        fields = ("email", "captcha_response")


class RegistrationConfirmationSerializer(serializers.ModelSerializer):

    password = serializers.CharField(required=True, write_only=True)

    def validate_username(self, username):
        username_exists = User.objects.filter(username__iexact=username).exists()
        if username_exists:
            raise exceptions.ValidationError("Username exists")
        return username

    def validate_password(self, password):
        password_validation.validate_password(password)
        return password

    class Meta:
        model = User
        fields = (
            "username",
            "first_name",
            "last_name",
            "gender",
            "password",
            "allergies",
            "phone_number",
        )

lego/apps/users/serializers/registration.py
diff --git a/lego/apps/users/serializers/registration.py b/lego/apps/users/serializers/registration.py
index d6c6647db..a12391464 100644
--- a/lego/apps/users/serializers/registration.py
+++ b/lego/apps/users/serializers/registration.py
@@ -41,4 +41,5 @@ class Meta:
             "gender",
             "password",
             "allergies",
+            "phone_number",
         )

Phone number not saved from registration form
When creating a new user, LEGO ignores the phone number inserted into the registration form.
