def database(): # Locate the testing config for Alembic config = Config(os.path.join(os.path.dirname(__file__), "../alembic.tests.ini")) # Set the migration secret key here if not os.environ.get("SECRET_KEY", None): os.environ["SECRET_KEY"] = "test" # Start up the in-memory database instance db_engine = create_engine("sqlite:///:memory:") Base.metadata.create_all(db_engine) db_session = Session(bind=db_engine) # Mark it as up-to-date with migrations command.stamp(config, "head") # Add some blacklisted karma items and a user who added them user = User(user_uid=1, username="******") db_session.add(user) c = BlockedKarma(topic="c", user_id=1) notepad = BlockedKarma(topic="notepad", user_id=1) db_session.add_all([c, notepad]) db_session.commit() return db_session
async def add(self, ctx: Context, item: str): author_id = ( db_session.query(User) .filter(User.user_uid == ctx.message.author.id) .first() .id ) if ( not db_session.query(BlockedKarma) .filter(BlockedKarma.topic == item.casefold()) .all() ): blacklist = BlockedKarma(topic=item.casefold(), user_id=author_id) db_session.add(blacklist) try: db_session.commit() await ctx.send(f"Added {item} to the karma blacklist. :pencil:") except (ScalarListException, SQLAlchemyError): db_session.rollback() await ctx.send( f"Something went wrong adding {item} to the karma blacklist. No change has occurred" ) else: await ctx.send( f"{item} is already in the karma blacklist. :page_with_curl:" )
async def add(self, ctx: Context, item: str): author_id = db_session.query(User).filter(User.user_uid == ctx.message.author.id).first().id if not db_session.query(BlockedKarma) \ .filter(BlockedKarma.topic == item.casefold()).all(): blacklist = BlockedKarma(topic=item.casefold(), user_id=author_id) db_session.add(blacklist) db_session.commit() await ctx.send(f'Added {item} to the karma blacklist. :pencil:') else: await ctx.send( f'{item} is already in the karma blacklist. :page_with_curl:')
def database(): # Locate the testing config for Alembic config = Config( os.path.join(os.path.dirname(__file__), '../alembic.tests.ini')) # Start up the in-memory database instance db_engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(db_engine) db_session = Session(bind=db_engine) # Mark it as up-to-date with migrations command.stamp(config, 'head') # Add some blacklisted karma items and a user who added them user = User(user_uid=1, username='******') db_session.add(user) c = BlockedKarma(topic='c'.casefold(), user_id=1) notepad = BlockedKarma(topic='notepad'.casefold(), user_id=1) db_session.add_all([c, notepad]) db_session.commit() return db_session