Exemplo n.º 1
0
    def validate(self, attrs):
        user = self.context['request'].user
        validated_data = super().validate(attrs)

        sms_token = validated_data.pop('sms_token', '')
        if sms_token:
            mobile = UpdateMobileSMSClaimSerializer.check_sms_token(
                sms_token)['mobile']
            UpdateMobileSMSClaimSerializer.clear_sms_token(sms_token)
            if User.valid_objects.filter(mobile=mobile).exclude(
                    id=user.id).exists():
                raise ValidationError({'mobile': ['existed']})
            validated_data['mobile'] = mobile

        email_token = validated_data.pop('email_token', '')
        if email_token:
            res = UpdateEmailEmailClaimSerializer.check_email_token(
                email_token)
            UpdateEmailEmailClaimSerializer.clear_email_token(email_token)
            email = res['email']
            username = res['username']

            if user.username != username:
                raise ValidationError({'email_token': ['invalid']})

            if User.valid_objects.filter(private_email=email).exclude(
                    id=user.id).exists():
                raise ValidationError({'email': ['existed']})

            validated_data['private_email'] = email

        return validated_data
Exemplo n.º 2
0
    def validate(self, attrs):
        validated_data = super().validate(attrs)
        old_mobile = SMSClaimSerializer.check_sms_token(validated_data.get('old_mobile_sms_token'))['mobile']
        new_mobile = UpdateMobileSMSClaimSerializer.check_sms_token(
            validated_data.get('new_mobile_sms_token'))['mobile']    # pylint: disable=line-too-long

        request = self.context.get("request")
        user = request.user

        if user.mobile != old_mobile:
            raise ValidationError({'old_mobile': 'invalid mobile'})

        if User.valid_objects.filter(mobile=new_mobile).exists():
            raise ValidationError({'new_mobile': 'has been used'})

        validated_data.update(new_mobile=new_mobile)

        return validated_data