def validate(self, attrs): ''' 校验token,验重 ''' validated_data = super().validate(attrs) if not (validated_data.get('sms_token', '') or validated_data.get('email_token', '')): raise ValidationError({'auth_token': ['auth_token is required, like "sms_token" or "email_token"']}) username = validated_data['username'] if User.valid_objects.filter(username=username).exists(): raise ValidationError({'username': ['existed']}) if not username_valid(username): raise ValidationError({'username': ['invalid']}) sms_token = validated_data.get('sms_token', '') if sms_token: mobile = RegisterSMSClaimSerializer.check_sms_token(sms_token)['mobile'] RegisterSMSClaimSerializer.clear_sms_token(sms_token) if User.valid_objects.filter(mobile=mobile).exists(): raise ValidationError({'mobile': ['existed']}) validated_data['mobile'] = mobile email_token = validated_data.get('email_token', '') if email_token: private_email = RegisterEmailClaimSerializer.check_email_token(email_token)['email'] RegisterEmailClaimSerializer.clear_email_token(email_token) if User.valid_objects.filter(private_email=private_email).exists(): raise ValidationError({'private_email': ['existed']}) validated_data['private_email'] = private_email return validated_data
def validate_username(self, value): ''' 校验username唯一 ''' value = value.strip(' ') if not username_valid(value): raise ValidationError('invalid') exclude = {'pk': self.instance.pk} if self.instance else {} if self.Meta.model.valid_objects.filter(username=value).exclude(**exclude).exists(): raise ValidationError(['existed']) return value
def validate(self, attrs): ''' 校验token,验重 ''' validated_data = super().validate(attrs) if not validated_data.get('sms_token', ''): raise ValidationError({'auth_token': ['auth_token is required, like "sms_token"']}) username = validated_data['username'] if User.valid_objects.filter(username=username).exists(): raise ValidationError({'username': ['existed']}) if not username_valid(username): raise ValidationError({'username': ['invalid']}) sms_token = validated_data.get('sms_token', None) if sms_token: mobile = SMSClaimSerializer.check_sms_token(sms_token)['mobile'] SMSClaimSerializer.clear_sms_token(sms_token) validated_data['mobile'] = mobile return validated_data