def add_node(args): node_new = Node(**args) try: session.add(node_new) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "数据提交失败,失败原因为{0}".format(e) return True, "数据提交成功,成功新增基站{0}".format(args)
def edit_node(nid, args): the_node = session.query(Node).filter_by(nid=nid) if not the_node.first(): # 记录不存在 return False, "无此基站" try: the_node.update(args) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "基站{0}数据修改失败, 错误原因{1}".format(nid, e) return True, "基站{0} 数据成功修改为{1}".format(nid, args)
def sensors_vary_modify(sensor_id, value): the_sensor = session.query(Sensor.sid).filter_by(sid=sensor_id) if not the_sensor.first(): return False, "无此传感器" if not re.match(r'^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0|[1-9]\d*$', str(value)): return False, "value值输入有误,应为整数或浮点型" try: the_sensor.update({"allow_vary": float(value)}) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "传感器{0}阈值修改失败, 错误原因{1}".format(sensor_id, e) return True, "传感器{0} 阈值成功修改为{1}".format(sensor_id, value)
def edit_intro(intro_id, **kwargs): tb_intro = TWorkIntroduction # 名字太长 换个短点的名字 the_intro = session.query(tb_intro).filter_by(id=intro_id) if the_intro.first().userid != g.user.ID: return False, "不能修改他人创建的工作简介" else: try: the_intro.update(kwargs) session.commit() except exc.SQLAlchemyError as e: session.rollback() raise e # return False, "工作简介数据修改失败" return True, "工作简介修改成功"
def add_intro(**intro): Intro = TWorkIntroduction the_intro = TWorkIntroduction(**intro) the_intro.userid = g.user.ID the_intro.create_user = g.user.ID the_intro.username = session.query(TStaff.StaffName).filter_by(ID=g.user.ID).first()[0] the_intro.snumber = session.query(func.max(Intro.snumber)).first()[0] + 1 try: session.add(the_intro) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "数据提交失败" return True, "数据提交成功"
def edit_daily(daily_id, **kwargs): the_daily = session.query(TDailyRecord).filter_by(ID=daily_id) date_p = datetime.strptime(kwargs["WorkDate"], '%Y-%m-%d').date() kwargs["JobDescription"] = session.query( TbIntro.workintro).filter_by(id=kwargs["workintroId"]).first()[0] kwargs["updateuser"] = g.user.ID years, kwargs["Weeks"], kwargs["DayInWeek"] = date_p.isocalendar( ) # 获取周数和星期几 if the_daily.first().userid != g.user.ID: return False, "不能修改他人的工作简介" else: try: the_daily.update(kwargs) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "工作日报数据修改失败" return True, "工作日报数据修改成功"
def add_daily(**kwargs): kwargs["userid"] = g.user.ID kwargs["createuser"] = g.user.ID kwargs["updateuser"] = g.user.ID kwargs["isdelete"] = 0 daily = TDailyRecord(**kwargs) date_p = datetime.strptime(kwargs["WorkDate"], '%Y-%m-%d').date() years, daily.Weeks, daily.DayInWeek = date_p.isocalendar() # 获取周数和星期几 daily.JobDescription = session.query( TbIntro.workintro).filter_by(id=daily.workintroId).first()[0] daily.StaffName = session.query( TStaff.StaffName).filter_by(ID=g.user.ID).first()[0] daily.ProjectName = session.query( TbProject.ProjectName).filter_by(ID=kwargs["ProjectID"]).first()[0] try: session.add(daily) session.commit() except exc.SQLAlchemyError as e: session.rollback() return False, "工作日报数据新增失败"