使用 couchdb-python 管理 _users 中的用户
Managing users in _users with couchdb-python
我正在尝试使用 couchdb-python
从数据库 _users
中存储和检索用户。我是 couchdb
.
的新手
我用 couchdb 文档 couchdb.mapping.Document
映射 python class 用户,如下所示:
import couchdb.mapping as cmap
class User(cmap.Document):
name = cmap.TextField()
password = cmap.TextField()
type = 'user'
roles = {}
但这不起作用。我得到 doc.type must be user
ServerError
所以我声明类型的方式可能不正确。
我应该如何构建我的 class 以便与 _users
数据库一起使用?
在 IRC 上 #couchdb
频道的一些提示后,我得出了这个 class(这可能比我问的要多...)
import couchdb.mapping as cmap
class User(cmap.Document):
""" Class used to map a user document inside the '_users' database to a
Python object.
For better understanding check https://wiki.apache.org
/couchdb/Security_Features_Overview
Args:
name: Name of the user
password: password of the user in plain text
type: (Must be) 'user'
roles: Roles for the users
"""
def __init__(self, **values):
# For user in the _users database id must be org.couchdb.user:<name>
# Here we're auto-generating it.
if 'name' in values:
_id = 'org.couchdb.user:{}'.format(values['name'])
cmap.Document.__init__(self, id=_id, **values)
type = cmap.TextField(default='user')
name = cmap.TextField()
password = cmap.TextField()
roles = cmap.ListField(cmap.TextField())
@cmap.ViewField.define('users')
def default(doc):
if doc['name']:
yield doc['name'], doc
这应该有效:
db = couchdb.server()['_users']
alice = User(name="Alice", password="strongpassword")
alice.store(db)
我正在尝试使用 couchdb-python
从数据库 _users
中存储和检索用户。我是 couchdb
.
我用 couchdb 文档 couchdb.mapping.Document
映射 python class 用户,如下所示:
import couchdb.mapping as cmap
class User(cmap.Document):
name = cmap.TextField()
password = cmap.TextField()
type = 'user'
roles = {}
但这不起作用。我得到 doc.type must be user
ServerError
所以我声明类型的方式可能不正确。
我应该如何构建我的 class 以便与 _users
数据库一起使用?
在 IRC 上 #couchdb
频道的一些提示后,我得出了这个 class(这可能比我问的要多...)
import couchdb.mapping as cmap
class User(cmap.Document):
""" Class used to map a user document inside the '_users' database to a
Python object.
For better understanding check https://wiki.apache.org
/couchdb/Security_Features_Overview
Args:
name: Name of the user
password: password of the user in plain text
type: (Must be) 'user'
roles: Roles for the users
"""
def __init__(self, **values):
# For user in the _users database id must be org.couchdb.user:<name>
# Here we're auto-generating it.
if 'name' in values:
_id = 'org.couchdb.user:{}'.format(values['name'])
cmap.Document.__init__(self, id=_id, **values)
type = cmap.TextField(default='user')
name = cmap.TextField()
password = cmap.TextField()
roles = cmap.ListField(cmap.TextField())
@cmap.ViewField.define('users')
def default(doc):
if doc['name']:
yield doc['name'], doc
这应该有效:
db = couchdb.server()['_users']
alice = User(name="Alice", password="strongpassword")
alice.store(db)