def PermissionsSave(): if request.method == 'POST': data = request.values try: datastr = json.loads(data.get("data")) #删除之前的权限 perss = db_session.query(Permission).filter(Permission.WorkNumber == datastr[0].get("WorkNumber")).all() for pe in perss: db_session.delete(pe) db_session.commit() for i in datastr: per = Permission() per.MenuName = i.get("MenuName") per.MenuType = i.get("MenuType") per.MenuCode = i.get("MenuCode") per.Name = i.get("Name") per.WorkNumber = i.get("WorkNumber") db_session.add(per) db_session.commit() return 'OK' except Exception as e: db_session.rollback() print(e) logger.error(e) insertSyslog("error", "添加用户报错Error:" + str(e), current_user.Name) return json.dumps([{"status": "Error:" + str(e)}], cls=AlchemyEncoder, ensure_ascii=False)
def delete(tablename, delete_data): ''' :param tablename: 要进行删除信息的model :param recv_data: 要进行更新的数据,数据类型为list,list中的每个元素为需要删除的每条记录的ID :return: 返回json信息,包含status,message ''' if hasattr(tablename, '__tablename__'): try: jsonstr = json.dumps(delete_data.to_dict()) if len(jsonstr) > 10: jsonnumber = re.findall(r"\d+\.?\d*", jsonstr) for key in jsonnumber: id = int(key) try: oclass = db_session.query(tablename).filter_by(ID=id).first() db_session.delete(oclass) db_session.commit() except Exception as ee: db_session.rollback() insertSyslog("error", "删除户ID为"+str(id)+"报错Error:" + str(ee), current_user.Name) return json.dumps("删除用户报错", cls=AlchemyEncoder,ensure_ascii=False) return 'OK' except Exception as e: db_session.rollback() logger.error(e) insertSyslog("error", "%s数据删除报错:"%tablename + str(e), current_user.Name) return json.dumps('数据删除失败!')
def menuToUser(): if request.method == 'GET': data = request.values # 返回请求中的参数和form try: # 获取菜单和用户并存入数据库 role_id = data['role_id'] # 获取角色ID if role_id is None: return menus = db_session.query(Menu).join(Role_Menu, isouter=True).filter_by(Role_ID=id).all() if menus: db_session.delete(menus) db_session.commit() menu_id = data['menu_id'] # 获取菜单ID if menu_id is None: return menu_id = re.findall(r'\d+\.?\d*', menu_id) for r in menu_id: role = db_session.query(Role).filter_by(ID=role_id).first() menu = db_session.query(Menu).filter_by(ID=r).first() # 将菜单ID和角色ID存入User_Role menu.roles.append(role) db_session.add(menu) db_session.commit() # 存入数据库后跳转到权限分配页面 return redirect(url_for("roleright")) except Exception as e: print(e) logger.error(e) insertSyslog("error", "权限分配下为角色添加权限Error:" + str(e), current_user.Name) return json.dumps([{"status": "Error:" + str(e)}], cls=AlchemyEncoder, ensure_ascii=False)
def delete_role(): rid = request.headers.get('rid') role = db_session.query(Role).filter(Role.ID == rid).first() user_query = db_session.query(RoleUser).filter( RoleUser.RoleName == role.RoleName).all() for item in user_query: db_session.delete(item) db_session.delete(role) db_session.commit() return json.dumps({'code': 10004, 'msg': '删除成功'})
def repair_tasks(p): """ 更新维修任务表的状态 :param p: 更改工单状态 :return: """ if p == 'jiedan': json_data = request.values data = db_session.query(Repair).filter_by( No=json_data.get('No')).first() data.Status = '维修中' data.ReceiveWorker = current_user.Name data.ReceiveTime = json_data.get('Time') equipment = db_session.query(Equipment).filter_by( EquipmentCode=json_data.get('EquipmentCode')).first() equipment.Status = '维修中' db_session.add_all([data, equipment]) db_session.commit() db_session.close() return json.dumps({ 'code': '10001', 'message': '操作成功' }, cls=AlchemyEncoder, ensure_ascii=False) if p == 'over': json_data = request.values data = db_session.query(Repair).filter_by( No=json_data.get('No')).first() task_data = RepairTask(EquipmentCode=data.EquipmentCode, No=data.No, Status='维修完成', Worker=data.Worker, ReceiveWorker=data.ReceiveWorker, Content=json_data.get('Content'), ApplyTime=data.ApplyTime, ReceiveTime=data.ReceiveTime, EndTime=json_data.get('EndTime')) equipment = db_session.query(Equipment).filter_by( EquipmentCode=json_data.get('EquipmentCode')).first() equipment.Status = '运行中' db_session.add_all([task_data, equipment]) db_session.delete(data) db_session.commit() db_session.close() return json.dumps({ 'code': '10001', 'message': '操作成功' }, cls=AlchemyEncoder, ensure_ascii=False)
def delete_department(): code = request.headers.get('department_code') department = db_session.query(DepartmentManager).filter( DepartmentManager.DepartCode == code).first() role_query = db_session.query(Role).filter( Role.ParentNode == department.DepartCode).all() for item in role_query: item.ParentNode = '' db_session.commit() user_query = db_session.query(User).filter( User.OrganizationName == department.DepartName).all() for item in user_query: item.OrganizationName = '' db_session.commit() db_session.delete(department) db_session.commit() return json.dumps({'code': 10001, 'msg': '删除成功'})
def deleterole(): ''' 删除角色 :return: ''' try: if request.method == 'POST': data = request.values ID = data.get("ID") role = db_session.query(Role).filter(Role.ID == ID).first() db_session.delete(role) db_session.commit() except Exception as e: print(e) db_session.rollback() logger.error(e) return json.dumps([{"status": "Error:" + str(e)}], cls=AlchemyEncoder, ensure_ascii=False)
def deleteUser(): if request.method == 'POST': data = request.values try: jsonstr = json.dumps(data.to_dict()) if len(jsonstr) > 10: jsonnumber = re.findall(r"\d+\.?\d*", jsonstr) for key in jsonnumber: id = int(key) try: oclass = db_session.query(User).filter_by(id=id).first() db_session.delete(oclass) db_session.commit() except Exception as ee: db_session.rollback() print(ee) insertSyslog("error", "删除户ID为"+string(id)+"报错Error:" + string(ee), current_user.Name) return json.dumps("删除用户报错", cls=AlchemyEncoder,ensure_ascii=False) return 'OK' except Exception as e: print(e) logger.error(e) insertSyslog("error", "删除用户报错Error:" + str(e), current_user.Name) return json.dumps([{"status": "Error:" + str(e)}], cls=AlchemyEncoder, ensure_ascii=False)
def keep_tasks(): """保养任务表""" try: query_data = db_session.query(KeepPlan).filter_by(Status='待保养').all() if request.method == 'GET': # 每页多少条 limit = int(request.values.get('limit', '5')) # 当前页 offset = int(request.values.get('offset', '1')) for item in query_data: q = db_session.query(KeepTask).filter_by(No=item.No).first() if not q and get_time_stamp(item.WorkTime): data = KeepTask(EquipmentCode=item.EquipmentCode, No=item.No, Worker=item.Worker, Status=item.Status, ApplyTime=item.ApplyTime, StartTime=item.StartTime, WorkTime=item.WorkTime, WeekTime=item.WeekTime, Type=item.Type) db_session.add(data) db_session.commit() # if item.Type == '单次': # pass # db_session.delete(item) # db_session.commit() data = db_session.query(KeepTask).order_by( KeepTask.ApplyTime.desc()).limit(limit).offset( (offset - 1) * limit) total = db_session.query(KeepTask).count() return json.dumps( { 'code': '10001', 'message': '操作成功', 'data': { 'rows': data.all(), 'total': total } }, cls=AlchemyEncoder, ensure_ascii=False) if request.method == 'POST': json_data = request.values no = json_data.get('No') end_time = json_data.get('EndTime') content = json_data.get('Content') item = db_session.query(KeepTask).filter_by(No=no).first() data = KeepRecord(EquipmentCode=item.EquipmentCode, No=no, Worker=item.Worker, Status='已完成', Type=item.Type, KeepWorker=current_user.Name, ApplyTime=item.ApplyTime, StartTime=item.StartTime, Describe=item.Describe, Content=content, WeekTime=item.WeekTime, EndTime=end_time) db_session.delete(item) db_session.commit() keep_plan = db_session.query(KeepPlan).filter_by(No=no).first() if keep_plan and keep_plan.Type == '周期': keep_plan.WorkTime = add_date(keep_plan.WeekTime, keep_plan.WorkTime) db_session.add_all([data, keep_plan]) db_session.commit() db_session.close() return json.dumps({ 'code': '10001', 'message': '操作成功' }, cls=AlchemyEncoder, ensure_ascii=False) else: db_session.delete(keep_plan) db_session.add(data) db_session.commit() db_session.close() return json.dumps({ 'code': '10001', 'message': '操作成功' }, cls=AlchemyEncoder, ensure_ascii=False) except Exception as e: logger.error(e) insertSyslog("error", "保养任务表修改错误:" + str(e), current_user.Name) return json.dumps({ 'code': '20002', 'message': str(e) }, cls=AlchemyEncoder, ensure_ascii=False)
def schedule_lqt(): """排班日程""" try: if request.method == 'GET': data = db_session.query(Schedulelqt).filter_by( energystrategyCode=request.values.get('code')).all() return json.dumps({ 'code': '20001', 'message': '成功', 'data': data }, cls=AlchemyEncoder, ensure_ascii=False) if request.method == 'POST': new_start = request.values.get('start_time') new_end = request.values.get('end_time') code = request.values.get('energystrategyCode') query_list = db_session.query(Schedulelqt).filter_by( energystrategyCode=code).all() if query_list: for item in query_list: if count_time(item.enablestarttime, item.enableendtime, new_start, new_end) != 'yes': return json.dumps({ 'code': '20003', 'message': '工作时间设置出现冲突' }) data = Schedulelqt(enablestarttime=new_start, enableendtime=new_end, comment=request.values.get('comment'), energystrategyCode=code, lqt1_allowrun=request.values.get('lqt1'), lqt2_allowrun=request.values.get('lqt2')) db_session.add(data) db_session.commit() db_session.close() return json.dumps({'code': '20001', 'message': '设置成功'}) else: data = Schedulelqt(enablestarttime=new_start, comment=request.values.get('comment'), energystrategyCode=request.values.get( 'energystrategyCode'), enableendtime=new_end, lqt1_allowrun=request.values.get('lqt1'), lqt2_allowrun=request.values.get('lqt2')) db_session.add(data) db_session.commit() db_session.close() return json.dumps({'code': '20001', 'message': '设置成功'}) if request.method == 'DELETE': items = request.values.get('ID').split(',') for item in items: data = db_session.query(Schedulelqt).filter_by( ID=int(item)).first() db_session.delete(data) db_session.commit() return json.dumps({'code': '20001', 'message': '删除成功'}) if request.method == 'PUT': new_start = request.values.get('start_time') new_end = request.values.get('end_time') query_data = db_session.query(Schedulelqt).filter_by( ID=request.values.get('ID')).first() code = request.values.get('energystrategyCode') query_list = db_session.query(Schedulelqt).filter_by( energystrategyCode=code).all() results = [] for i in query_list: if i.ID != query_data.ID: results.append(i) if results: for item in results: if count_time(item.enablestarttime, item.enableendtime, new_start, new_end) != 'yes': return json.dumps({ 'code': '20003', 'message': '工作时间设置出现冲突' }) query_data.enablestarttime = new_start query_data.enableendtime = new_end query_data.comment = request.values.get('comment') query_data.lqt1_allowrun = request.values.get('lqt1') query_data.lqt2_allowrun = request.values.get('lqt2') db_session.add(query_data) db_session.commit() db_session.close() return json.dumps({'code': '20001', 'message': '设置成功'}) else: query_data.enablestarttime = new_start query_data.enableendtime = new_end query_data.comment = request.values.get('comment') query_data.lqt1_allowrun = request.values.get('lqt1') query_data.lqt2_allowrun = request.values.get('lqt2') db_session.add(query_data) db_session.commit() db_session.close() return json.dumps({'code': '20001', 'message': '设置成功'}) except Exception as e: logger.error(e) insertSyslog("error", "工时安排设置出错:" + str(e), current_user.Name) return json.dumps({ 'code': '20002', 'message': str(e) }, cls=AlchemyEncoder, ensure_ascii=False)