def authenticate(*, email, passwd): responseCode = 0 if not email: responseCode = 1 raise APIValueError('email', 'Invalid email.') if not passwd: responseCode = 1 raise APIValueError('passwd', 'Invalid password.') users = yield from User.findAll('email=?', [email]) if len(users) == 0: raise APIValueError('email', 'Email not exist.') user = users[0] # check passwd: sha1 = hashlib.sha1() sha1.update(user.email.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd', 'Invalid password.') # authenticate ok, set cookie: 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, user) r.body = json.dumps(result, ensure_ascii=False).encode('utf-8') return r
async def devicesListTask(): device = await Device.findAll("schedule=?", [1]) r = web.Response() r.content_type = 'application/json' result = ResultBean(0, device) r.body = json.dumps(result, 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
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 devicesDetails(*, id): if not id: raise APIValueError('kind') device = await Device.findAll("id=?", [id]) 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
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 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 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