def post(self, *args, **kwargs): if is_json_request(self.request): body = json.loads(self.request.body) body = body.get('data', None) else: body = self.get_argument('data', None) body = escape.json_decode(body) if body else {} # self.send_error(reason="仅支持Content-type:application/json") # return db = self.settings['mongo_client'][self.db] for row in body: id = row.get('id', None) if row.get('_state', None) == 'removed': if self.is_object_id(id): yield db[self.cname].remove({"_id": ObjectId(id)}) if id and self.is_object_id(id): # update row['update_time'] = format_datetime(datetime.now()) row['update_user'] = self.current_user.get('userid', '') yield db[self.cname].update({"_id": ObjectId(id) if self.is_object_id(id) else id}, { "$set": clone_dict(row, without=[]) }) else: obj = clone_dict(row) obj['id'] = ObjectId() obj['_id'] = obj['id'] obj['create_time'] = format_datetime(datetime.now()) obj['create_user'] = self.current_user.get('userid', '') yield db[self.cname].insert(obj) # self.write(generate_response(message="保存成功")) self.send_message("保存成功")
def post(self, *args, **kwargs): logging.info(self.request.body) if not is_json_request(self.request): raise HTTPError(status_code=500, log_message="目前仅支持application/json的请求") body = json.loads(self.request.body) db = self.settings['db'] olduser = yield db.user.find({"name": body.get("name")}).to_list(length=None) if olduser and len(olduser) > 0: self.write("当前用户%s已经存在" % body.get("name")) self.finish() else: body['password'] = make_password(body.get('password')) yield db.user.insert(body) self.write(bson_encode({"user": body}))
def post(self, *args, **kwargs): if is_json_request(self.request): body = json.loads(self.request.body) else: raise HTTPError(status_code=500, log_message="仅支持Content-type:application/json") db = self.settings['db'] if body.get('_id', None): # update yield db[self.cname].update({"_id": ObjectId(body.get('_id'))}, { "$set": clone_dict_without_id(body) }) else: yield db[self.cname].insert(clone_dict_without_id(body)) self.write(generate_response(message="保存成功"))
def post(self, *args, **kwargs): if is_json_request(self.request): body = json.loads(self.request.body) else: raise HTTPError(status_code=500, log_message="仅支持Content-type:application/json") db = self.settings['db'] if body.get('_id', None): # update yield db[self.cname].update({"_id": ObjectId(body.get('_id'))}, {"$set": clone_dict_without_id(body)}) else: yield db[self.cname].insert(clone_dict_without_id(body)) self.write(generate_response(message="保存成功"))
def post(self, *args, **kwargs): if is_json_request(self.request): body=json.loads(self.request.body) else: raise HTTPError(status_code=500,log_message="仅支持Content-type:application/json") db=self.settings['dbsync'] db.menu.remove({}) def save_node(item): for innerItem in item.get('children',[]): save_node(innerItem) item['selected']=False item['is_leaf']=len(item['children'])==0 item.pop('children') db.menu.insert(item) #body is array for item in body: save_node(item)
def post(self, *args, **kwargs): if is_json_request(self.request): body=json.loads(self.request.body) else: raise HTTPError(status_code=500,log_message="仅支持Content-type:application/json") db=self.settings['db'] if body.get('_id',None):#update user_dict=clone_dict_without_id(body) user_dict.pop('password') yield db.user.update({"_id":ObjectId(body.get('_id'))},{ "$set":user_dict }) else: body['password']=make_password(body.get('password','111111')) yield db.user.insert(body) self.write(generate_response(message="保存成功"))
def post(self, *args, **kwargs): if is_json_request(self.request): body = json.loads(self.request.body) else: raise HTTPError(status_code=500, log_message="仅支持Content-type:application/json") db = self.settings['db'] if body.get('_id', None): #update user_dict = clone_dict_without_id(body) user_dict.pop('password') yield db.user.update({"_id": ObjectId(body.get('_id'))}, {"$set": user_dict}) else: body['password'] = make_password(body.get('password', '111111')) yield db.user.insert(body) self.write(generate_response(message="保存成功"))
def post(self, *args, **kwargs): logging.info(self.request.body) if not is_json_request(self.request): raise HTTPError(status_code=500, log_message="目前仅支持application/json的请求") body = json.loads(self.request.body) db = self.settings['db'] olduser = yield db.user.find({ "name": body.get("name") }).to_list(length=None) if olduser and len(olduser) > 0: self.write("当前用户%s已经存在" % body.get("name")) self.finish() else: body['password'] = make_password(body.get('password')) yield db.user.insert(body) self.write(bson_encode({"user": body}))
def put(self, *args, **kwargs): """接受用户的请求对文档进行更新 :param args url路径的参数 """ if is_json_request(self.request): body = json.loads(self.request.body) else: self.send_error(reason="仅支持Content-type:application/json") db = self.settings['mongo_client'][self.db] id = body.get('_id', None) if id : # update body['update_time'] = format_datetime(datetime.now()) body['update_user'] = self.current_user.get('userid', '') yield db[self.cname].update({"_id": ObjectId(id) if self.is_object_id(id) else id}, { "$set": clone_dict(body) }) self.send_message("保存成功")
def post(self, *args, **kwargs): if is_json_request(self.request): body = json.loads(self.request.body) else: raise HTTPError(status_code=500, log_message="仅支持Content-type:application/json") db = self.settings['dbsync'] db.menu.remove({}) def save_node(item): for innerItem in item.get('children', []): save_node(innerItem) item['selected'] = False item['is_leaf'] = len(item['children']) == 0 item.pop('children') db.menu.insert(item) #body is array for item in body: save_node(item)
def post(self, *args, **kwargs): logging.info(self.request.body) user = {} if is_json_request(self.request): user = json_decode(self.request.body) logging.info('email:%s' % user.get('token')['principal']) logging.info('password:%s' % user.get('token')['credentials']) email = user.get('token')['principal'] password = user.get('token')['credentials'] db = self.settings['db'] dbsync = self.settings['dbsync'] current_user = yield db.user.find_one({"email": email}) if current_user: if make_password( password) == current_user['password']: # login success # 当前登陆用户的角色必须在角色列表中存在 if current_user.get('selected_roles', None): user_roles = current_user.get('selected_roles') role_id_list = [ ObjectId(item.get('_id')) for item in user_roles ] roles = yield db.role.find({ "_id": { '$in': role_id_list } }).to_list(length=None) if roles and len(roles) > 0: current_user['roles'] = [ item.get('name') for item in roles ] # 当前登陆用户的权限必须在权限列表中存在 if current_user.get('selected_permissons', None): user_permissions = current_user.get('selected_permissons') permission_id_list = [ ObjectId(item.get('_id')) for item in user_permissions ] permissions = yield db.permission.find({ "_id": { '$in': permission_id_list } }).to_list(length=None) if permissions and len(permissions) > 0: current_user['permissions'] = [ item.get('name') for item in permissions ] menu_tree = [] # 当前菜单必须在菜单表中存在 if current_user.get('selected_menus'): user_menu = current_user.get('selected_menus') menu_tree = get_user_menu_tree(dbsync) menu_id_list = [item["_id"] for item in user_menu] # print menu_id_list # 3 level for l1 in menu_tree: # 1 level for l2 in l1.get('children'): # 2 level for l3 in l2.get('children'): # 3 level if l3['_id'] in menu_id_list: continue else: # remove item from children list l2.get('children').remove(l3) result = { "info": { "authc": { "principal": { "name": current_user.get('name'), "login": current_user.get('login'), "email": current_user.get('email') }, "credentials": { "name": current_user.get('name'), "login": current_user.get('login'), "email": current_user.get('email') }, "menu": menu_tree }, "authz": { "roles": current_user.get('roles', []), "permissions": current_user.get('permissions', []) } } } self.write(bson_encode(result)) else: self.write("密码输入错误") self.finish() else: self.write("当前登陆用户不存在") self.finish()
def post(self, *args, **kwargs): logging.info(self.request.body) user = {} if is_json_request(self.request): user = json_decode(self.request.body) logging.info('email:%s' % user.get('token')['principal']) logging.info('password:%s' % user.get('token')['credentials']) email = user.get('token')['principal'] password = user.get('token')['credentials'] db = self.settings['db'] dbsync = self.settings['dbsync'] current_user = yield db.user.find_one({"email": email}) if current_user: if make_password(password) == current_user['password']: # login success # 当前登陆用户的角色必须在角色列表中存在 if current_user.get('selected_roles', None): user_roles = current_user.get('selected_roles') role_id_list = [ObjectId(item.get('_id')) for item in user_roles] roles = yield db.role.find({"_id": {'$in': role_id_list}}).to_list(length=None) if roles and len(roles) > 0: current_user['roles'] = [item.get('name') for item in roles] # 当前登陆用户的权限必须在权限列表中存在 if current_user.get('selected_permissons', None): user_permissions = current_user.get('selected_permissons') permission_id_list = [ObjectId(item.get('_id')) for item in user_permissions] permissions = yield db.permission.find({"_id": {'$in': permission_id_list}}).to_list(length=None) if permissions and len(permissions) > 0: current_user['permissions'] = [item.get('name') for item in permissions] menu_tree = [] # 当前菜单必须在菜单表中存在 if current_user.get('selected_menus'): user_menu = current_user.get('selected_menus') menu_tree = get_user_menu_tree(dbsync) menu_id_list = [item["_id"] for item in user_menu] # print menu_id_list # 3 level for l1 in menu_tree: # 1 level for l2 in l1.get('children'): # 2 level for l3 in l2.get('children'): # 3 level if l3['_id'] in menu_id_list: continue else: # remove item from children list l2.get('children').remove(l3) result = { "info": { "authc": { "principal": { "name": current_user.get('name'), "login": current_user.get('login'), "email": current_user.get('email') }, "credentials": { "name": current_user.get('name'), "login": current_user.get('login'), "email": current_user.get('email') }, "menu": menu_tree }, "authz": { "roles": current_user.get('roles', []), "permissions": current_user.get('permissions', []) } } } self.write(bson_encode(result)) else: self.write("密码输入错误") self.finish() else: self.write("当前登陆用户不存在") self.finish()