def get(self, *args, **kwargs): user_id = args[0] if len(args) > 0 else None db = self.settings['db'] if user_id: user = yield db.user.find_one({"_id": ObjectId(user_id)}) self.write(bson_encode(user)) else: users = yield db.user.find({}).to_list(length=None) self.write(bson_encode(users))
def get(self, *args, **kwargs): user_id=args[0] if len(args)>0 else None db=self.settings['db'] if user_id: user =yield db.user.find_one({"_id":ObjectId(user_id)}) self.write(bson_encode(user)) else: users=yield db.user.find({}).to_list(length=None) self.write(bson_encode(users))
def get(self, *args, **kwargs): id = args[0] if len(args) > 0 else None if id: db = self.settings['db'] role = yield db[self.cname].find_one({"_id": ObjectId(id)}) self.write(bson_encode(role)) return # get all role db = self.settings['db'] roles = yield db[self.cname].find({}).to_list(length=None) self.write(bson_encode(roles))
def get(self, *args, **kwargs): id = args[0] if len(args) > 0 else None if id: db = self.settings['db'] role = yield db[self.cname].find_one({"_id": ObjectId(id)}) self.write(bson_encode(role)) return # get all role db = self.settings['db'] roles = yield db[self.cname].find({}).to_list(length=None) self.write(bson_encode( roles))
def send_message(self, obj, status_code=0,total=None): """ 发送消息到客户端 :param obj 带发送到客户端的对象一个字典对象 :param status_code 状态码 """ # todo # if isinstance(obj,dict): self.set_header("content-type", "application/json") if not total : data={"data": obj, "status_code": status_code,"total":len(obj) if type(obj)==list else 0} self.write(bson_encode(data)) else: self.write(bson_encode({"data": obj, "status_code": status_code,"total":total }))
def get(self, *args, **kwargs): db=self.settings['dbsync'] def get_node_child(node): if not node: return child_list=db.menu.find({"parent_uid":node.get('uid')}) node['children']=[] node['children']=[get_node_child(item) for item in child_list] return node if args[0] if len(args)>0 else None and args[0]=='all_leaf':#取所有的叶子节点 leaf_node=list(db.menu.find({"is_leaf":True})) self.write(bson_encode(leaf_node)) else: root_node= db.menu.find({"parent_uid":{"$exists":False}}) result=[get_node_child(itm) for itm in root_node] self.write(bson_encode(result))
def get(self, *args, **kwargs): db = self.settings['dbsync'] def get_node_child(node): if not node: return child_list = db.menu.find({"parent_uid": node.get('uid')}) node['children'] = [] node['children'] = [get_node_child(item) for item in child_list] return node if args[0] if len( args) > 0 else None and args[0] == 'all_leaf': #取所有的叶子节点 leaf_node = list(db.menu.find({"is_leaf": True})) self.write(bson_encode(leaf_node)) else: root_node = db.menu.find({"parent_uid": {"$exists": False}}) result = [get_node_child(itm) for itm in root_node] self.write(bson_encode(result))
def get(self, *args, **kwargs): id = args[0] if len(args) > 0 else None if id: #db = self.settings['db'] db = self.settings['mongo_client'][self.db] obj = yield db[self.cname].find_one({"_id": ObjectId(id) if self.is_object_id(id) else id}) self.write(bson_encode(obj)) return q, p, s = get_query_args(self.request) pageIndex = int(self.get_query_argument('pageIndex', 0)) pageSize = int(self.get_query_argument('pageSize', 10)) if self.sort_field: s[self.sort_field]=1 if self.sort_order=='desc' else -1 db = self.settings['mongo_client'][self.db] cursor = db[self.cname].find(q) if not p: if not s: objs = yield cursor.skip(pageIndex * pageSize).limit(pageSize).to_list(length=None) else: objs = yield cursor.skip(pageIndex * pageSize).limit(pageSize).sort(s.items()).to_list(length=None) else: cursor = db[self.cname].find(q, p) if not s: objs = yield cursor.skip(pageIndex * pageSize).limit(pageSize).to_list(length=None) else: objs = yield cursor.skip(pageIndex * pageSize).limit(pageSize).sort(s.items()).to_list(length=None) total = yield cursor.count() result = { 'total': total, 'data': objs } self.write(bson_encode(result))
def get(self, *args, **kwargs): q, p, s = get_query_args(self.request) db = self.settings['mongo_client'][self.db] if p: cursor = db[self.cname].find(q,p) else: cursor=db[self.cname].find(q) result = yield cursor.to_list(length=None) # print result self.write(bson_encode(result))
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 write_page(self,cursor,*args,**kwargs): """向浏览器输出一页数据 :param cursor mongodb的数据库游标 :param pageIndex 浏览器传入的当前页码 :param pageSize 当前页显示的行数""" total=yield cursor.count() s={} if self.sort_field: s[self.sort_field]=1 if self.sort_order =='desc' else -1 if s: data=yield cursor.skip(self.page_index*self.page_size).limit(self.page_size).sort(s.items()).to_list(length=None) else: data=yield cursor.skip(self.page_index*self.page_size).limit(self.page_size).to_list(length=None) self.set_header('Content-Type', 'application/json;charset=UTF-8') self.write(bson_encode({'data':data,'total':total}))
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): 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()