def clean_username(self): username = self.cleaned_data['username'] if 'username' in self.changed_data: key = 'username_attempts_%s' % self.instance.username attempt = cache.get(key) or 0 if attempt < 10: try: reserved_validator = ReservedNameValidator( reserved_names=DEFAULT_RESERVED_NAMES) reserved_validator(username) validate_confusables(username) except forms.ValidationError as v: self.add_error('username', v) attempt += 1 cache.set(key, attempt, 300) else: raise forms.ValidationError( 'Maximum username change attempts exceeded') return username
def test_confusables_validator(self): """ Test the confusable-username validator standalone. """ for dangerous_value in ( "p\u0430yp\u0430l", "g\u043e\u043egle", "\u03c1ay\u03c1al", ): with self.assertRaises(ValidationError): validators.validate_confusables(dangerous_value) for safe_value in ( "paypal", "google", "root", "admin", "\u041f\u0451\u0442\u0440", "\u5c71\u672c", 3, ): validators.validate_confusables(safe_value)
def test_confusables_validator(self): """ Test the confusable-username validator standalone. """ for dangerous_value in ( u'p\u0430yp\u0430l', u'g\u043e\u043egle', u'\u03c1ay\u03c1al', ): with self.assertRaises(ValidationError): validators.validate_confusables(dangerous_value) for safe_value in ( u'paypal', u'google', u'root', u'admin', u'\u041f\u0451\u0442\u0440', u'\u5c71\u672c', 3, ): validators.validate_confusables(safe_value)