def delete_notifications(days: int = typer.Option( 30, help="Number of days to keep")): """Delete notifications older than X days""" typer.echo(f"Delete notifications older than {days} days...") db = database.SessionLocal() crud.delete_notifications(db, days) db.close()
async def db_session_middleware(request: Request, call_next: callable): response = Response('Internal server error', status_code=500) try: request.state.db = database.SessionLocal() response = await call_next(request) finally: request.state.db.close() return response
def seed(): db = database.SessionLocal() db.query(models.Ownership).delete() db.query(models.User).delete() user1 = models.User(id=1, fund=20000) user2 = models.User(id=2) user3 = models.User(id=3) db.add(user1) db.add(user2) db.add(user3) db.commit() db.close()
def delete_user(username: str = typer.Argument(..., help="The user to delete")): """Delete the user USERNAME""" db = database.SessionLocal() user = crud.get_user_by_username(db, username) if user is None: typer.secho(f"User '{username}' not found. Aborting.", fg=typer.colors.RED) else: crud.delete_user(db, user) typer.secho(f"User '{username}' deleted", fg=typer.colors.GREEN) db.close()
def create_users(): """ create test users: - username: admin, password: admin, isadmin: "true" - username: user2, password: user2pass, isadmin: "false" """ if not crud.get_user(db=database.SessionLocal(), username="******") and not crud.get_admins( db=database.SessionLocal()): user = schemas.UserCreate(**{ "username": "******", "password": "******", "isadmin": "true" }) crud.create_user(db=database.SessionLocal(), user=user) if not crud.get_user(db=database.SessionLocal(), username="******"): user = schemas.UserCreate(**{ "username": "******", "password": "******", "isadmin": "false" }) crud.create_user(db=database.SessionLocal(), user=user)
def create_configs(): """ create test configs """ # admin default config for testing config1 = schemas.ConfigCreate( **{ "owner": "admin", "name": "api-1", "metadata": { "name": "SimpleAPI", "url": "http://127.0.0.1:5057", "database": { "name": "apidb", "type": "sql", "ms": "postgresql", "host": "0.0.0.0", "port": "5432", "enabled": "true", "running": "true", }, "enabled": "true", "running": "true", }, "note": "The api has been enabled.", }) # user2 default config for testing config2 = schemas.ConfigCreate( **{ "owner": "user2", "name": "api-2", "metadata": { "name": "SimpleAPI", "url": "http://127.0.0.1:5057", "database": { "name": "apidb", "type": "sql", "ms": "postgresql", "host": "0.0.0.0", "port": "5432", "enabled": "true", "running": "false", }, "enabled": "true", "running": "false", }, "note": "The api has been enabled without the DB!", }) # create admin config if not crud.get_config( db=database.SessionLocal(), name="api-1", owner="admin"): crud.create_config(db=database.SessionLocal(), config=config1, owner="admin") # create user2 config if not crud.get_config( db=database.SessionLocal(), name="api-2", owner="user2"): crud.create_config(db=database.SessionLocal(), config=config2, owner="user2")
def get_db(): db = database.SessionLocal() try: yield db finally: db.close()