def validate(self, attrs): key = attrs.get('key', '') if not key: raise ValidationError({'key': ['this field is required']}) invitation = Invitation.parse(key) if invitation is None: raise ValidationError({'key': ['invalid']}) if invitation.is_expired: raise ValidationError({'key': ['expired']}) if not invitation.invitee.mobile: raise ValidationError({'key': ['invalid']}) return { 'key': key, 'mobile': invitation.invitee.mobile, }
def get(self, request, *args, **kwargs): key = request.query_params.get('key', '') if not key: raise ValidationError({'key': ['this field is requied']}) invitation = Invitation.parse(key) if invitation is None: return Response({'key': ['invalid']}, status=status.HTTP_400_BAD_REQUEST) if invitation.is_expired: return Response({'key': ['expired']}, status=status.HTTP_400_BAD_REQUEST) user = invitation.invitee return Response({ # 'token': user.token, **UserWithPermSerializer(user).data, })
def validate(self, attrs): validated_data = super().validate(attrs) key = validated_data.pop('key') invitation = Invitation.parse(key) if not invitation: raise ValidationError({'key': ['invalid']}) user = invitation.invitee user_validated = False username = validated_data.get('username', '') if username and User.valid_objects.filter(username=username).exclude( pk=user.pk).exists(): raise ValidationError({'username': ['existed']}) sms_token = validated_data.pop('sms_token', '') if sms_token: mobile = UserActivateSMSClaimSerializer.check_sms_token( sms_token)['mobile'] UserActivateSMSClaimSerializer.clear_sms_token(sms_token) if mobile != user.mobile: raise ValidationError({'sms_token': ['invalid']}) user_validated = True email_token = validated_data.pop('email_token', '') if email_token: email = UserActivateEmailClaimSerializer.check_email_token( email_token)['email'] UserActivateEmailClaimSerializer.clear_email_token(email_token) if email != user.private_email: raise ValidationError({'email_token': ['invalid']}) user_validated = True if not user_validated: raise ValidationError( {'auth_token': ['must provide "email_token" or "sms_token"']}) validated_data.update(user=user) return validated_data