def change_network(net_id): """修改网络设备信息""" net = NetworkAsset.query.get(net_id) if net is None: return jsonify(errno=RET.PARAMERR, errmsg=u'错误的id!!') param_dict = request.json ip = param_dict.get("ip") hostname = param_dict.get("hostname") manufacturer = param_dict.get("manufacturer") productname = param_dict.get("productname") service_tag = param_dict.get("service_tag") idc_jg = param_dict.get("idc_jg") status = param_dict.get("status") remark = param_dict.get("remark") net.ip = ip net.hostname = hostname net.productname = productname net.manufacturer = manufacturer net.idc_jg = idc_jg net.service_tag = service_tag net.status = status net.remark = remark commit(net) return jsonify(errno=RET.OK, errmsg="OK")
def registered(): param_dict = request.json name = param_dict.get("name") password = param_dict.get("password") # 2.1 非空判断 if not all([name, password]): current_app.logger.error("参数不足") return jsonify(errno=RET.PARAMERR, errmsg="参数不足") try: user = Users.query.filter(Users.username == name).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="操作失败!") else: if user: return jsonify(errno=RET.USERERR, errmsg="用户已经存在!") user = Users() user.username = name # TODO: 密码加密 user.set_password_hash(password) user.last_login = datetime.now() commit(user) session["user_id"] = user.id session["name"] = user.username return jsonify(errno=RET.OK, errmsg="注册成功")
def host_change(host_id): """修改host""" host = HostList.query.get(host_id) if host is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') param_dict = request.json ip = param_dict.get("ip") ip1 = param_dict.get("ip1") product = param_dict.get("product") application = param_dict.get("application") idc_jg = param_dict.get("idc_jg") status = param_dict.get("status") remark = param_dict.get("remark") host.ip = ip host.ip1 = ip1 host.product = product host.application = application host.idc_jg = idc_jg host.status = status host.remark = remark commit(host) return jsonify(errno=RET.OK, errmsg="OK")
def system_add(): param_dict = request.json ip = param_dict.get("ip") hostname = param_dict.get("hostname") macaddress = param_dict.get("macaddress") systemVersion = param_dict.get("systemVersion") status = param_dict.get("status") remark = param_dict.get("remark") system = SystemInstall.query.filter( SystemInstall.macaddress == macaddress).first() if system: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!') system_param = { "ip": ip, "hostname": hostname, "macaddress": macaddress, "systemVersion": systemVersion, "status": status, "remark": remark, } system_info = SystemInstall(**system_param) commit(system_info) return jsonify(errno=RET.OK, errmsg="OK")
def add_fault(): """新增故障""" param_dict = request.json title = param_dict.get("title") level = param_dict.get("level") typeinfo = param_dict.get("typeinfo") project = param_dict.get("project") status = param_dict.get("status") faulttime = param_dict.get("faulttime") improve = param_dict.get("improve") effect = param_dict.get("effect") content = param_dict.get("content") solution = param_dict.get("solution") fault_param = { "title": title, "level": level, "typeinfo": typeinfo, "project": project, "faulttime": faulttime, "status": status, "improve": improve, "effect": effect, "content": content, "solution": solution, } fault_info = FaultContent(**fault_param) commit(fault_info) return jsonify(errno=RET.OK, errmsg="OK")
def add_host(): """新增主机""" param_dict = request.json ip = param_dict.get("ip") ip1 = param_dict.get("ip1") product = param_dict.get("product") application = param_dict.get("application") idc_jg = param_dict.get("idc_jg") status = param_dict.get("status") remark = param_dict.get("remark") if not ip: return jsonify(errno=RET.PARAMERR, errmsg=u'请输入IP值!') host = HostList.query.filter_by(ip=ip).first() if host: return jsonify(errno=RET.PARAMERR, errmsg="ip值已经存在!") host_param = { "ip": ip, "ip1": ip1, "product": product, "application": application, "idc_jg": idc_jg, "status": status, "remark": remark, } host_info = HostList(**host_param) commit(host_info) return jsonify(errno=RET.OK, errmsg="OK")
def change_idc(idc_id): """修改""" param_dict = request.json idc_name = param_dict.get("idc_name") idc_type = param_dict.get("idc_type") idc_location = param_dict.get("idc_location") contract_date = param_dict.get("contract_date") idc_contacts = param_dict.get("idc_contacts") status = param_dict.get("status") remark = param_dict.get("remark") idc = IdcAsset.query.get(idc_id) if idc is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') idc.idc_name = idc_name idc.idc_type = idc_type idc.idc_location = idc_location idc.contract_date = contract_date idc.idc_contacts = idc_contacts idc.status = status idc.remark = remark commit(idc) return jsonify(errno=RET.OK, errmsg="OK")
def add_idc(): param_dict = request.json idc_name = param_dict.get("idc_name") idc_type = param_dict.get("idc_type") idc_location = param_dict.get("idc_location") contract_date = param_dict.get("contract_date") idc_contacts = param_dict.get("idc_contacts") status = param_dict.get("status") remark = param_dict.get("remark") idc_param = { "idc_name": idc_name, "idc_type": idc_type, "idc_location": idc_location, "contract_date": contract_date, "idc_contacts": idc_contacts, "status": status, "remark": remark, } idc_info = IdcAsset(**idc_param) commit(idc_info) return jsonify(errno=RET.OK, errmsg="OK")
def delete_idc(idc_id): """逻辑删除host""" host = IdcAsset.query.get(idc_id) if host is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') host.status = -1 commit(host) return jsonify(errno=RET.OK, errmsg="OK")
def system_delete(system_id): """逻辑删除host""" system = SystemInstall.query.get(system_id) if system is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') system.status = -1 commit(system) return jsonify(errno=RET.OK, errmsg="OK")
def delete_network(net_id): """逻辑删除网络设备""" net = NetworkAsset.query.get(net_id) if net is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') net.status = -1 commit(net) return jsonify(errno=RET.OK, errmsg="OK")
def type_delete(type_id): """逻辑删除host""" host = TypeInfo.query.get(type_id) if host is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') host.status = -1 commit(host) return jsonify(errno=RET.OK, errmsg="OK")
def record_delete(record_id): """逻辑删除host""" record = InstallRecord.query.get(record_id) if record is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') record.status = -1 commit(record) return jsonify(errno=RET.OK, errmsg="OK")
def type_add(): """添加类型""" param_dict = request.json faulttype = param_dict.get("faulttype") typeinfo = TypeInfo.query.filter(TypeInfo.faulttype == faulttype).first() if typeinfo: return jsonify(errno=RET.DATAERR, errmsg=u'记录已存在!') typeinfo = TypeInfo(faulttype=faulttype) commit(typeinfo) return jsonify(errno=RET.OK, errmsg="OK")
def record_add(): param_dict = request.json ip = param_dict.get("ip") systemVersion = param_dict.get("systemVersion") status = param_dict.get("status") remark = param_dict.get("remark") record_param = { "ip": ip, "systemVersion": systemVersion, "status": status, "remark": remark, } record_info = InstallRecord(**record_param) commit(record_info) return jsonify(errno=RET.OK, errmsg="OK")
def record_change(record_id): record = InstallRecord.query.get(record_id) if record is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') param_dict = request.json ip = param_dict.get("ip") systemVersion = param_dict.get("systemVersion") status = param_dict.get("status") remark = param_dict.get("remark") record.ip = ip record.systemVersion = systemVersion record.status = status record.remark = remark commit(record) return jsonify(errno=RET.OK, errmsg="OK")
def change_password(): """修改密码后端接口""" # 当前登录的用户对象, old_password:旧密码, new_password:新密码 user = g.user # type:Users if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") old_password = request.json.get("old_password") new_password = request.json.get("new_password") if not all([old_password, new_password]): return jsonify(errno=RET.PARAMERR, errmsg="参数不足") if not user.check_password(old_password): return jsonify(errno=RET.DATAERR, errmsg="旧密码填写错误") user.set_password_hash(new_password) commit(user) return jsonify(errno=RET.OK, errmsg="修改密码成功")
def system_change(system_id): system = SystemInstall.query.get(system_id) if system is None: return jsonify(errno=RET.PARAMERR, errmsg=u'操作错误!!') param_dict = request.json ip = param_dict.get("ip") hostname = param_dict.get("hostname") macaddress = param_dict.get("macaddress") systemVersion = param_dict.get("systemVersion") status = param_dict.get("status") remark = param_dict.get("remark") system.ip = ip system.systemVersion = systemVersion system.hostname = hostname system.macaddress = macaddress system.status = status system.remark = remark commit(system) return jsonify(errno=RET.OK, errmsg="OK")
def login(): """登录后端接口""" if request.method == "GET": return render_template("login.html") param_dict = request.json name = param_dict.get("name") password = param_dict.get("password") if not all([name, password]): current_app.logger.error("参数不足") return jsonify(errno=RET.PARAMERR, errmsg="参数不足") try: user = Users.query.filter(Users.username == name).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="操作失败!") if not user: return jsonify(errno=RET.USERERR, errmsg="用户不存在") # 存在:根据用户对象,校验密码是否正确 if user.check_password(password) is False: return jsonify(errno=RET.DATAERR, errmsg="密码错误") # 3.2 更新最后一次登录时间 [将修改操作保存回数据] # 注意配置数据库字段:SQLALCHEMY_COMMIT_ON_TEARDOWN = True 自动提交数据db.session.commit() user.last_login = datetime.now() commit(user) session["user_id"] = user.id session["name"] = user.username return jsonify(errno=RET.OK, errmsg="登录成功")
def add_network(): """添加设备""" param_dict = request.json ip = param_dict.get("ip") hostname = param_dict.get("hostname") manufacturer = param_dict.get("manufacturer") productname = param_dict.get("productname") service_tag = param_dict.get("service_tag") idc_jg = param_dict.get("idc_jg") status = param_dict.get("status") remark = param_dict.get("remark") if not ip: return jsonify(errno=RET.PARAMERR, errmsg=u'请输入IP值!') network = NetworkAsset.query.filter_by(ip=ip).first() if network: return jsonify(errno=RET.PARAMERR, errmsg="ip值已经存在!") network_param = { "ip": ip, "hostname": hostname, "manufacturer": manufacturer, "productname": productname, "service_tag": service_tag, "idc_jg": idc_jg, "status": status, "remark": remark, } network_info = NetworkAsset(**network_param) commit(network_info) return jsonify(errno=RET.OK, errmsg="OK")