def count_by_user(self, user=User): # sql = 'select count(h.id) from host as h ' \ # 'left join user_host_relation as uhr on h.id = uhr.host_id ' \ # 'left join user as u on u.id = uhr.user_id ' \ # 'where u.id = :id' # print(db.engine.execute( # text(sql), {'id': user.id} # )) # return db.engine.execute( # text(sql), {'id': user.id} # ) return Host().query.filter( (Host.users.any(User.id == user.id))).count()
def create_modfiy_copy_delete(host_id=int): type = request.args.get('type') method = request.args.get('method') form = HostForm() host = HostService().find_one(id=host_id) if (host_id <= 0) or host is None: title = '新建主机' else: # 回显表单数据 BuildModelToFrom(host=host, form=form) if (host_id > 0 and type is None): title = '修改主机' else: title = '复制主机' if form.validate_on_submit(): host = Host() host.hostname = request.form.get('hostname') host.active = True host.username = request.form.get('username') host.password = request.form.get('security_password') host.command = request.form.get('command') host.command_start = request.form.get('command_start') host.command_stop = request.form.get('command_stop') host.command_restart = request.form.get('command_restart') host.server_name = request.form.get('server_name') host.server_type = request.form.get('server_type') host.server = request.form.get('server') host.users = [current_user] host.ssh_port = request.form.get('ssh_port') host.key = form.security_key.data if form.submit.data: if method == 'PUT': host.id = host_id if request.form.get('security') == '1': host.key = None else: host.password = None logger.info( 'execute update operation type <%s> primary key <%s>', logger_type, host_id) if HostService().update_one(host): return redirect(url_for('host_view.index')) elif request.method == 'POST': if HostService().add(host): return redirect(url_for('host_view.index')) if form.test_connection.data: ssh_connect = Ssh(hostname=host.hostname, port=host.ssh_port, username=host.username, password=host.password, private_key=host.key) if ssh_connect.check_connect() is False: flash('用户 <{}> 连接主机 <{}> 失败,错误如下:\r\n{}'.format( host.hostname, host.username, ssh_connect.get_message())) else: flash('用户 <{}> 连接主机 <{}> 成功!'.format(host.hostname, host.username)) if StringUtils().is_not_empty(method): return redirect( url_for('host_view.create_modfiy_copy_delete', host_id=host_id, method=method)) else: return redirect( url_for('host_view.create_modfiy_copy_delete', host_id=host_id)) return render_template('host/host.html', form=form, host=host, title=title, active_menu='host')
def find_all_order_by_create_time_desc_and_user(self, user=User): return Host().query.filter( (Host.users.any(User.id == user.id))).order_by( desc(Host.create_time)).all()
def count(self): return Host().query.count()
def find_all_order_by_create_time_desc(self): return Host().query.order_by(desc(Host.create_time)).all()
def find_by_email(self, email): return Host().query.filter_by(email=email).first()
def find_one(self, id=int): return Host().query.filter_by(id=id).first()
def find_all(self): """ 查询所有数据 :return: 数据集合 """ return Host().query.all()
def delete_one(self, id=int): host = Host().query.filter_by(id=id).first() if host is not None: db.session.delete(host) db.session.commit()