本文整理汇总了Python中model.User.find方法的典型用法代码示例。如果您正苦于以下问题:Python User.find方法的具体用法?Python User.find怎么用?Python User.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model.User
的用法示例。
在下文中一共展示了User.find方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def post(data):
json = request.json['accessRequest']
user = UserModel.find(email=json['email'])
if user is not None:
resp = jsonify({
'status': 409,
'message': 'Conflict.'
})
resp.status_code = 409
return resp
user = UserModel(
email=json['email'],
firstname=json['firstname'],
lastname=json['lastname'],
username=json['firstname'] + '.' + json['lastname'],
status='pending',
employee_id=json['employee_id'],
password=json['password']
)
if user.save():
user = user.find(username=user.username)
users = UserModel.find(role='administrator')
for u in users:
message = Message()
message.type = 'access-request'
message.to_user = u
resp = jsonify({
'accessRequest': {
'id': user.id
}
})
resp.status_code = 201
return resp
resp = jsonify({
'status': 400,
'message': 'Bad Request.'
})
resp.status_code = 400
return resp
示例2: cookie2user
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def cookie2user(cookie_str):
# cookie_str是空则返回
if not cookie_str:
return None
try:
# 通过'-'分割字符串
L = cookie_str.split('-')
# 如果不是3个元素的话,与我们当初构造sha1字符串时不符,返回None
if len(L) != 3:
return None
# 分别获取到用户id,过期时间和sha1字符串
uid, expires, sha1 = L
# 如果超时,返回None
if int(expires) < time.time():
return None
# 根据用户id查找库,对比有没有该用户
user = yield from User.find(uid)
# 没有该用户返回None
if user is None:
return None
# 根据查到的user的数据构造一个校验sha1字符串
s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY)
# 比较cookie里的sha1和校验sha1,一样的话,说明当前请求的用户是合法的
if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
logging.info('invalid sha1')
return None
user.passwd = '******'
# 返回合法的user
return user
except Exception as e:
logging.exception(e)
return None
示例3: cookie2user
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def cookie2user(cookie_str):
if not cookie_str:
return None
try:
logging.info('1111111')
L = cookie_str.split('-')
if len(L) != 3:
return None
uid, expires, sha1 = L
logging.info('22222222')
if int(expires) < time.time():
return None
user = yield from User.find(uid)
logging.info('33333333')
if user is None:
return None
s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY)
logging.info('444444444')
if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
logging.info('invalid sha1')
return None
logging.info('55555555')
user.passwd = '******'
return user
except Exception as e:
logging.exception(e)
return None
示例4: cookie2user
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def cookie2user(cookie_str):
'''
Parse cookie and load user if cookie is valid.
'''
if not cookie_str:
return None
try:
L = cookie_str.split('-')
if len(L) != 3:
return None
uid, expires, sha1 = L
if int(expires) < time.time():
return None
user = yield from User.find(uid)
if user is None:
return None
s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY)
if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
logging.info('invalid sha1')
return None
user.passwd = '******'
return user
except Exception as e:
logging.exception(e)
return None
示例5: __init__
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
class Single:
def __init__(self, client):
self.model = User(client, bcrypt)
def on_get(self, req, resp, user_id):
if authorize_as(req.auth, 'developer'):
resource = self.model.find(user_id)
if resource != None:
resp.body = dumps(resource)
else:
resp.status = HTTP_404
else:
raise HTTPUnauthorized('unautharized', 'unautharized')
def on_put(self, req, resp, user_id):
try:
if(authorize_as(req.auth, 'developer')):
is_dev = True
except:
is_player = authorize_as(req.auth, 'player')
is_dev = False
if not is_player:
raise HTTPUnauthorized('unautharized', 'unautharized')
body = loads(req.stream.read().decode('utf-8'))
resource = self.model.update(body, user_id, is_dev)
if resource.modified_count == 1:
resp.status = HTTP_204
else:
raise HTTPBadRequest('failed to update resource',
'a resource with id: ' + user_id + ' was not found')
示例6: put
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def put(self, id):
json = request.json['user']
print json
user = UserModel.find(id=id)
user.email = json['email']
user.role = json['role']
user.employee_id = json['employee_id']
user.status = json['status']
user.departments = []
for department in json['departments']:
user.departments.append(Department.find(id=department['id']))
user.save()
departments = []
for department in user.departments:
departments.append({
'id': department.id,
'parent_id': department.parent_id,
'name': department.name,
'description': department.description
})
work_orders = []
for work_order in current_user.work_orders:
work_orders.append({
'id': work_order.id,
'department': work_order.department_id,
'user': work_order.user_id,
'asset': work_order.asset_id
})
model = {
'user': {
'id': user.id,
'user_id': user.id,
'firstname': user.firstname,
'lastname': user.lastname,
'email': user.email,
'username': user.username,
'employee_id': user.employee_id,
'status': user.status,
'role': user.role,
'departments': departments,
'work_orders': work_orders
}
}
return jsonify(model)
示例7: cookie2user
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def cookie2user(cookie_str):
if not cookie_str:
return None
try:
L = cookie_str.split('-')
if len(L) != 3:
return None
uid, expires, sha1 = L
if int(expires) < time.time():
return None
user = yield from User.find(uid)
if user is None:
return None
s = '%s-%s-%s-%s' % (uid, user.passwd, expires, __COOKIE_KEY)
if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
return None
user.passwd = '******'
return user
except Exception as e:
return None
示例8: test_find
# 需要导入模块: from model import User [as 别名]
# 或者: from model.User import find [as 别名]
def test_find(loop):
yield from create_pool(loop,**kw)
user = yield from User.find('123')
print(user)