def insertAuditTrace(Operation, DeitalMSG, TableName, User, Other): ''' 审计追踪记录 :param Operation: 操作 :param DeitalMSG: 详细信息 :param ReviseDate: 修改日期 :param TableName: 操作表 :param User: 用户 :param Other: 其他 :return: ''' try: if Operation == None: Operation = "" if DeitalMSG == None: DeitalMSG = "" if TableName == None: TableName = "" if User == None: User = "" if Other == None: Other = "" db_session.add( AuditTrace(Operation=Operation, DeitalMSG=DeitalMSG, ReviseDate=datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S'), User=User, Other=Other)) db_session.commit() except Exception as e: db_session.rollback() print(e) logger.error(e)
def delete(data): ''' :param data: 要删除的数据 :return: ''' try: tableName = str(data.get("tableName")) jsonstr = json.dumps(data.to_dict()) if len(jsonstr) > 10: jstr = data.get("delete_data") jsonnumber = re.findall(r"\d+\.?\d*", jstr) for key in jsonnumber: try: sql = "delete from " + "" + tableName + " where ID = " + str(key) db_session.execute(sql) aud = AuditTrace() aud.TableName = tableName aud.Operation = current_user.Name + " 对表" + tableName + "中的ID为" + key + "的数据做了删除操作!" aud.DeitalMSG = "用户:" + current_user.Name + " 对表" + tableName + "中的ID为" + key + "的数据做了删除操作!" aud.ReviseDate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') aud.User = current_user.Name db_session.add(aud) db_session.commit() db_session.close() except Exception as ee: print(ee) db_session.rollback() insertSyslog("error", "删除户ID为" + str(id) + "报错Error:" + str(ee), current_user.Name) return {"code": "500", "message": "请求错误", "data": "删除户ID为" + str(id) + "报错Error:" + str(ee)} return {"code": "200", "message": "删除成功"} except Exception as e: db_session.rollback() logger.error(e) insertSyslog("error", "%s数据删除报错:" % tableName + str(e), current_user.Name) return {"code": "500", "message": "请求错误", "data": "%s数据删除报错:" % tableName + str(e)}
def update(data): ''' :param data: 更新数据 :return: ''' if isinstance(data, dict) and len(data) > 0: try: tableName = str(data.get("tableName")) obj = Base.classes.get(tableName.lower()) ss = obj() ID = data.get('ID') oclass = db_session.query(obj).filter_by( ID=int(data.get('ID'))).first() if oclass: for key in data: if hasattr( oclass, key ) and key != 'ID' and key != 'tableName' and key != "id" and key != "Creater": if key == "Password": setattr(oclass, key, generate_password_hash(data['Password'])) elif key == "WorkNumber": ocal = db_session.query(User).filter( User.WorkNumber == data['WorkNumber']).first() if ocal != None: if oclass.WorkNumber != data['WorkNumber']: return "工号重复,请重新录入!" else: setattr(oclass, key, data['WorkNumber']) else: setattr(oclass, key, data[key]) db_session.add(oclass) aud = AuditTrace() aud.TableName = tableName aud.Operation = current_user.Name + " 对表" + tableName + "的数据做了更新操作!" aud.DeitalMSG = "用户:" + current_user.Name + " 对表" + tableName + "ID为:" + ID + "做了更新操作" aud.ReviseDate = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') aud.User = current_user.Name db_session.add(aud) db_session.commit() db_session.close() return {"code": "200", "message": "修改成功"} else: return {"code": "200", "message": "修改成功", "data": "当前记录不存在"} except Exception as e: db_session.rollback() logger.error(e) insertSyslog("error", "%s数据更新报错:" % tableName + str(e), current_user.Name) return { "code": "500", "message": "请求错误", "data": "%s数据更新报错:" % tableName + str(e) }
def insert(data): ''' :param data: 需要添加的数据 :return: ''' if isinstance(data, dict) and len(data) > 0: try: tableName = str(data.get("tableName")) obj = Base.classes.get(tableName.lower()) ss = obj() for key in data: if key != "ID" and key != "tableName" and key != "id": if key == "Password": setattr(ss, key, generate_password_hash(data['Password'])) if tableName == "User": setattr(ss, "Creater", current_user.Name) elif key == "WorkNumber": ocal = db_session.query(User).filter( User.WorkNumber == data['WorkNumber']).first() if ocal != None: return "工号重复,请重新录入!" else: setattr(ss, key, data['WorkNumber']) else: setattr(ss, key, data[key]) db_session.add(ss) aud = AuditTrace() aud.TableName = tableName aud.Operation = current_user.Name + " 对表" + tableName + "添加一条数据!" aud.DeitalMSG = "用户:" + current_user.Name + " 对表" + tableName + "添加一条数据!" aud.ReviseDate = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S') aud.User = current_user.Name db_session.add(aud) db_session.commit() db_session.close() return {"code": "200", "message": "添加成功"} except Exception as e: print(e) db_session.rollback() logger.error(e) insertSyslog("error", "%s数据添加报错:" % tableName + str(e), current_user.Name) return { "code": "500", "message": "请求错误", "data": "%s数据添加报错:" % tableName + str(e) }