def deleteUserInfo(accName): if request.method == 'GET': api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:delete").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: user = User.query.filter(User.accName == accName).first() if user == None: api_response["code"] = 400 api_response["msg"] = "删除失败,账户不存在" return jsonify(api_response), 400 db.session.delete(user) try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) db.session.rollback() return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[删除用户异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def upload(): if request.method == 'POST': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:save").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: files = request.files.getlist("excelFile") for file in files: filename = file.filename if not os.path.exists(UPLOADFILES_DIRS): os.makedirs(UPLOADFILES_DIRS) newFilePath = os.path.join(UPLOADFILES_DIRS, filename) file.save(newFilePath) except Exception as e: Logger('error.log', level='error').logger.error("[上传文件异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 400 api_response["msg"] = "系统异常" return jsonify(api_response) api_response["result"] = filename return jsonify(api_response)
def createEnterprise(): if request.method == 'POST': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:save").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() # if 'etpCode' not in request_json or request_json['etpCode'] is "": # raise ValidationError("参数不能为空") # etpCode = request_json['etpCode'] if 'etpName' not in request_json or request_json['etpName'] is "": raise ValidationError("参数不能为空") etpName = request_json['etpName'] if 'LDAPCode' not in request_json or request_json['LDAPCode'] is "": raise ValidationError("参数不能为空") LDAPCode = request_json['LDAPCode'] etpCode = LDAPCode + "_" + str(int(time.time() * 10)) if 'serverInfolist' not in request_json or request_json['serverInfolist'] is "": raise ValidationError("参数不能为空") serverInfolist = request_json['serverInfolist'] dhcpServerIP = (request_json['dhcpServerIP'] if ('dhcpServerIP' in request_json) else "") TFTPServerIP = (request_json['TFTPServerIP'] if ('TFTPServerIP' in request_json) else "") FTPServerIP = (request_json['FTPServerIP'] if ('FTPServerIP' in request_json) else "") createAdmin = g.user createTime = (request_json['createTime'] if ('createTime' in request_json) else datetime.now()) newenterprise = Enterprise(etpCode, etpName, LDAPCode, dhcpServerIP, TFTPServerIP, FTPServerIP, createAdmin, createTime) db.session.add(newenterprise) for serverinfo in serverInfolist: if 'serverType' not in serverinfo or serverinfo['serverType'] is "": raise ValidationError("参数不能为空") serverType = serverinfo['serverType'] serverIP = (serverinfo['serverIP'] if ('serverIP' in serverinfo) else "") serverUsername = (serverinfo['serverUsername'] if ('serverUsername' in serverinfo) else "") serverPasswd = (serverinfo['serverPasswd'] if ('serverPasswd' in serverinfo) else "") newServer = Server(etpCode, serverType, serverIP, serverUsername, serverPasswd) db.session.add(newServer) try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error("[事务提交失败]accName:【%s】%s" % (g.user, ie)) db.session.rollback() api_response["result"] = etpCode return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[添加企业异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response),500
def editUserPermission(): if request.method == 'POST': api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:update").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() if 'accName' not in request_json or request_json[ 'accName'] is "": raise ValidationError("参数不能为空") accName = request_json['accName'] if 'accType' not in request_json or request_json[ 'accType'] is "": raise ValidationError("参数不能为空") accType = request_json['accType'] user = User.query.filter(User.accName == accName).first() if user == None: api_response["code"] = 400 api_response["msg"] = "所修改账户不存在" return jsonify(api_response), 400 user.role_id = accType try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) db.session.rollback() return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[修改角色异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def getEnterpriseList(): if request.method == 'GET': page = int(request.args.get("page",1)) limit = int(request.args.get("limit",10)) etpCode = request.args.get("etpCode", "") etpName = request.args.get("etpName", "") filters = {1 == 1} api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:list").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: enterpriselist = [] if etpCode is not "": filters.add(Enterprise.etpCode == etpCode) if etpName is not "": filters.add(Enterprise.etpName.like("%" + etpName + "%")) enterpriseList = Enterprise.query.filter(*filters).limit(limit).offset((page - 1) * limit).all() sumList = Enterprise.query.filter(*filters).all() for enterprise in enterpriseList: enterpriselist.append( dict(etpCode=enterprise.etpCode, etpName=enterprise.etpName, LDAPCode=enterprise.LDAPCode, dhcpServerIP=enterprise.dhcpServerIP, TFTPServerIP=enterprise.TFTPServerIP, FTPServerIP=enterprise.FTPServerIP, createAdmin=enterprise.createAdmin, createTime=enterprise.createTime, updateTime=enterprise.updateTime)) result = dict(sumcount=len(sumList), detailcount=len(enterpriseList), list=enterpriselist) api_response['result'] = result return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[查询企业列表异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response),500
def getEnterprise(etpCode): if request.method == 'GET': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:list").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: enterprise = Enterprise.query.filter(Enterprise.etpCode == etpCode).first() serverList = Server.query.filter(Server.etpCode == etpCode).all() if enterprise == None: api_response["code"] = 400 api_response["msg"] = "该组织机构不存在" return jsonify(api_response), 400 else: data = [] serverinfoList = [] if serverList: for server in serverList: serverinfoList.append(dict(etpCode=etpCode, serverType=server.serverType, serverIP=server.serverIP, serverUsername=server.serverUsername, serverPasswd=server.serverPasswd)) data.append(dict(etpCode=enterprise.etpCode, etpName=enterprise.etpName, LDAPCode=enterprise.LDAPCode, dhcpServerIP=enterprise.dhcpServerIP, TFTPServerIP=enterprise.TFTPServerIP, FTPServerIP=enterprise.FTPServerIP, createAdmin=enterprise.createAdmin, createTime=enterprise.createTime, updateTime=enterprise.updateTime, serverInfolist=serverinfoList)) response = dict(list=data) api_response['result'] = response return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[查询企业信息异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def getCurrentUserInfo(): if request.method == 'GET': api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:info").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: data = [] user = User.query.filter(User.accName == g.user).first() if user == None: api_response["code"] = 400 api_response["msg"] = "该账户不存在" return jsonify(api_response), 400 data.append( dict(accName=user.accName, userID=user.userID, accType=user.role_id, userName=user.userName, accAttr=user.accAttr, etpName=user.etpName, userDP=user.userDP, userMail=user.userMail, userPhone=user.userPhone, userTel=user.userTel)) response = dict(list=data) api_response['result'] = response return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[获取当前用户信息异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def deleteBatch(): if request.method == 'POST': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:delete").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() etpCodeList = (request_json['etpCodeList'] if ('etpCodeList' in request_json) else "") for etpCode in etpCodeList: enter = Enterprise.query.filter(Enterprise.etpCode==etpCode).first() server = Server.query.filter(Server.etpCode == etpCode).first() if enter == None: api_response["code"] = 400 api_response["msg"] = "该组织机构不存在" return jsonify(api_response), 400 if server != None: Server.query.filter(Server.etpCode == etpCode).delete() db.session.delete(enter) try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error("[事务提交失败]accName:【%s】%s" % (g.user, ie)) db.session.rollback() return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[批量删除企业异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response),500
def downLoad(fileName): if request.method == 'GET': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:list").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: files = os.path.join(UPLOADFILES_DIRS, fileName) if os.path.exists(files): # 流式读取 def send_file(): store_path = files with open(store_path, 'rb') as targetfile: while 1: data = targetfile.read(20 * 1024 * 1024) # 每次读取20M if not data: break yield data response = Response(send_file(), content_type='application/octet-stream') response.headers["Content-disposition"] = 'attachment; filename=%s' % fileName # 如果不加上这行代码,导致下图的问题 return response api_response["code"] = 400 api_response["msg"] = "文件不存在!" return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[下载文件异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 400 api_response["msg"] = "系统异常" return jsonify(api_response)
def editEnterprise(): if request.method == 'POST': api_response = { "code": 0, "msg": "success" } try: currResource = Resource.query.filter(Resource.perms == "modules:enterprise:update").first() if currResource==None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() if 'etpCode' not in request_json or request_json['etpCode'] is "": raise ValidationError("参数不能为空") etpCode = request_json['etpCode'] enterprise = Enterprise.query.filter(Enterprise.etpCode == etpCode).first() if enterprise == None: api_response["code"] = 400 api_response["msg"] = "该组织机构不存在" return jsonify(api_response), 400 updateTime = (request_json['updateTime'] if ('updateTime' in request_json) else datetime.now()) enterprise.updateTime = updateTime if ('dhcpServerIP' in request_json): enterprise.dhcpServerIP = request_json['dhcpServerIP'] if ('TFTPServerIP' in request_json): enterprise.TFTPServerIP = request_json['TFTPServerIP'] if ('FTPServerIP' in request_json): enterprise.FTPServerIP = request_json['FTPServerIP'] if ('serverInfolist' in request_json): serverInfoList = request_json['serverInfolist'] for server in serverInfoList: serverType = (server['serverType'] if ('serverType' in server) else "") serverInfo = Server.query.filter(Server.etpCode == etpCode,Server.serverType == serverType).first() if serverInfo==None: api_response["code"] = 400 api_response["msg"] = "该服务器信息不存在" return jsonify(api_response), 400 if ('serverIP' in server): serverInfo.serverIP = server['serverIP'] if ('serverUsername' in server): serverInfo.serverUsername = server['serverUsername'] if ('serverPasswd' in server): serverInfo.serverPasswd = server['serverPasswd'] try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error("[事务提交失败]accName:【%s】%s" % (g.user, ie)) db.session.rollback() api_response["result"] = etpCode return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error("[修改企业信息异常]accName:【%s】%s" % (g.user, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response)
def editUser(): if request.method == 'POST': api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:update").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() if 'accName' not in request_json or request_json[ 'accName'] is "": raise ValidationError("参数不能为空") accName = request_json['accName'] # if 'userID' not in request_json or request_json['userID'] is "": # raise ValidationError("参数不能为空") # userID = request_json['userID'] # if 'userName' not in request_json or request_json['userName'] is "": # raise ValidationError("参数不能为空") # userName = request_json['userName'] # if 'accAttr' not in request_json or request_json['accAttr'] is "": # raise ValidationError("参数不能为空") # accAttr = request_json['accAttr'] # if 'etpName' not in request_json or request_json['etpName'] is "": # raise ValidationError("参数不能为空") # etpName = request_json['etpName'] # if 'userDP' not in request_json or request_json['userDP'] is "": # raise ValidationError("参数不能为空") # userDP = request_json['userDP'] if 'userMail' not in request_json or request_json[ 'userMail'] is "": raise ValidationError("参数不能为空") userMail = request_json['userMail'] if 'userPhone' not in request_json or request_json[ 'userPhone'] is "": raise ValidationError("参数不能为空") userPhone = request_json['userPhone'] if 'userTel' not in request_json or request_json[ 'userTel'] is "": raise ValidationError("参数不能为空") userTel = request_json['userTel'] # user = User.query.filter(User.accName == accName,User.userID == userID,User.userName == userName, # User.accAttr == accAttr,User.etpName == etpName,User.userDP == userDP).first() user = User.query.filter(User.accName == accName).first() if user == None: api_response["code"] = 400 api_response["msg"] = "该账户不存在" return jsonify(api_response), 400 if ('accType' in request_json): user.role_id = request_json['accType'] user.userMail = userMail user.userPhone = userPhone user.userTel = userTel if ('password' in request_json): user.hash_pwd(request_json['password']) if ('status' in request_json): user.status = request_json['status'] if ('remarks' in request_json): user.remarks = request_json['remarks'] try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) db.session.rollback() return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[修改用户信息异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def createUser(): if request.method == 'POST': api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:save").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: request_json = request.get_json() if 'accName' not in request_json or request_json[ 'accName'] is "": raise ValidationError("参数不能为空") accName = request_json['accName'] if 'password' not in request_json or request_json[ 'password'] is "": raise ValidationError("参数不能为空") password = request_json['password'] if 'accType' not in request_json or request_json[ 'accType'] is "": raise ValidationError("参数不能为空") accType = request_json['accType'] if 'userID' not in request_json or request_json['userID'] is "": raise ValidationError("参数不能为空") userID = request_json['userID'] if 'userName' not in request_json or request_json[ 'userName'] is "": raise ValidationError("参数不能为空") userName = request_json['userName'] if 'accAttr' not in request_json or request_json[ 'accAttr'] is "": raise ValidationError("参数不能为空") accAttr = request_json['accAttr'] if 'etpName' not in request_json or request_json[ 'etpName'] is "": raise ValidationError("参数不能为空") etpName = request_json['etpName'] if 'userDP' not in request_json or request_json['userDP'] is "": raise ValidationError("参数不能为空") userDP = request_json['userDP'] if 'userMail' not in request_json or request_json[ 'userMail'] is "": raise ValidationError("参数不能为空") userMail = request_json['userMail'] if 'userPhone' not in request_json or request_json[ 'userPhone'] is "": raise ValidationError("参数不能为空") userPhone = request_json['userPhone'] if 'userTel' not in request_json or request_json[ 'userTel'] is "": raise ValidationError("参数不能为空") userTel = request_json['userTel'] status = (request_json['status'] if ('status' in request_json) else 1) create_user_id = g.user create_date = (request_json['create_date'] if ('create_date' in request_json) else datetime.now()) remarks = (request_json['remarks'] if ('remarks' in request_json) else "") newUser = User(accName, userID, userName, userMail, userPhone, userTel, password, status, accType, accAttr, etpName, userDP, create_date, create_user_id, remarks) newUser.hash_pwd(password) db.session.add(newUser) try: db.session.commit() except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) db.session.rollback() return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[添加用户异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500
def getUserList(): if request.method == 'GET': page = int(request.args.get("page", 1)) limit = int(request.args.get("limit", 10)) accName = request.args.get("accName", "") etpCode = request.args.get("etpCode", "") etpName = request.args.get("etpName", "") accType = request.args.get("accType", "") filters = {User.status == 1} api_response = {"code": 0, "msg": "success"} try: currResource = Resource.query.filter( Resource.perms == "modules:usermanage:list").first() if currResource == None: api_response["code"] = 401 api_response["msg"] = "当前操作权限实体不存在" return jsonify(api_response), 401 very = very_permission(currResource.id) if very == False: api_response["code"] = 401 api_response["msg"] = "该账户无操作权限" return jsonify(api_response), 401 else: userList = [] if accName is not "": filters.add(User.accName.like("%" + accName + "%")) if etpCode is not "": filters.add(User.accAttr == etpCode) if etpName is not "": filters.add(User.etpName.like("%" + etpName + "%")) if accType is not "": filters.add(User.role_id == int(accType)) userlist = User.query.filter(*filters).limit(limit).offset( (page - 1) * limit).all() sumList = User.query.filter(*filters).all() for user in userlist: last_login = Last_Online.query.filter( Last_Online.accName == user.accName).first() if last_login is None: lastLogintime = None else: lastLogintime = last_login.last_login_time userList.append( dict(accName=user.accName, userName=user.userName, accType=user.role_id, userDP=user.userDP, etpName=user.etpName, createTime=user.create_date, lastLogintime=lastLogintime)) result = dict(sumcount=len(sumList), detailcount=len(userlist), list=userList) api_response['result'] = result return jsonify(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[获取用户列表异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return jsonify(api_response), 500