def clean_username(self, username): """ Validates the username. You can hook into this if you want to (dynamically) restrict what usernames can be chosen. """ from django.contrib.auth.forms import UserCreationForm USERNAME_REGEX = UserCreationForm().fields['username'].regex if not USERNAME_REGEX.match(username): raise forms.ValidationError( _("Usernames can only contain " "letters, digits and @/./+/-/_.")) # TODO: Add regexp support to USERNAME_BLACKLIST username_blacklist_lower = [ ub.lower() for ub in app_settings.USERNAME_BLACKLIST ] if username.lower() in username_blacklist_lower: raise forms.ValidationError( _("Username can not be used. " "Please use other username.")) username_field = app_settings.USER_MODEL_USERNAME_FIELD assert username_field user_model = get_user_model() try: query = {username_field + '__iexact': username} user_model.objects.get(**query) except user_model.DoesNotExist: return username raise forms.ValidationError( _("This username is already taken. Please " "choose another."))
def clean_username(self, username): """ Validates the username. You can hook into this if you want to (dynamically) restrict what usernames can be chosen. """ from django.contrib.auth.forms import UserCreationForm USERNAME_REGEX = UserCreationForm().fields['username'].regex if not USERNAME_REGEX.match(username): raise forms.ValidationError(_("Usernames can only contain " "letters, digits and @/./+/-/_.")) # TODO: Add regexp support to USERNAME_BLACKLIST if username in app_settings.USERNAME_BLACKLIST: raise forms.ValidationError(_("Username can not be used. " "Please use other username.")) username_field = app_settings.USER_MODEL_USERNAME_FIELD assert username_field user_model = get_user_model() try: query = {username_field + '__iexact': username} user_model.objects.get(**query) except user_model.DoesNotExist: return username raise forms.ValidationError(_("This username is already taken. Please " "choose another."))
def clean_username(self, username): """ Validates the username. You can hook into this if you want to (dynamically) restrict what usernames can be chosen. """ from django.contrib.auth.forms import UserCreationForm USERNAME_REGEX = UserCreationForm().fields['username'].regex if not USERNAME_REGEX.match(username): raise forms.ValidationError(_("Usernames can only contain " "letters, digits and @/./+/-/_.")) # TODO: Add regexp support to USERNAME_BLACKLIST if username in app_settings.USERNAME_BLACKLIST: raise forms.ValidationError(_("Username can not be used. " "Please use other username.")) return username
def validate_username(self, value): """ Validates the username. You can hook into this if you want to (dynamically) restrict what usernames can be chosen. """ username = value.strip() from django.contrib.auth.forms import UserCreationForm USERNAME_REGEX = UserCreationForm().fields['username'].regex if not USERNAME_REGEX.match(username): raise serializers.ValidationError(_("Usernames can only contain letters, digits and @/./+/-/_.")) # TODO: Add regexp support to USERNAME_BLACKLIST if username in settings.USERNAME_BLACKLIST: raise serializers.ValidationError(_("Username can not be used. " "Please use another username.")) if UserModel.query(UserModel.username==username).get(): raise serializers.ValidationError(_("This username is already taken. Please choose another.")) value = username return value