def updRolePermission(): myRes = MyResponse() RESOURCE_KEYS = MyRequest.get_verify_list("RESOURCE_KEYS") ROLE_KEY = MyRequest.get("ROLE_KEY", type=int) RESOURCE_TYPE = MyRequest.get("RESOURCE_TYPE", type=int) try: db_session.begin_nested() db_session.query(RolePermission).filter( RolePermission.ROLE_KEY == ROLE_KEY).filter( RolePermission.RESOURCE_TYPE == RESOURCE_TYPE).delete( synchronize_session=False) for key in RESOURCE_KEYS: rolePermission = RolePermission() rolePermission.ROLE_KEY = ROLE_KEY rolePermission.RESOURCE_KEY = key rolePermission.RESOURCE_TYPE = RESOURCE_TYPE db_session.add(rolePermission) db_session.commit() myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: db_session.rollback() mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def comAdd(cls, model): """ 添加方法 :param model: :return: """ try: db_session.add(model) db_session.commit() except SQLAlchemyError as ex: raise ex finally: db_session.close()
def delete(): myRes = MyResponse() ROLE_KEY = MyRequest.get("ROLE_KEY", type=int) try: MySqlalchemy.comDel(RoleInfo, [RoleInfo.ROLE_KEY == ROLE_KEY]) myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: db_session.rollback() mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def resetPwdByUserKey(): myRes = MyResponse() USER_KEY = MyRequest.get("USER_KEY", type=int) try: q_model = db_session.query(UserInfo).filter( UserInfo.USER_KEY == USER_KEY).one() q_model.set_password("123456") db_session.commit() myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def delete(): myRes = MyResponse() DPT_KEY = MyRequest.get_verify_empty("DPT_KEY", errmsg="ID不能为空") try: rows = db_session.query(DptInfo).filter(DptInfo.DPT_KEY.like(DPT_KEY+"%")).delete(synchronize_session=False) db_session.commit() if rows < 1: raise SQLAlchemyError("操作失败") myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: mylog.error(ex) myRes.msg = ResState.ERROR_DB finally: db_session.close() return myRes.to_json()
def comUpdate(cls, model, filters, attrs): """ 修改方法 :param model: :param filters: :param attrs: :return: """ try: rows = db_session.query(model).filter(*filters).update(attrs) db_session.commit() if rows < 1: raise SQLAlchemyError("操作失败") except SQLAlchemyError as ex: raise ex finally: db_session.close()
def comDel(cls, model, filters): """ 删除方法 :param model: :param filters: :return: """ try: rows = db_session.query(model).filter(*filters).delete( synchronize_session=False) db_session.commit() if rows < 1: raise SQLAlchemyError("操作失败") except SQLAlchemyError as ex: raise ex finally: db_session.close()
def updMyInfo(): myRes = MyResponse() USER_KEY = g.USER_KEY upd_attr = {} upd_attr["USER_NAME"] = MyRequest.get_verify_empty("USER_NAME", errmsg="姓名不能为空") upd_attr["PHONE"] = MyRequest.get("PHONE", type=str) try: MySqlalchemy.comUpdate(UserInfo, [UserInfo.USER_KEY == USER_KEY], upd_attr) myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def comAdds(cls, models): """ 批量添加 :param models: :return: """ try: if isinstance(models, (list, tuple)): db_session.begin_nested() for model in models: db_session.add(model) db_session.commit() else: raise MyError("Models mast be list or tuple.") except SQLAlchemyError as ex: db_session.rollback() raise ex finally: db_session.close()
def comUpdates(cls, list_model, id, attrs): """ 批量修改 :param list_model: :param id: :param attrs: :return: """ try: db_session.begin_nested() for model in list_model: rows = db_session.query(model).filter( model.Fid == id).update(attrs) if rows < 1: raise SQLAlchemyError("操作失败") db_session.commit() except SQLAlchemyError as ex: db_session.rollback() raise ex finally: db_session.close()
def updUserRole(): myRes = MyResponse() ROLE_KEYS = MyRequest.getList("ROLE_KEYS[]") USER_KEY = MyRequest.get("USER_KEY", type=int) try: db_session.begin_nested() db_session.query(UserRole).filter( UserRole.USER_KEY == USER_KEY).delete() for key in ROLE_KEYS: userRole = UserRole() userRole.USER_KEY = USER_KEY userRole.ROLE_KEY = key db_session.add(userRole) db_session.commit() myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: db_session.rollback() mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def resetMyPwd(): myRes = MyResponse() FuserId = g.USER_KEY FoldPwd = MyRequest.get_verify_empty("FoldPwd", errmsg="旧密码不能为空") FnewPwd = MyRequest.get_verify_empty("FnewPwd", errmsg="新密码不能为空") FnewPwdOk = MyRequest.get("FnewPwdOk", type=str) try: if FnewPwd != FnewPwdOk: return myRes.to_json_msg("新旧密码不一致") userInfo = db_session.query(UserInfo).filter( UserInfo.USER_KEY == FuserId).one() if not userInfo.check_password(userInfo.USER_PWD, FoldPwd): return myRes.to_json_msg("旧密码错误,请重新输入") userInfo.set_password(FnewPwd) db_session.commit() myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()
def comDels(cls, models, ids): """ 批量删除 :param models: :param ids: :return: """ try: if isinstance(models, (list, tuple)) and isinstance(ids, (list, tuple)): db_session.begin_nested() for model in models: rows = db_session.query(models).filter( model.Fid == id).delete(synchronize_session=False) if rows < 1: raise SQLAlchemyError("操作失败") db_session.commit() else: raise MyError("Models and ids mast be list or tuple.") except SQLAlchemyError as ex: db_session.rollback() raise ex finally: db_session.close()
def delete(): myRes = MyResponse() OPER_KEY = MyRequest.get_verify_empty("OPER_KEY", errmsg="OPER_KEY不能为空") try: db_session.begin_nested() ###删除按钮相关的角色权限信息 db_session.query(RolePermission).filter( RolePermission.RESOURCE_TYPE == 1).filter( RolePermission.RESOURCE_KEY.like(OPER_KEY + "%")).delete( synchronize_session=False) ###删除按钮信息 db_session.query(OperInfo).filter( OperInfo.OPER_KEY.like(OPER_KEY + "%")).delete(synchronize_session=False) db_session.commit() myRes.code = ResState.HTTP_SUCCESS myRes.msg = "操作成功" except SQLAlchemyError as ex: db_session.rollback() mylog.error(ex) myRes.msg = str(ex) finally: db_session.close() return myRes.to_json()