def clean_new_password2(self): password1 = self.cleaned_data['new_password1'] password2 = self.cleaned_data['new_password2'] if password1 and password2: if password1 != password2: raise forms.ValidationError('Contraseñas no coinciden') try: password_validation.validate_password(password2, self.user) except password_validation.ValidationError as errores: mensajes = [] for m in errores.messages: if m == 'This password is too short. It must contain at least 8 characters.': mensajes.append( 'Contraseña muy corta, debe contener más de 7 caracteres' ) if m == 'This password is too common.': mensajes.append('Contraseña muy común') if m == 'This password is entirely numeric.': mensajes.append( 'Contraseña no puede contener solo números') if m.startswith("The password is too similar"): mensajes.append( 'Contraseña muy similar a los datos del usuario') raise forms.ValidationError(mensajes) return password2
def clean_email(self): email = self.cleaned_data['email'] if self.instance.pk is not None and self.instance.email == email: raise forms.ValidationError('This is your current email address!') if CustomUser.objects.filter(email=email).exists(): raise forms.ValidationError(f'Email address {email} already exists!') return email
def clean_url(self): url = self.cleaned_data['url'] if self.instance: concurso = Concurso.objects.filter(url=url) if len(concurso) > 0 and concurso[0].id != self.instance.id: raise forms.ValidationError( 'La url ya está ha sido tomada por otro concurso') else: if Concurso.objects.filter(url=url): raise forms.ValidationError( 'La url ya está ha sido tomada por otro concurso') return url
def authenticate(self, request, username, password=None, **kwargs): # 重写authenticate 方法, 跳过Django自带的权限验证 try: user = User.objects.get(username=username) except User.DoesNotExist: raise forms.ValidationError('用户名或密码错误') else: if user.check_password(password) and self.user_can_authenticate( user): return user else: raise forms.ValidationError("用户名或密码错误")
def clean_phone(self): # 从cleaned_data中取出我们需要的数据 phone = self.cleaned_data.get('phone') if not phone: raise forms.ValidationError('您并未输入电话号码') if not len(phone) == 11: # 号码长度不正确 raise forms.ValidationError("该手机号码位数不正确") phone_re = "1[3,4,5,7,8]\d{9}" if not re.match(phone_re, phone): # 号码不是合法号码 raise forms.ValidationError("您输入的手机号码不合法") return phone
def clean(self): cleaned_data = super(RegisterForm, self).clean() telephone = cleaned_data.get('telephone') password = cleaned_data.get('password') re_password = cleaned_data.get('re_password') if password != re_password: raise forms.ValidationError('您输入的两次密码不一致') exists = User.objects.filter(telephone=telephone).exists() print(exists) if exists: raise forms.ValidationError('您的手机号已经被注册') return cleaned_data
def clean(self, *args, **kwargs): password = self.cleaned_data.get('password') password1 = self.cleaned_data.get('password1') email = self.cleaned_data.get('email') if password and password1 and password != password1: raise forms.ValidationError( self.errors_messages['password_mismatch'], code='password_mismatch') if User.objects.filter(email=email).exists(): raise forms.ValidationError( 'Please use another Email,That is already taken') return super(UserRegisterForm, self).clean(*args, **kwargs)
def clean_password2(self): """Comprueba que password y password2 sean iguales""" password = self.cleaned_data['password'] password2 = self.cleaned_data['password2'] if password != password2: raise forms.ValidationError('Las claves no coinciden.') return password2
def clean_email(self): """Comprueba que no exista un email igual en la bd""" email = self.cleaned_data['email'] if User.objects.filter(email=email): raise forms.ValidationError( 'Ya existe un email igual al registrado.') return email
def clean_age(self): age = self.cleaned_data['age'] if age < 18: raise forms.ValidationError( self.error_messages['Возраст не соответствует!'], code='Возраст не соответствует!') return age
def clean_email(self): email = self.cleaned_data['email'] if email and ShopUser.objects.filter(email=email).exists(): raise forms.ValidationError( "Dude! that mail has been already registered") return email
def clean_email(self): email = self.cleaned_data.get('email') username = self.cleaned_data.get('username') if email and User.objects.filter(email=email).exclude( username=username).exists(): raise forms.ValidationError(u'Email addresses must be unique.') return email
def clean_email(self): email = self.cleaned_data.get('email') if not User.objects.filter(email__iexact=email): # 该邮箱未注册 raise forms.ValidationError("该邮箱未注册") # 必须返回email, 不然email就会变为None return email
def clean_email(self): email = self.cleaned_data.get('email') if User.objects.filter(email=email).count() > 0: raise forms.ValidationError( self.error_messages['same_email'], code='same_email', ) if not email: raise forms.ValidationError( self.error_messages['required_email'], code='required_email', ) return email
def clean_username(self): username = self.cleaned_data['username'] if User.objects.exclude(pk=self.instance.id).filter( username=username).exists(): raise forms.ValidationError(u'Usuario "%s" ya esta en uso.' % username) return username
def clean(self): cleaned_data = super(LoginForm, self).clean() if not self.errors: user = authenticate(username=cleaned_data['username'], password=cleaned_data['password']) if user is None: raise forms.ValidationError(u'Имя пользователя и пароль не подходят') self.user = user return cleaned_data
def clean_fecha_terminacion(self): fecha_inicio = self.cleaned_data['fecha_inicio'] fecha_terminacion = self.cleaned_data['fecha_terminacion'] if fecha_terminacion < fecha_inicio: raise forms.ValidationError( 'La fecha de inicio debe ser anterior a la fecha de terminacion' ) return fecha_inicio
def clean(self, *args, **kwargs): email = self.cleaned_data.get("email") if not User.objects.filter(email=email).exists(): raise forms.ValidationError("This Email isn't Found") return super(ResetPasswordForm, self).clean(*args, **kwargs)
def clean(self): cleaned_data = super(login_form, self).clean() if not self.errors: user = authenticate(username=cleaned_data['username'], password=cleaned_data['password']) if user is None: raise forms.ValidationError(u'Неправильный логин или пароль') self.user = user return cleaned_data
def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError( self.error_messages['password_mismatch'], code='password_mismatch', ) return password2
def clean(self, *args , **kwargs): username = self.cleaned_data.get("username") password = self.cleaned_data.get('password') try: user = User.objects.get(username=username) if not user.is_active: raise forms.ValidationError("This Account isn't Active please Activate your account to login") except User.DoesNotExist: raise forms.ValidationError(self.error_message['username'] , params={'username': username}) if not user.check_password(password): raise forms.ValidationError(self.error_message['password'] , code=self.error_message['password']) return super(UserLoginForm, self).clean(*args , **kwargs)
def check_username(self): username = self.cleaned_data.get('username') if User.objects.filter(username=username).count() > 0: raise forms.ValidationError( self.error_messages['same_username'], code='same_username' ) return username
def clean_email(self): email = self.cleaned_data['email'] busqueda = User.objects.filter(email=email) if busqueda: user = len(busqueda) if user > 1: raise forms.ValidationError('Correo ya ha sido registrado') else: return email return email
def clean_username(self): username = self.cleaned_data["username"] try: VmmasterUser._default_manager.get(username=username) except VmmasterUser.DoesNotExist: return username raise forms.ValidationError( self.error_messages['duplicate_username'], code='duplicate_username', )
def clean(self): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') # 后端验证是否有数据 if not (username and password): raise forms.ValidationError('用户名或密码为空') # 调用方法, 验证密码, 成功返回用户 self.user_cache = self.authenticate(self.request, username=username, password=password) return self.cleaned_data
def clean(self, *args, **kwargs): password = self.cleaned_data.get('password') password1 = self.cleaned_data.get('password1') if password and password1 and password != password1: raise forms.ValidationError( self.errors_messages['password_mismatch'], code='password_mismatch') return super(NewPasswordForm, self).clean(*args, **kwargs)
def clean(self): cleaned_data = super().clean() try: create_connection(cleaned_data.get('db_type'), cleaned_data.get('username'), cleaned_data.get('password'), cleaned_data.get('host'), cleaned_data.get('port'), cleaned_data.get('db_instance')) except: raise forms.ValidationError( ugettext('Can not connect to database with settings, please recheck settings again.'), code='invalid' )
def confirm_login_allowed(self, user): """ Controls whether the given User may log in. This is a policy setting, independent of end-user authentication. This default behavior is to allow login by active users, and reject login by inactive users. If the given user cannot log in, this method should raise a ``forms.ValidationError``. If the given user may log in, this method should return None. """ if not user.is_active: raise forms.ValidationError( self.error_messages['inactive'], code='inactive', )
def clean_username(self): username = self.cleaned_data['username'] if User.objects.filter(username=username): raise forms.ValidationError('Este usuario ya existe') return username
def clean_username(self): """Comprueba que no exista un username igual en la db""" username = self.cleaned_data['username'] if User.objects.filter(username=username): raise forms.ValidationError('Nombre de usuario ya registrado.') return username