Flatten budget-app contents into repo root
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""Run once at container start: creates the first admin and the default categories and sites."""
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from .config import settings
|
||||
from .db import SessionLocal
|
||||
from .models import Category, Department, Site, User
|
||||
from .security import hash_password
|
||||
from .seed import CATEGORIES, SITES
|
||||
|
||||
DEFAULT_DEPARTMENT = "IT"
|
||||
|
||||
|
||||
def seed_categories(db, department: Department) -> None:
|
||||
for i, (en, pt, section) in enumerate(CATEGORIES):
|
||||
db.add(Category(department_id=department.id, name_en=en, name_pt=pt, section=section, sort_order=(i + 1) * 10))
|
||||
|
||||
|
||||
def bootstrap() -> None:
|
||||
with SessionLocal() as db:
|
||||
if not db.scalar(select(func.count(Department.id))):
|
||||
dept = Department(name=DEFAULT_DEPARTMENT)
|
||||
db.add(dept)
|
||||
db.flush()
|
||||
seed_categories(db, dept)
|
||||
if not db.scalar(select(func.count(User.id))):
|
||||
db.add(User(email=settings.admin_email.lower(), name=settings.admin_name,
|
||||
password_hash=hash_password(settings.admin_password), role="admin",
|
||||
language=settings.default_language,
|
||||
department_id=db.scalar(select(Department.id).order_by(Department.id))))
|
||||
print(f"Created admin user {settings.admin_email}")
|
||||
if not db.scalar(select(func.count(Site.id))):
|
||||
for name in SITES:
|
||||
db.add(Site(name=name))
|
||||
db.commit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bootstrap()
|
||||
Reference in New Issue
Block a user