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


Python User.find方法代码示例

本文整理汇总了Python中models.User.find方法的典型用法代码示例。如果您正苦于以下问题:Python User.find方法的具体用法?Python User.find怎么用?Python User.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.User的用法示例。


在下文中一共展示了User.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.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
开发者ID:hellozmz,项目名称:awesome_python3_webapp,代码行数:27,代码来源:handlers.py

示例2: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
    '''
    Parse cookie and load user if cookie is valid
    '''
    if not cookie_str:
        logging.info("cookie_str is null!")
        return None
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            logging.info("cookie form is error!")
            return None
        uid, expires, sha1 = L
        if int(expires) < time.time():
            logging.info("expires is error!")
            return None
        
        user = yield from User.find(uid)
        
        if user is None:
            logging.info("user is not exist!")
            return None
        s = "%s-%s-%s-%s" %(uid, user.passwd, expires, _COOKIE_KEY)
        if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
            logging.info("invaild sha1!")
            return None
        user.passwd = "******"
        return user
    except Exception as e:
        logging.exception(e)
        return None
开发者ID:Parlefan,项目名称:Python3-BlogWeb,代码行数:33,代码来源:handlers.py

示例3: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.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
开发者ID:wp3xpp,项目名称:freemind-python,代码行数:27,代码来源:handlers.py

示例4: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.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, sha1s = l
        # 若失效时间小于当前时间,cookie失效
        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)
        # 与浏览器cookie中的MD5进行比较。MD5 ?
        if sha1s != 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
开发者ID:lovelywxd,项目名称:blogs-web-python,代码行数:27,代码来源:handlers.py

示例5: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.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' % (user.id, user.passwd, expires, _COOKIE_KEY)
        if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
            return None

        user.passwd = "*****"
        logging.info('cooke2user %s' % user.email)
        return user
    except Exception as e:
        logging.exception(e)
        return None
开发者ID:pengxiaojun,项目名称:python_utils,代码行数:29,代码来源:handlers.py

示例6: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
    '''Parse cookie and load user if cookie is valid'''
    # cookie_str就是user2cookie函数的返回值
    if not cookie_str:
        return None
    try:
        # 解密是加密的逆向过程,所以先通过’-‘拆分cookie,得到加密的字符串,用户id,失效时间
        L = cookie_str.spilt('-')
        if len(L) != 3:
            return None
        uid, expires, sha1 = L
        if int(expires) < time.time(): # 失效时间小于当前时间表明cookie已失效
            return None
        user = yield from User.find(uid) # 拆分的道德id在数据库中查找用户信息
        if user is None:
            return None
        # 利用用户id,加密后的密码,失效时间,加上cookie密钥,组合成待加密的原始字符串
        # 再对其进行加密,与从cookie分解得到的sha1进行比较.若相等,则该cookie合法
        s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY)
        if sha1 != hashlib.sha1(s.encode('utf-8')):
            logging.info('invalid sha1')
            return None
        # 以上就完成了cookie的验证
        # 验证cookie就是为了验证用户是否仍登录,从而使用户不必重新登录
        # 因此返回用户信息即可
        user.passwd = '*****'
        return user
    except Exception as e:
        logging.exception(e)
    return None
开发者ID:Patrickctyyx,项目名称:superawesome-python3-webapp,代码行数:32,代码来源:handlers.py

示例7: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
    '''Parse cookie and load user if cookie is valid'''
    # cookie_str就是user2cookie函数的返回值
    if not cookie_str:
        return None
    try:
        # 拆分cookie,得到用户id,失效时间,以及加密字符串
        L = cookie_str.split("-") # 返回一个str的list
        if len(L) != 3: # cookie由3部分组成
            return None
        uid, expires, sha1 = L
        if int(expires) < time.time(): # 时间是浮点表示的时间戳,一直在增大.因此失效时间小于当前时间,说明cookie已失效
            return None
        user = yield from User.find(uid)  # 在拆分得到的id在数据库中查找用户信息
        if user is None:
            return None
        # 利用用户id,加密后的密码,失效时间,加上cookie密钥,组合成待加密的原始字符串
        # 再对其进行加密,与从cookie分解得到的sha1进行比较.若相等,则该cookie合法
        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
        # 验证cookie,就是为了验证当前用户是否仍登录着,从而使用户不必重新登录
        # 因此,返回用户信息即可
        user.passwd = "*****"
        return user
    except Exception as e:
        logging.exception(e)
    return None
开发者ID:Joe-Blake,项目名称:Blog,代码行数:31,代码来源:handlers.py

示例8: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.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
        # 根据用户id查找库,对比有没有该用户
        user = yield from User.find(uid)
        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
开发者ID:COREENE,项目名称:python,代码行数:31,代码来源:handlers.py

示例9: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
    if not cookie_str:
        return None
    #cookie的格式为id-expires-sha1,若正确,将其拆分开
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            return None
        uid, expires, sha1 = L
        #判断cookie是否过期
        if int(expires) < time.time():
            return None
        user = yield from User.find(uid)
        if user is None:
            return None
        #将原始字符串加密,与从cookie中获取的加密字符串比较,若不相等,则cookie是伪造的
        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
开发者ID:Tdpiamix,项目名称:webapp-blog,代码行数:28,代码来源:handlers.py

示例10: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
	if not cookie_str:
		return None
	try:
		L = cookie_str.split('-')
		if len(L) != 3:
			# 如果不是3个元素的话,与我们当初构造sha1字符串时不符,返回None
			return None
		uid, expires, sha1 = L
		# 分别获取到用户id,过期时间和sha1字符串
		if int(expires) < time.time():
			# 如果超时(超过一天),返回None
			return None
		user = yield from User.find(uid)
		# 根据用户id(id为primary key)查找库,对比有没有该用户
		if user is None:
			return None
		s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY)
		# 根据查到的user的数据构造一个校验sha1字符串
		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
开发者ID:JasonZhao001,项目名称:awesome-python3-webapp,代码行数:30,代码来源:handlers.py

示例11: get_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def get_user( id ):
    user = yield from User.find( id )
    push_options = yield from PushOption.findAll( 'user_id=?', [id], limit = 1 )

    return {
        '__template__' : 'push_options.html',
        'push_option' : push_options[0]
    }
开发者ID:finalsatan,项目名称:PersonalBlog,代码行数:10,代码来源:handlers.py

示例12: get_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def get_user(id):
    user = yield from User.find(id)
    blogs = yield from Blog.findAll('user_id=?', [id], orderBy='created_at desc')
    return {
        '__template__': 'user.html',
        'user': user,
        'blogs': blogs
    }
开发者ID:chenyanglong,项目名称:ideal,代码行数:10,代码来源:handlers.py

示例13: route_weibo_index

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def route_weibo_index(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_header(headers)
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find(user_id)
    if user is None:
        return error(request)
    # 找到 user 发布的所有 weibo
    weibos = Weibo.find_all(user_id=user.id)

    # 任一 user 访问任一 index
    current_username = current_user(request)
    u = User.find_by(username=current_username)
    if u is None:
        return redirect('/login')

    def weibo_tag(weibo):
        comment_list = Comment.find_all(weibo_id=weibo.id)
        comments = '<br>'.join([c.content for c in comment_list])
        # format 函数的字典用法
        # 注意 u.id 是 current_user
        # user.username 是博主
        w = {
            'id': weibo.id,
            'user_id': u.id,
            'content': weibo.content,
            'username': user.username,
            'time': weibo.created_time,
            'comments': comments,
        }
        # 手动处理 weibos 这个 list
        # 把每个 weibo 以 <p> 的形式展现在页面
        return """
            <p>{content} from {username}@{time}
                <a href="/weibo/delete?id={id}">删除</a>
                <a href="/weibo/edit?id={id}">修改</a></p>
                <button class="weibo-show-comment" data-id="{id}">评论</button>
                <div>
                    {comments}
                </div>
                <div id="id-div-comment-{id}" class="weibo-comment-form weibo-hide">
                    <form action="/weibo/comment/add" method="post">
                        <input name="user_id" value="{user_id}" type="hidden">
                        <input name="weibo_id" value="{id}" type="hidden">
                        <textarea name="content"></textarea>
                        <button type="submit">添加评论</button>
                    </form>
                </div>
            </p>
            """.format(**w)
    # 用 join() 返回 str
    weibos = '\n'.join([weibo_tag(w) for w in weibos])
    body = template('weibo_index.html', weibos=weibos)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
开发者ID:gayu-mike,项目名称:learnweb,代码行数:60,代码来源:routes_weibo.py

示例14: getAllUser

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def getAllUser():
    userList = []
    for item in User.find():
        user = {
            'id' : item['id'],
            'user_name' : item['user_name'],
            'password' : item['password'],
            'type' : item['type'],
            'project_id' : item['project_id'],
            'vehicle_id' : item['vehicle_id'],
            'name' : item['name'],
            'phone' : item['phone'],
            'status' : item['status'],
        }
        userList.append(user)
    return userList
开发者ID:EmbeddedSoftwareCaiShuPeng,项目名称:vehicleDispatcher,代码行数:18,代码来源:UserAccess.py

示例15: cookie2user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import find [as 别名]
def cookie2user(cookie_str):
    if not cookie_str:
        return None

    try:
        L = cookie_str.split('-')
        user_id, expires, sha1 = L
        user = yield from  User.find(user_id)
        if user is None:
            return None

        s = '%s-%s-%s-%s' % (user_id, user.password, expires, _COOKIE_KEY)
        if (sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest()):
            return None
        user.password = '******'
        return user
    except Exception as e:
        logging.exception(e)
        return None
开发者ID:pengxiaojun,项目名称:ipsan,代码行数:21,代码来源:handlers.py


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