예제 #1
0
async def api_register_user(*, email, name, passwd):
    #str.strip([chars])移除字符串头尾指定的字符(默认空格)
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    users = await User.findAll('email=?', [email])
    if len(users) > 0:
        raise APIValueError('register:failed', 'email',
                            'Email is already in use.')
    uid = next_id()
    # 加密形式:next_id():passwd,数据库中保存其摘要hexdigest()。与上面验证的时候要保持一致
    sha1_passwd = '%s:%s' % (uid, passwd)
    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=robohash&s=120' %
                hashlib.md5(email.encode('utf-8')).hexdigest())
    await user.save()
    r = web.Response()
    #set_cookie(name,value,*,path='/',expires=None,domain=None,max_age=None,secure=None,httponly=None,version=None)
    #name:cookie名称(str),value:cookie值(str),expires在http1.1被遗弃,使用max_age代替
    #path(str):指定Cookie应用于的url的子集,默认'/'
    r.set_cookie(COOKIE_NAME,
                 user2cookie(user, 86400),
                 max_age=86400,
                 httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(users, ensure_ascii=False).encode('utf-8')
    return r
예제 #2
0
def api_register_user(*, email, name, passwd):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    users = yield from User.findAll('`email`=?', email)
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use')
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)
    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()
    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
예제 #3
0
async def api_register_user(*, email, name, passwd):
    # print('type of request= ------------',type(request))
    # print(request)
    # print(request.items())
    # print(type(request['request']))
    # print(dir(request['request']))
    # print(request['request'].json)
    print(email, name, passwd)

    # 判断name是否存在,且是否只是'\n', '\r',  '\t',  ' ',这种特殊字符
    if not name or not name.strip():
        raise APIValueError('name')
    # 判断email是否存在,且是否符合规定的正则表达式
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    # 判断passwd是否存在,且是否符合规定的正则表达式
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')

    #检测数据库中是否有相同的email地址,如果有,提示用户email已被注册
    users = await User.findAll('email=?', [email])
    if len(users) > 0:
        raise APIValueError('register:failed', 'email', 'Email is already in use.')

    #生产注册用户唯一uid
    uid = next_id()
    #构建sha1_passwd
    sha1_passwd = '%s:%s' % (uid,passwd)

    admin = False
    if email == '*****@*****.**':
        admin = True

    #创建用户
    user = User(
        id=uid,
        name=name.strip(),
        email=email,
        #密码存储用sha1算法转化
        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(), 
        admin=admin
        )
    await user.save()
    logging.info('save user ok')

    r = web.Response()
    #添加cookie
    r.set_cookie(COOKIE_NAME,user2cookie(user,86400),max_age=86400,httponly=True)
    #把返回的实例的密码改成‘**************’,防止密码泄露
    user.passwd = '*******'
    #返回的shijson,所及设置content-type为json
    r.content_type = 'application/json'
    #把对象转换成json格式
    r.body = json.dumps(user,ensure_ascii=False).encode('utf-8')
    return r
예제 #4
0
from coroweb import get, post