# Path: user_management/utils/validators.py
# def validate_password_strength(value):
#     """
#     Passwords should be tough.
# 
#     That means they should use:
#     - mixed case letters,
#     - numbers,
#     - (optionally) ascii symbols and spaces.
# 
#     The (contrversial?) decision to limit the passwords to ASCII only
#     is for the sake of:
#     - simplicity (no need to normalise UTF-8 input)
#     - security (some character sets are visible as typed into password fields)
# 
#     TODO: In future, it may be worth considering:
#     - rejecting common passwords. (Where can we get a list?)
#     - rejecting passwords with too many repeated characters.
# 
#     It should be noted that no restriction has been placed on the length of the
#     password here, as that can easily be achieved with use of the min_length
#     attribute of a form/serializer field.
#     """
#     used_chars = set(value)
#     good_chars = set(ascii_letters + digits + punctuation + ' ')
#     required_sets = (ascii_uppercase, ascii_lowercase, digits)
# 
#     if not used_chars.issubset(good_chars):
#         raise ValidationError(too_fancy)
# 
#     for required in required_sets:
#         if not used_chars.intersection(required):
#             raise ValidationError(too_simple)


from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, validators
from user_management.utils.validators import validate_password_strength


unique_email_validator = UniqueEmailValidator(
    queryset=User.objects.all(),
    message=_('That email address has already been registered.'),
)


class ValidateEmailMixin(object):
    def validate_email(self, value):
        return value.lower()


class EmailSerializerBase(serializers.Serializer):
    """Serializer defining a read-only `email` field."""
    email = serializers.EmailField(max_length=511, label=_('Email address'))

    class Meta:
        fields = ('email',)


class RegistrationSerializer(ValidateEmailMixin, serializers.ModelSerializer):
    email = serializers.EmailField(
        label=_('Email address'),
        validators=[unique_email_validator],
    )
    password = serializers.CharField(
        write_only=True,
        min_length=8,
        label=_('Password'),

# Path: user_management/utils/validators.py
# def validate_password_strength(value):
#     """
#     Passwords should be tough.
# 
#     That means they should use:
#     - mixed case letters,
#     - numbers,
#     - (optionally) ascii symbols and spaces.
# 
#     The (contrversial?) decision to limit the passwords to ASCII only
#     is for the sake of:
#     - simplicity (no need to normalise UTF-8 input)
#     - security (some character sets are visible as typed into password fields)
# 
#     TODO: In future, it may be worth considering:
#     - rejecting common passwords. (Where can we get a list?)
#     - rejecting passwords with too many repeated characters.
# 
#     It should be noted that no restriction has been placed on the length of the
#     password here, as that can easily be achieved with use of the min_length
#     attribute of a form/serializer field.
#     """
#     used_chars = set(value)
#     good_chars = set(ascii_letters + digits + punctuation + ' ')
#     required_sets = (ascii_uppercase, ascii_lowercase, digits)
# 
#     if not used_chars.issubset(good_chars):
#         raise ValidationError(too_fancy)
# 
#     for required in required_sets:
#         if not used_chars.intersection(required):
#             raise ValidationError(too_simple)


# Path: user_management/api/serializers.py
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, validators
from user_management.utils.validators import validate_password_strength



unique_email_validator = UniqueEmailValidator(
    queryset=User.objects.all(),
    message=_('That email address has already been registered.'),
)


class ValidateEmailMixin(object):
    def validate_email(self, value):
        return value.lower()


class EmailSerializerBase(serializers.Serializer):
    """Serializer defining a read-only `email` field."""
    email = serializers.EmailField(max_length=511, label=_('Email address'))

    class Meta:
        fields = ('email',)


class RegistrationSerializer(ValidateEmailMixin, serializers.ModelSerializer):
    email = serializers.EmailField(
        label=_('Email address'),
        validators=[unique_email_validator],
    )
    password = serializers.CharField(
        write_only=True,
        min_length=8,
        label=_('Password'),

        validators=[validate_password_strength],