本文整理汇总了Python中auth.models.User.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python User.get_by_id方法的具体用法?Python User.get_by_id怎么用?Python User.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类auth.models.User
的用法示例。
在下文中一共展示了User.get_by_id方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_user_management
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def test_user_management(self):
"""
test user creation and deletion
"""
# creation
user = User.create('[email protected]', 'world')
self.assertTrue(user)
# retrieve
self.assertTrue(User.get_by_id(user.id))
self.assertTrue(User.get_by_email(user.email))
# duplicated users
self.assertFalse(User.create('[email protected]', 'world'))
try:
user2 = User(email='[email protected]', password='world')
user2.save()
except UserSaveError:
pass
else:
self.assertFalse('The user wrongly saved')
# deletion
self.assertTrue(user.delete())
user4 = User.get_by_email('[email protected]')
self.assertFalse(user4)
示例2: user_context
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def user_context():
if 'email' in session:
if 'user_id' in session:
from auth.models import User
return {
'user':User.get_by_id(session.get('user_id',None)),
'email':session.get('email',None)
}
return {'user':None}
示例3: user_password_put
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def user_password_put(request):
"""
Update user's password
errors
======
* status 400 - if failed to update user
* status 404 - if failed to find user
returns
=======
::
{
'status': 'success',
'message': 'user password updated'
}
"""
user_id = request.matchdict['user_id']
user = User.get_by_id(user_id)
if user:
# check for fields to update
user.password = request.validated['password']
# save
if user.save():
logger.debug('user:{} updated'.format(user_id))
response_body = json.dumps({
'status': 'success',
'message': 'user password updated'
})
else:
logger.debug('failed to save user:{} password'.format(user_id))
request.response.status_int = 400
response_body = json.dumps({
'status': 'error',
'message': 'failed to update user'
})
else:
logger.debug('failed to update user:{} password, user not found'.format(user_id))
request.response.status_int = 404
response_body = json.dumps({
'status': 'error',
'message': 'could not find user'
})
request.response.body = response_body
request.response.content_type = 'application/json'
return request.response
示例4: user_put
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def user_put(request):
"""
Update user
errors
======
* status 400 - if failed to update user
* status 404 - if user isn't found
returns
=======
::
{
'status': 'success',
'message': 'user updated'
}
"""
user_id = request.matchdict['user_id']
user = User.get_by_id(user_id)
if user:
user.active = request.validated['active']
user.email = request.validated['email']
# save
if user.save():
logger.debug('user:{} updated'.format(user_id))
response_body = json.dumps({
'status': 'success',
'message': 'user updated'
})
else:
logger.debug('failed to save user:{}'.format(user_id))
request.response.status_int = 400
response_body = json.dumps({
'status': 'error',
'message': 'failed to update user'
})
else:
logger.debug('failed to update user:{} user not found'.format(user_id))
request.response.status_int = 404
response_body = json.dumps({
'status': 'error',
'message': 'could not find user'
})
request.response.body = response_body
request.response.content_type = 'application/json'
return request.response
示例5: password
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def password(self, user):
# password
self.assertTrue(user.authenticate('hello'))
user.password = 'world'
user.save()
# check updating password
user2 = User.get_by_id(user.id)
self.assertFalse(user2.authenticate('hello'))
self.assertTrue(user2.authenticate('world'))
# reset password
user.password = 'hello'
user.save()
示例6: validate_access_token
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def validate_access_token(access_token):
"""
This request requires validation. To get an access token use the ``/authenticate`` endpoint.
"""
"""
Check to see if the access_token is valid
The access token will invalidate if the user changes their password.
parameters
==========
* access_token - a token created with create_access_token
returns
=======
returns True or False depending on if the access_token is valid
"""
from settings import config
validated = False
try:
decoded = jwt.decode(access_token, config.get('pepper', ''), algorithms=['HS256'])
except jwt.DecodeError:
logger.debug('jwt DecodeError')
else:
now = datetime.utcnow()
then = datetime.fromtimestamp(decoded['iat'])
age = now - then
user = User.get_by_id(decoded['user_id'])
# TODO: make the age configurable
# for now the access_token is valid for 5 hours
if age.seconds > 60 * 60 * 5:
logger.debug('stale access token, timestamp expired')
elif user and decoded['password'] == user.password:
validated = {
'user': user,
}
logger.debug('Valid access token for user:{}'.format(user.id))
else:
logger.debug('access token failed to validate')
return validated
示例7: test_user
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def test_user(self):
response = self.testapp.post_json('/api/v1/users', {}, status=400)
# create new user
payload = {
'email': '[email protected]',
'password': 'hello',
}
response = self.testapp.post_json('/api/v1/users', payload, status=200)
self.assertTrue(response.json['id'])
# check duplicate
response = self.testapp.post_json('/api/v1/users', payload, status=400)
# check database
user = User.get_by_email('[email protected]')
self.assertTrue(user)
self.created.append(user)
# get user
endpoint = '/api/v1/users/{}'.format(user.id)
response = self.testapp.get(endpoint, status=200)
self.assertTrue(response.json['id'])
# update user
payload = {
'active': True,
'email': '[email protected]',
}
endpoint = '/api/v1/users/{}'.format(user.id)
response = self.testapp.put_json(endpoint, payload, status=200)
user = User.get_by_id(user.id)
self.assertTrue(user.active)
# change password
payload = {
'password': 'world',
}
endpoint = '/api/v1/users/{}/password'.format(user.id)
response = self.testapp.put_json(endpoint, payload, status=200)
user = User.authenticate_user('[email protected]', 'world')
self.assertTrue(user)
示例8: user_get
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def user_get(request):
"""
get user
errors
======
* status 404 - if the user couldn't be found
returns
=======
::
{
"modified": "2015-08-10T00:20:13.708000",
"active": "false",
"id": "55c7ee3dfad9b43993d7190e",
"email": "[email protected]",
"created": "2015-08-10T00:20:12.882000"
}
"""
user_id = request.matchdict['user_id']
user = User.get_by_id(user_id)
if user:
logger.debug('got user:{}'.format(user_id))
response_body = user.json
else:
logger.debug('could not find user:{}'.format(user_id))
request.response.status_int = 404
response_body = json.dumps({
'status': 'error',
'message': 'user does not exist'
})
request.response.body = response_body
request.response.content_type = 'application/json'
return request.response
示例9: get
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def get(self):
from auth.models import User
if 'user_id' in session:
user = User.get_by_id(session['user_id'])
logout_user(user)
return self.redirect('core.index')
示例10: user
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def user(self):
user = User.get_by_id(self.data['user_id'])
return user
示例11: get_user
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import get_by_id [as 别名]
def get_user(self, user_id):
try:
return User.get_by_id(user_id)
except:
return None