def get_db(): db = None try: db = SessionLocal() yield db finally: db.close()
def init() -> None: try: db = SessionLocal() # Try to create session to check if DB is awake db.execute("SELECT 1") except Exception as e: logger.error(e) raise e
async def get_number_of_questions(data_dict: {}): db: Session = SessionLocal() db_room: Room = db.query(Room).filter( Room.idRoom == data_dict.get('room_id')).first() quantity_questions = len(db_room.test.questions) db.close() return quantity_questions
async def save_room_data(res, data_dict: {}): db: Session = SessionLocal() db_room: Room = db.query(Room).filter( Room.idRoom == data_dict.get('room_id')).first() db_room.roomData = res db.flush() db.commit() db.close()
def update_turn_off_room(data_dict: {}): db: Session = SessionLocal() db_room: Room = db.query(Room).filter( Room.idRoom == data_dict.get('room_id')).first() db_room.isRunning = False db_room.isActive = False db.flush() db.commit() db.close()
def store_user_contributes(data: Dict) -> None: db = SessionLocal() try: user_contributes = models.UserContributes(**data) db.add(user_contributes) db.commit() except Exception as err: logger.error(f"error {err}")
def get_db() -> Generator: try: db = SessionLocal() yield db finally: db.close()
def get_db(): db = SessionLocal() return db
async def verify_is_room_active(data_dict: {}): db: Session = SessionLocal() db_room: Room = db.query(Room).filter( Room.idRoom == data_dict.get('room_id')).first() return db_room.isActive
from sqlalchemy.orm import Session from app import crud, schemas from app.config import settings from app.db import base # noqa: F401 from app.db.database import Base, SessionLocal, engine # noqa def init_db(db: Session) -> None: # Base.metadata.create_all(bind=engine) user = crud.user.get_by_username(db, username=settings.SUPERUSER_USERNAME) if not user: user_in = schemas.UserCreate( username=settings.SUPERUSER_USERNAME, password=settings.SUPERUSER_PASSWORD, is_superuser=True, ) crud.user.create(db, user_in) if __name__ == "__main__": init_db(SessionLocal())
def init() -> None: db = SessionLocal() init_db(db)
from app.db.database import SessionLocal from app.db.models import UserContributes from sqlalchemy import func from sqlalchemy_filters import apply_filters db = SessionLocal() async def get_user_list(): return db.query(UserContributes.user.distinct()).all() async def topics_by_user(username: str): res = db.query( UserContributes.user, UserContributes.title, func.count(UserContributes.id_sk) ).filter( UserContributes.user.ilike(f'%{username}%') ).group_by( UserContributes.user, UserContributes.title ).order_by( func.count(UserContributes.id_sk).desc() ).all() return res async def get_most_active_user(year: int = None, month: int = None, day=None): query_params = {