当前位置: 首页>>代码示例>>Python>>正文


Python User.get_by_id方法代码示例

本文整理汇总了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)
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:29,代码来源:tests.py

示例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}
开发者ID:Kolab-Education,项目名称:flask-ide,代码行数:11,代码来源:context_processors.py

示例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
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:58,代码来源:views.py

示例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
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:57,代码来源:views.py

示例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()
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:16,代码来源:tests.py

示例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
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:51,代码来源:security.py

示例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)
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:44,代码来源:tests.py

示例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
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:43,代码来源:views.py

示例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')
开发者ID:erhuabushuo,项目名称:flask-xxl,代码行数:8,代码来源:views.py

示例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
开发者ID:digitaldreamer,项目名称:fivesquare,代码行数:5,代码来源:models.py

示例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
开发者ID:sleepyjames,项目名称:sleepyjames-aeblog,代码行数:7,代码来源:backends.py


注:本文中的auth.models.User.get_by_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。