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.gravator.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
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 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.') sha1_passwd = '%s:%s' % (email, passwd) user = User(name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest()) await user.save() # make session cookie: print('---------------response -----------------') users = await User.findAll('email=?', [email]) r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' result = ResultBean(0, users[0]) r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
def api_register_user(*, UserID, Phone, name, Pass): print(UserID) if not UserID: raise APIValueError('身份证号') if not name: raise APIValueError('姓名') if not Pass or not _RE_SHA1.match(Pass): raise APIValueError('密码') if not Phone: raise APIValueError('手机号') users = yield from User.findAll('Phone=?', [Phone]) if len(users) > 0: raise APIError('register:failed', 'phone', 'Phone is already in use.') sha1_Pass = '******' % (Phone, Pass) user = User(UserID=UserID, User=name, Pass=hashlib.sha1(sha1_Pass.encode('utf-8')).hexdigest(), Phone=Phone) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.Pass = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=True).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_signup_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('signup:failed', 'email', 'Email is already in use') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) if name == 'sharon': admin = 1 else: admin = 0 user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), admin=admin) 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 switchDevice(*, id, status): if not id or not id.strip(): raise APIValueError('id') device = await Device.findAll("id=?", [id]) if len(device) == 0: raise APIError('swicth:failed', 'device', 'do not has device.') if device[0].status == status: raise APIError('swicth:failed', 'device', 'device is in the same status') # control.switch(device[0].port,status) device[0].status = status await device[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "swicth finish") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def api_register_users(*, 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') send_user_email(email, name, passwd) return dict(r='yes')
async def deleteDevice(*, id): if not id or not id.strip(): raise APIValueError('name') device = await Device.findAll('id=?', [id]) if len(device) == 0: raise APIError('delete:failed', 'id', 'do not has id.') await device[0].remove() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "remove finish") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def user_update_name(*, id, name): if not id or not id.strip(): raise APIValueError('id') users = await User.findAll("id=?", [id]) if len(users) == 0: raise APIError('swicth:failed', 'device', 'do not has device.') users[0].name = name await users[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "update success") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def updateDevicePer(*, id, perLevel): if not id or not id.strip(): raise APIValueError('id') device = await Device.findAll("id=?", [id]) if len(device) == 0: raise APIError('update:failed', 'device', 'do not has device.') device[0].perLevel = perLevel await device[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "update finish") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def updateDeviceTimer(*, id, week, time, task, schedule): if not id or not id.strip(): raise APIValueError('id') device = await Device.findAll("id=?", [id]) if len(device) == 0: raise APIError('update:failed', 'device', 'do not has device.') device[0].time = time device[0].week = week device[0].task = task device[0].schedule = schedule await device[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "success") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def user_admin_update_password(*, id, oldPass, newPas): if not id or not id.strip(): raise APIValueError('id') users = await User.findAll("id=?", [id]) if len(users) == 0: raise APIError('swicth:failed', 'device', 'do not has device.') if oldPass != users[0].perPasswd: r = web.Response() result = ResultBean(1, "oldpass erroe") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r users[0].perPasswd = newPas await users[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "update success") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def addDevice(*, name, kind, permissLevel=_PERMISS_CHILD): if not name or not name.strip(): raise APIValueError('name') if not kind: raise APIValueError('kind') device = await Device.findAll('name=?', [name]) if len(device) > 0: raise APIError('add:failed', 'device', 'device is already in use.') device = Device(name=name, kind=kind, status=_SWITCH_OFF, permissLevel=permissLevel) await device.save() device = await Device.findAll('name=?', [name]) r = web.Response() r.content_type = 'application/json' result = ResultBean(0, device[0]) r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def user_update_password(*, id, oldPass, newPas): if not id or not id.strip(): raise APIValueError('id') users = await User.findAll("id=?", [id]) if len(users) == 0: raise APIError('swicth:failed', 'device', 'do not has device.') sha1_passwd = '%s:%s' % (users[0].email, oldPass) sha_old = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest() if sha_old != users[0].passwd: r = web.Response() result = ResultBean(1, "oldpass erroe") r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r sha1_passwd = '%s:%s' % (users[0].email, newPas) users[0].passwd = hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest() await users[0].update() r = web.Response() r.content_type = 'application/json' result = ResultBean(0, "update success") r.body = json.dumps(result, 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() 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