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.find_all('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()) user = User() user.id = uid user.name = name.strip() user.email = email user.passwd = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest() user.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' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_register_user(*, email, name, passwd): # https://www.python.org/dev/peps/pep-3102/ 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 already in use') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) # get SHA1 for 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
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="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" r.body = json.dumps(user, ensure_ascii=False).encode("utf-8") return r
async def api_register_user(*, email, name, passwd): if not name or not str(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='') await 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
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(first).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
async def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name', 'Name must not be empty.') if not email or not _RE_EAMIL.match(email.lower()): raise APIValueError('email', 'Illegal email.') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd', 'Illegal passwd.') uid = next_id() passwd_solted = '%s:%s' % (uid, passwd) passwd_sha1 = hashlib.sha1(passwd_solted.encode('utf-8')).hexdigest() image_url = 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5( email.encode('utf-8')).hexdigest() user = User(id=uid, name=name.strip(), email=email, passwd=passwd_sha1, image=image_url) 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
async def api_reqister_user(*, email, name, passwd): users = await User.findAll(cond={'email': email}, step=1) if users: return ' User exists' user = User(name=name.strip(), email=email, passwd=passwd) await 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
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_password = '******' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email,passwd=hashlib.sha1(sha1_password.encode('utf-8').hexdigest(), image='')) 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).enconde('utf-8') return r
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() shal_passwd = '%s:%s' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(shal_passwd.encode('utf-8')).hexdigest(), image='http://test.download.cycore.cn/test/5ed5fc74-f110-42df-ade8-c5a2f10d572a.png') 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