def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) tag_name = data.get('tag_name') users = data.get('users') dbs = data.get('dbs') ### ID列表 servers = data.get('servers') ### ID列表 proxy_host = data.get('proxy_host', None) if not tag_name: return self.write(dict(code=-1, msg='标签名称不能为空')) with DBContext('r') as session: exist_id = session.query(Tag.id).filter(Tag.tag_name == tag_name).first() if exist_id: return self.write(dict(code=-2, msg='标签名称重复')) ### 判断是否存在 with DBContext('w', None, True) as session: if users: users = ','.join(users) new_tag = Tag(tag_name=tag_name, users=users, proxy_host=proxy_host) session.add(new_tag) session.commit() if dbs: for db in dbs: session.add(DBTag(db_id=int(db), tag_id=new_tag.id)) if servers: for server in servers: session.add(ServerTag(server_id=int(server), tag_id=new_tag.id)) self.write(dict(code=0, msg='添加成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) db_id = data.get('id', None) db_code = data.get('db_code', None) db_host = data.get('db_host', None) db_port = data.get('db_port', 3306) db_user = data.get('db_user', None) db_pwd = data.get('db_pwd', None) db_env = data.get('db_env', None) proxy_host = data.get('proxy_host', None) db_type = data.get('db_type', 'mysql') db_mark = data.get('db_mark', '写') tag_list = data.get('tag_list', []) db_detail = data.get('db_detail', None) if not db_id or not db_code or not db_host or not db_port or not db_user or not db_pwd or not db_env or not db_detail: return self.write(dict(code=-1, msg='关键参数不能为空')) with DBContext('w', None, True) as session: all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() session.query(DBTag).filter(DBTag.db_id == db_id).delete(synchronize_session=False) if all_tags: for tag_id in all_tags: session.add(DBTag(db_id=int(db_id), tag_id=tag_id[0])) session.query(DB).filter(DB.id == int(db_id)).update({DB.db_code: db_code, DB.db_host: db_host, DB.db_port: db_port, DB.db_user: db_user, DB.db_pwd: db_pwd, DB.db_env: db_env, DB.proxy_host: proxy_host, DB.db_type: db_type, DB.db_mark: db_mark, DB.db_detail: db_detail}) return self.write(dict(code=0, msg='编辑成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) tag_id = data.get('id') users = data.get('users') db_id_list = data.get('dbs') ### ID列表 server_id_list = data.get('servers') ### ID列表 proxy_host = data.get('proxy_host', None) with DBContext('w', None, True) as session: ### session.query(DBTag).filter(DBTag.tag_id == int(tag_id)).delete( synchronize_session=False) session.add_all( [DBTag(db_id=i, tag_id=tag_id) for i in db_id_list]) session.query(ServerTag).filter( ServerTag.tag_id == int(tag_id)).delete( synchronize_session=False) session.add_all([ ServerTag(server_id=i, tag_id=tag_id) for i in server_id_list ]) users = ','.join(users) if users else None session.query(Tag).filter(Tag.id == int(tag_id)).update({ Tag.users: users, Tag.proxy_host: proxy_host }) session.commit() self.write(dict(code=0, msg='修改成功'))
def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) db_code = data.get('db_code', None) db_host = data.get('db_host', None) db_port = data.get('db_port', 3306) db_user = data.get('db_user', None) db_pwd = data.get('db_pwd', None) db_env = data.get('db_env', None) proxy_host = data.get('proxy_host', None) db_type = data.get('db_type', 'mysql') db_mark = data.get('db_mark', '写') tag_list = data.get('tag_list', []) db_detail = data.get('db_detail', None) if not db_code or not db_host or not db_port or not db_user or not db_pwd or not db_env or not db_detail: return self.write(dict(code=-1, msg='关键参数不能为空')) with DBContext('r') as session: exist_id = session.query(DB.id).filter(DB.db_code == db_code, DB.db_host == db_host, DB.db_port == db_port, DB.db_user == db_user, DB.db_env == db_env, DB.proxy_host == proxy_host, DB.db_type == db_type, DB.db_mark == db_mark).first() if exist_id: return self.write(dict(code=-2, msg='不要重复记录')) with DBContext('w', None, True) as session: new_db = DB(db_code=db_code, db_host=db_host, db_port=db_port, db_user=db_user, db_pwd=db_pwd, db_env=db_env, proxy_host=proxy_host, db_type=db_type, db_mark=db_mark, db_detail=db_detail) session.add(new_db) all_tags = session.query(Tag.id).filter(Tag.tag_name.in_(tag_list)).order_by(Tag.id).all() if all_tags: for tag_id in all_tags: session.add(DBTag(db_id=new_db.id, tag_id=tag_id[0])) return self.write(dict(code=0, msg='添加成功'))
def put(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) tag_id = data.get('id') users = data.get('users') db_id_list = data.get('dbs') ### ID列表 server_id_list = data.get('servers') ### ID列表 proxy_host = data.get('proxy_host', None) with DBContext('w', None, True) as session: if db_id_list: db_list, new_db_list, del_db_list = [], [], [] in_db_tags = session.query(DB.id).outerjoin( DBTag, DBTag.db_id == DB.id).filter(DBTag.tag_id == tag_id).all() for i in in_db_tags: i = i[0] db_list.append(i) if i not in db_id_list: del_db_list.append(i) session.query(DBTag).filter(DBTag.db_id == i).delete( synchronize_session=False) for i in db_id_list: if i not in db_list: session.add(DBTag(db_id=int(i), tag_id=tag_id)) new_db_list.append(i) if server_id_list: server_list, new_server_list, del_server_list = [], [], [] in_server_tags = session.query(Server.id).outerjoin( ServerTag, ServerTag.server_id == Server.id).filter( ServerTag.tag_id == tag_id).all() for i in in_server_tags: i = i[0] server_list.append(i) if i not in db_id_list: del_server_list.append(i) session.query(ServerTag).filter( ServerTag.server_id == i).delete( synchronize_session=False) for i in server_id_list: if i not in server_list: session.add(ServerTag(server_id=int(i), tag_id=tag_id)) new_server_list.append(i) if users: users = ','.join(users) else: users = None session.query(Tag).filter(Tag.id == int(tag_id)).update({ Tag.users: users, Tag.proxy_host: proxy_host }) session.commit() self.write(dict(code=0, msg='修改成功'))