def device_get(device_id=None): (_type,instance)=TokenRef.get_ref(request.headers.get('token')) if _type is 'user': device = Device.get_instance(device_id) if device: if device in instance.devices: return succ_doc(device) else: return erro('permission not allow.') return succ_doc(instance.devices) elif _type is 'device': return succ_doc(instance) return erro('wrong token.')
def action_get(action_symbol=None): if not action_symbol: return erro('No symbol provided.') arg = request.args (arg_device,arg_state) = (arg.get('device'),arg.get('state')) (_type,instance)=TokenRef.get_ref(request.headers.get('token')) if not _type: return erro('wrong token.') device = None updated= None if _type is 'user': _device = Device.get_instance(arg_device) if not _device: return erro('wrong device id.') if instance.check_device(_device): device = _device updated='should_state' else: return erro('permission not allow.') elif _type is 'device': device = instance updated='state' actions = device.actions action = None try: action = actions[action_symbol] except KeyError: return erro('(symbol not found.)action not exists.') if arg_state: #update try: action[updated] = arg_state actions[action_symbol] = action device.update(actions=actions) return succ({}) finally: return erro('unknown error.') else: #get return succ_doc(action)
def device_delete(device_id): (_type,instance)=TokenRef.get_ref(request.headers.get('token')) if _type is 'device': try: instance.delete() return succ({}) finally: return erro('unknown mistakes.') elif _type is 'user': device = Device.get_instance(device_id) if device: try: instance.update(pull__devices=device) return succ_doc(instance) finally: return erro('unknown mistakes.') else: erro('wrong device_id.') else: return erro('wrong token.')
def device_post(): def bind_device(user,device): user.update(push__devices=device) (_type,instance)=TokenRef.get_ref(request.headers.get('token')) json=request.get_json() if _type is 'user': user = instance if not json: json = {} arg_device = json.get('device') arg_bindcode = json.get('bind_code') arg_name=json.get('name') if arg_device and arg_bindcode: #bind a device try: d= Device.get_instance(arg_device) if not d: return erro('wrong device id.') if arg_bindcode is d.bind_code: bind_device(user,d) return succ_doc(d) else: return erro('wring bind code.') finally: #if program here,some mistake must happend. return erro('unknown misktake.') elif arg_device: #clone a device try: be_cloned= Device.get_instance(arg_device) if not be_cloned: return erro('wrong device id.') clone_device = Device.clone(be_cloned) clone_device.name = arg_name clone_device.save() bind_device(user,clone_device) return succ({ 'device':clone_device.id, 'token':TokenRef.get_token(clone_device), 'bind_code':clone_device.bind_code }) finally:#if program here,some mistake must happend. return erro('unknown misktake.') else: #create a new Device try: device = Device(name = arg_name).save() bind_device(user,device) return succ({ 'device':str(device.id), 'token':TokenRef.get_token(device), 'bind_code':device.bind_code }) except Exception as err: raise(err); return erro(err) # finally: # return erro('unknown misktake.') elif _type is 'device': #update a device try: instance.update(**json) return succ_doc(instance) finally: return erro('something wrong when update the device.') else: return erro('wrong token')