python类findAll()的实例源码

handlers.py 文件源码 项目:python3-webapp 作者: chenpengcong 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def api_get_users(*, page = '1'):
    '''
    ?????????.
    '''
    page_index = get_page_index(page)
    num = await User.findNumber('count(id)')
    p = Page(num, page_index)
    if num == 0:
        return dict(page = p, users = ())
    users = await User.findAll(orderBy = 'created_at desc', limit = (p.offset, p.limit))
    for u in users:
        u.passwd = '******'
    return dict(page = p, users = users)
handlers.py 文件源码 项目:python3-webapp 作者: chenpengcong 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def api_blogs(*, page = '1'):
    '''
    ????????, ????manage_blogs.html.
    '''
    page_index = get_page_index(page)
    num = await Blog.findNumber('count(id)')
    p = Page(num, page_index)
    if num == 0:
        return dict(page = p, blogs = ())
    blogs = await Blog.findAll(orderby = 'created_at desc', limit = (p.offset, p.limit))
    return dict(page = p, blogs = blogs)
handlers.py 文件源码 项目:python1 作者: lilizhiwei 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def authenticate(*, email, passwd):  # ???????????
    # ????????????
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid password.')
    # ???????email???list?????
    users = yield from User.findAll('email=?', [email])
    # ??list???0??????????????????????
    if len(users) == 0:
        raise APIValueError('email', 'Email not exist.')
    user = users[0] # ??????.???,?????????,???????list
    # ????:
    # ????????????????,????????
    # ????????????????????,???????????????,?????????
    # ???????????:sha1 = hashlib.sha1((user.id+":"+passwd).encode("utf-8"))
    # ???????????????(?api_register_user),??????
    sha1 = hashlib.sha1()
    sha1.update(user.id.encode('utf-8'))
    sha1.update(b':')
    sha1.update(passwd.encode('utf-8'))
    if user.passwd != sha1.hexdigest():
        raise APIValueError('passwd', 'Invalid password.')
    # ???????????cookie:
    # ?????????????
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r

# ??????
handlers.py 文件源码 项目:python1 作者: lilizhiwei 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def api_comments(*, page='1'):
    page_index = get_page_index(page)
    num = yield from Comment.findNumber('count(id)')  # num?????
    p = Page(num, page_index)  # ??Page?????????
    if num == 0:
        return dict(page=p, comments=())  # ???????????????app.py?response??????
    # ??????0,??????????
    # limit??select??????????,?????????,?????????????
    comments = yield from Comment.findAll(orderBy='created_at desc', limit=(p.offset, p.limit))
    return dict(page=p, comments=comments)

# day14??
# API?????
handlers.py 文件源码 项目:Preeminent 作者: ReedSun 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def api_register_user(*, email, name, passwd):  # ??????????????
    # ????????
    # ?????name
    if not name or not name.strip():  # s.strip(rm)??????s?????????????rm???????
                                      # ??rm????????????
        raise APIValueError('name')
    # ??email?????????????
    if not email or not _RE_EMAIL.match(email): 
        raise APIValueError('email')
    # ??passwd???SHA1??????????
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    # ?????????????email
    users = yield from User.findAll('email=?', [email])
    # users??????????????????email???????
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')

    # ???????email???????????
    uid = next_id()   # next_id?models????????????????????id??????????????
    sha1_passwd = '%s:%s' % (uid, passwd)  # ???id?????
    # ????????????????????
    # unicode???????????????????utf8??
    # hashlib.sha1()??????????sha1?
    # hash.hexdigest()???hash?????16????????
    # ?????sha1?????????md5??
    # Gravatar???????????????????????????
    user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
    yield from user.save()  # ????????????

    # make session cookie:
    r = web.Response()
    # ?????????cookie(???????????????????????)
    # http???????????,?????????????????.
    # ??????????????Cookies?????,????????????????
    # user2cookie????cookie?????????
    # max_age?cookie???????,????.??????,???????cookie.????????
    # ?????????24??
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    user.passwd = '******'  # ??????????*
    # ??content_type???data_factory????????
    r.content_type = 'application/json'
    # json.dump?????????json??
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r



# API?????
handlers.py 文件源码 项目:python1 作者: lilizhiwei 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def api_register_user(*, email, name, passwd):  # ??????????????
    # ????????
    # ?????name
    if not name or not name.strip():  # s.strip(rm)??????s?????????????rm???????
                                      # ??rm????????????
        raise APIValueError('name')
    # ??email?????????????
    if not email or not _RE_EMAIL.match(email): 
        raise APIValueError('email')
    # ??passwd???SHA1??????????
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    # ?????????????email
    users = yield from User.findAll('email=?', [email])
    # users??????????????????email???????
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')

    # ???????email???????????
    uid = next_id()   # next_id?models????????????????????id??????????????
    sha1_passwd = '%s:%s' % (uid, passwd)  # ???id?????
    # ????????????????????
    # unicode???????????????????utf8??
    # hashlib.sha1()??????????sha1?
    # hash.hexdigest()???hash?????16????????
    # ?????sha1?????????md5??
    # Gravatar???????????????????????????
    user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
    yield from user.save()  # ????????????

    # make session cookie:
    r = web.Response()
    # ?????????cookie(???????????????????????)
    # http???????????,?????????????????.
    # ??????????????Cookies?????,????????????????
    # user2cookie????cookie?????????
    # max_age?cookie???????,????.??????,???????cookie.????????
    # ?????????24??
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    user.passwd = '******'  # ??????????*
    # ??content_type???data_factory????????
    r.content_type = 'application/json'
    # json.dump?????????json??
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r



# API?????


问题


面经


文章

微信
公众号

扫码关注公众号