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


Python User.passwd方法代码示例

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


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

示例1: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*, email, name, passwd,img_uuid):
    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 APIValueError('email', 'Email is already in use.')
    users = yield from User.findAll('name=?', [name])
    if len(users) > 0:
        raise APIValueError('name', 'name is already in use.')
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)
    img_path="/static/HeadImg/"
    img_path=img_path+img_uuid
    img_path=img_path+".jpg"

    path=os.path.abspath('.')
    path=os.path.join(path,"static")
    path=os.path.join(path,"HeadImg")
    path=os.path.join(path,"%s.jpg" % img_uuid)
    if not os.path.exists(path):
        img_path="/static/img/default.jpg"
    user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image=img_path)
    yield from user.save()
    # make session 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
开发者ID:daihaovigg,项目名称:web,代码行数:36,代码来源:handlers.py

示例2: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*, email, name, passwd):
    #判断name是否为空:
    if not name or not name.strip():
        raise APIValueError('name')
    #判断email是否为空及是否满足email格式:
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    #判断password首付为空及是否满足password格式:
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    #数据中查询对应的email信息:
    users = yield from User.findAll('email=?', [email])
    #判断查询结果是否存在,若存在则返回异常提示邮件已存在:
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    #生成唯一ID:
    uid = next_id()
    #重构唯一ID和password成新的字符串:
    sha1_passwd = '%s:%s' % (uid, passwd)
    #构建用户对象信息:
    #hashlib.sha1().hexdigest():取得SHA1哈希摘要算法的摘要值。
    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:
    #构造session cookie信息:
    r = web.Response()
    #aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)   #max_age:定义cookie的有效期(秒);
    user.passwd = '******'
    r.content_type = 'application/json'
    #以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:zhangshengyue,项目名称:text,代码行数:36,代码来源:handlersdetail.py

示例3: api_register_user

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

示例4: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
async 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('password')

    users = await 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)
    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()
    user = User(uid=uid,
                name=name.strip(),
                email=email,
                passwd=passwd,
                image=image)
    await user.save()
    # make session in 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
开发者ID:chensource,项目名称:BeginningPython,代码行数:35,代码来源:handlers.py

示例5: API_UserRegister

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
async def API_UserRegister(*, 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 = await 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   = r'E:\Study\Git\Python\myPython3WebApp\www\static\img\user.png'
    )
    await user.save()

    #make session 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
开发者ID:LightsJiao,项目名称:MyGitHub,代码行数:33,代码来源:handlers.py

示例6: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
async def api_register_user(*, email, name, passwd):
	'''
	Store user register info
	'''
	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 APIError('register:failed', 'email', 'Email is already in use.')
	users = await User.findAll('name=?', [name])
	if len(users) > 0:
		raise APIError('register:failed', 'name', 'Username is already in use.')
	uid = next_id()
	sha1_passwd = '%s:%s' % (uid, passwd)
	# hashlib.sha1().hexdigest():取得SHA1哈希摘要算法的摘要值。
	# 用户口令是客户端传递的经过SHA1计算后的40位Hash字符串,所以服务器端并不知道用户的原始口令
	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())
	await user.save()
	# make session cookie
	r = web.Response()
	r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
	user.passwd = '******'
	r.content_type = 'application/json'
	# 以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。
	r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
	return r
开发者ID:jiejackyzhang,项目名称:awsome-python3-blog-webapp,代码行数:32,代码来源:handlers.py

示例7: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
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('password')

    # 要求邮箱是唯一的
    users = yield from User.findAll('email=?', [email])
    if len(users) > 0:
        raise APIError('register:faild', 'email', 'Email is already in use')
    
    # 生成当前注册用户唯一的uid
    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()
    logging.info('save user: %s ok' % name)

    # 构建返回信息
    r = web.Response()
    # 添加cookie
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    user.passwd = '******'
    # 设置返回的数据格式是json
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:Parlefan,项目名称:Python3-BlogWeb,代码行数:34,代码来源:handlers.py

示例8: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*, email, name, passwd):
    logging.info('api_register_user...')
    #判断name是否存在,且是否'\n','\r','\t',' '这种特殊字符
    if not name or not name.strip():
        raise APIValueError('name')
    #判断email是否存在,且符合格式
    if not email or not _RE_EMAIL.match(email):
        logging.info('email api_register_user...')
        raise APIValueError('email')
    #判断passwd是否存在,且是否符合格式
    if not passwd  or not _RE_SHA1.match(passwd):
        logging.info('passwd api_register_user...')
        raise APIValueError('passwd')

    #查一下库里是否有相同的email地址,如果有的话提示用户email已经被注册过
    users = yield from User.findAll('email=?', [email])
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use')

    #生成一个当前要注册的用户的唯一uid
    uid = next_id()
    #构建shal_passwd
    sha1_passwd = '%s:%s' % (uid, passwd)

    admin = False
    if email == '[email protected]':
            admin = True

    #创建一个用户,密码通过sha1加密保存
    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(), admin=admin)

    #保存这个用户到数据库用户表
    yield from 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 = '******'
    #返回的是json数据,所以设置content-type为json的
    r.content_type = 'application/json'
    #把对象转换成json格式返回
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:idyllchow,项目名称:bit-record,代码行数:47,代码来源:handlers.py

示例9: api_register

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register(*, 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(), groups='001449655503983177fbe60d9744c9d99c77ed1a7612acd000')
    yield from user.save()
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(user, MAX_AGE), max_age=MAX_AGE, httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return(r)
开发者ID:Madongming,项目名称:myframework,代码行数:22,代码来源:userregAlogin.py

示例10: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
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.find_all('email=?', [email])
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    user = User(name=name.strip(), email=email, passwd=passwd,
                image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
    yield from user.register()
    # make session cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user.user2cookie(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
开发者ID:utopiaprince,项目名称:python-blog,代码行数:22,代码来源:handlers.py

示例11: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
async def api_register_user(*, email, name, passwd):
    if not email and not name and not passwd:
        raise Exception('missing arguments for register')
    if not _RE_EMAIL.match(email):
        raise Exception('illegal email')
    if not _RE_SHA1.match(passwd):
        raise Exception('illegal passwd')
    users = await User.findAll('email=?', [email])
    if len(users) > 0:
        raise Exception('email existed')
    uid = next_id()
    sha1_passwd = '%s:%s' % (uid , passwd)
    user = User(id=uid, email=email, name=name.strip(), passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image="blank:about", created_at=time.time())
    await user.save()
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(user, 60*60*24), max_age=60*60*24, httponly=True)
    user.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:theJian,项目名称:plogger,代码行数:22,代码来源:handlers.py

示例12: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*, email, name, passwd):
    """
    save in table: USER
    登录之后,可以增加邮箱激活模块,邮件激活。
    """
    logging.info("......................")
    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)
    # 创建用户对象, 其中密码并不是用户输入的密码,而是经过复杂处理后的保密字符串
    # sha1(secure hash algorithm),是一种不可逆的安全算法.
    # hexdigest()函数将hash对象转换成16进制表示的字符串
    # md5是另一种安全算法
    # Gravatar(Globally Recognized Avatar)是一项用于提供在全球范围内使用的头像服务。
    # 便可以在其他任何支持Gravatar的博客、论坛等地方使用它。此处image就是一个根据用户email生成的头像
    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(),
        image="about:blank"
    )
    yield from user.save()
    # 此处的cookie:网站为了辨别用户身份而储存在用户本地终端的数据
    # http协议是一种无状态的协议,即服务器并不知道用户上一次做了什么.服务器通过cookie跟踪用户状态。
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)  # 86400s=24h
    # 修改密码的外部显示为* ?
    user.passwd = '*****'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:lovelywxd,项目名称:blogs-web-python,代码行数:43,代码来源:handlers.py

示例13: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
async 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 = await 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='https://www.funnypica.com/wp-content/uploads/2015/05/TOP-50-Beautiful-Girls-Girl-25-of-50.jpg')
    await user.save()
    # make session 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
开发者ID:GitDongLei,项目名称:Awesome_Equipment_Management_System,代码行数:23,代码来源:handlers.py

示例14: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*,name, email, 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")
    # 在数据库里查看是否已存在该email
    users = yield from User.findAll('email=?', [email]) # mysql parameters are listed in list
    if len(users) > 0: # findAll的结果不为0,说明数据库已存在同名email,抛出异常报错
        raise APIError('register:failed', 'email', 'Email is already in use.')

    # 数据库内无相应的email信息,说明是第一次注册
    uid = next_id() # 利用当前时间与随机生成的uuid生成user id
    sha1_passwd = '%s:%s' % (uid, passwd) # 将user id与密码的组合赋给sha1_passwd变量
    # 创建用户对象, 其中密码并不是用户输入的密码,而是经过复杂处理后的保密字符串
    # unicode对象在进行哈希运算之前必须先编码
    # sha1(secure hash algorithm),是一种不可逆的安全算法.这在一定程度上保证了安全性,因为用户密码只有用户一个人知道
    # hexdigest()函数将hash对象转换成16进制表示的字符串
    # md5是另一种安全算法
    # Gravatar(Globally Recognized Avatar)是一项用于提供在全球范围内使用的头像服务。只要在Gravatar的服务器上上传了你自己的头像,便可以在其他任何支持Gravatar的博客、论坛等地方使用它。此处image就是一个根据用户email生成的头像
    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() # 将用户信息储存到数据库中,save()方法封装的实际是数据库的insert操作

    # 这其实还是一个handler,因此需要返回response. 此时返回的response是带有cookie的响应
    r = web.Response()
    # 刚创建的的用户设置cookiei(网站为了辨别用户身份而储存在用户本地终端的数据)
    # http协议是一种无状态的协议,即服务器并不知道用户上一次做了什么.
    # 因此服务器可以通过设置或读取Cookies中包含信息,借此维护用户跟服务器会话中的状态
    # user2cookie设置的是cookie的值
    # max_age是cookie的最大存活周期,单位是秒.当时间结束时,客户端将抛弃该cookie.之后需要重新登录
    r.set_cookie(COOKIE_NAME, user2cookie(user, 600), max_age=600, httponly=True)  # 设置cookie最大存会时间为10min
    # r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)  #86400s=24h
    user.passwd = '*****' # 修改密码的外部显示为*
    # 设置content_type,将在data_factory中间件中继续处理
    r.content_type = 'application/json'
    # json.dumps方法将对象序列化为json格式
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:Joe-Blake,项目名称:Blog,代码行数:42,代码来源:handlers.py

示例15: api_register_user

# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import passwd [as 别名]
def api_register_user(*, email, name, passwd):
    # 判断name是否存在,且是否只是'\n', '\r',  '\t',  ' ',这种特殊字符
    if not name or not name.strip():
        raise APIValueError('name')
    # 判断email和passwd是否存在,且是否符合规定的正则表达式
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not passwd or not _RE_SHA1.match(passwd):
        raise APIValueError('passwd')
    # 查一下库里是否有相同的email地址,如果有的话提示用户email已经被注册过    
    users = yield from User.findAll('email=?', [email])
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    # 生成一个当前要注册用户的唯一uid    
    uid = next_id()
    # 构建shal_passwd
    sha1_passwd = '%s:%s' % (uid, passwd)

    admin = False
    if email == '[email protected]':
        admin = True

    # 创建一个用户
    # 用户口令是客户端传递的经过SHA1计算后的40位Hash字符串,所以服务器端并不知道用户的原始口令
    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()
    logging.info('save user OK')

    # make session cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True)
    # 只把要返回的实例的密码改成'******',库里的密码依然是正确的,以保证真实的密码不会因返回而暴漏
    user.passwd = '******'
    # 返回的是json数据,所以设置content-type为json的
    r.content_type = 'application/json'
    # 把对象转换成json格式返回   
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
开发者ID:COREENE,项目名称:python,代码行数:42,代码来源:handlers.py


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