Features: - Manager-centric parking spot management - Fair assignment algorithm (parking/presence ratio) - Presence tracking calendar - Closing days (specific & weekly recurring) - Guarantees and exclusions - Authelia/LLDAP integration for SSO Stack: - FastAPI backend - SQLite database - Vanilla JS frontend - Docker deployment
40 lines
815 B
Python
40 lines
815 B
Python
"""
|
|
Database Connection Management
|
|
"""
|
|
from contextlib import contextmanager
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app import config
|
|
|
|
engine = create_engine(
|
|
config.DATABASE_URL,
|
|
connect_args={"check_same_thread": False} if "sqlite" in config.DATABASE_URL else {}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(bind=engine)
|
|
|
|
|
|
def get_db():
|
|
"""Database session for FastAPI dependency injection"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@contextmanager
|
|
def get_db_session():
|
|
"""Database session for regular Python code"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def init_db():
|
|
"""Create all tables"""
|
|
from database.models import Base
|
|
Base.metadata.create_all(bind=engine)
|