def host_edit(id=None): form = HostForm() host = Host.query.get_or_404(id) if request.method == "GET": form.status.data = host.status if form.validate_on_submit(): data = form.data host_num = Host.query.filter_by(name=data["host_name"]).count() if host.name != data["host_name"] and host_num == 1: flash("主机名已经存在!", "err") return redirect(url_for("admin.host_edit", id=id)) outernetip_num = Host.query.filter_by( outernet_ip=data["outernet_ip"]).count() if host.outernet_ip != data["outernet_ip"] and outernetip_num == 1: flash("外网IP已经存在!", "err") return redirect(url_for("admin.host_edit", id=id)) host.name = data["host_name"] host.system = data["system"] host.outernet_ip = data["outernet_ip"] host.intranet_ip = data["intranet_ip"] host.cpu = data["cpu"] host.memory = data["memory"] host.disk = data["disk"] host.username = data["username"] host.password = data["password"] host.port = data["port"] host.ssh_port = data["ssh_port"] host.status = data["status"] db.session.add(host) db.session.commit() flash("修改主机成功!", "ok") return redirect(url_for("admin.host_list", page=1)) return render_template("admin/host_edit.html", form=form, host=host)
def host_add(): form = HostForm() if form.validate_on_submit(): data = form.data name_count = Host.query.filter_by(name=data["host_name"]).count() if name_count == 1: flash("主机名已经存在!", "err") return redirect(url_for("admin.host_add")) outernetip_num = Host.query.filter_by( outernet_ip=data["outernet_ip"]).count() if outernetip_num == 1: flash("外网IP已经存在!", "err") return redirect(url_for("admin.host_add")) host = Host( name=data["host_name"], system=data["system"], outernet_ip=data["outernet_ip"], intranet_ip=data["intranet_ip"], cpu=data["cpu"], memory=data["memory"], disk=data["disk"], username=data["username"], password=data["password"], port=data["port"], ssh_port=data["ssh_port"], status=data["status"], ) db.session.add(host) db.session.commit() flash("添加主机成功", "ok") return redirect(url_for("admin.host_list", page=1)) return render_template("admin/host_add.html", form=form)