import string

from django.contrib.auth.models import User
from django.core.exceptions import ValidationError

from users.models import Player

def usernameValidator(username):
    if User.objects.filter(username__iexact=username).first() is not None:
        raise ValidationError("Sorry, this username is already in use")

    if len(username) < 3 or len(username) > 16:
        raise ValidationError("Username must be between 3-16 characters long")

    charset = set(string.ascii_letters + string.digits + "_-")
    if not all(x in charset for x in username):
        raise ValidationError("Username may only contain normal letters, numbers and _-")