def clean_username(self): """Checks if the user exists and that the username is only -a-zA-Z""" username = self.cleaned_data.get('username') # Check that the username is alphanumeric and dash only # and doesnt start with a dash if not checkutils.username_correct(username): # We can safely return only this error because is not a valid # username so doesn't matter if exists or not the username raise forms.ValidationError(_(u"Username may only contain alphanumeric characters or dashes and cannot begin with a dash")) try: User.objects.get(username=username) except ObjectDoesNotExist: return username # If no exception then wrong user raise forms.ValidationError(_(u"This username is already taken"))
def test_correct_username(self): usernames = { "slok69": True, "sharem": True, "-wrong": False, "right-": True, "rig-ht": True, "wro_ng": False, "wr0ng_": False, "r1ght-": True, "_wrong": False, "w?rong": False, "wr@ong": False, "wr#ng": False, "wrong+": False, "wro/g": False, "wro ng": False, } for username, result in usernames.items(): self.assertEquals(result, checkutils.username_correct(username))