diff --git a/api/app/account/fixtures.py b/api/app/account/fixtures.py new file mode 100644 index 0000000..bb9d99b --- /dev/null +++ b/api/app/account/fixtures.py @@ -0,0 +1,166 @@ +from account.models import Account +from account.schemas import AccountCreate + +def inject_fixtures(session): + for f in fixtures: + if f['parent_path']: + parent = Account.get_by_path(session, f['parent_path']) + f['parent_account_id'] = parent.id + else: + f['parent_account_id'] = None + del f['parent_path'] + schema = AccountCreate(**f) + Account.create(schema) + +fixtures = [{ + "name": "Current Assets", + "parent_path": None, + "type": "Asset" + },{ + "name": "Cash in Wallet", + "parent_path": "/Accounts/Asset/", + "type": "Asset", + },{ + "name": "Checking Account", + "parent_path": "/Accounts/Asset/", + "type": "Asset", + },{ + "name": "Savings Account", + "parent_path": "/Accounts/Asset/", + "type": "Asset", + }, + { + "name": "Debt Accounts", + "parent_path": None, + "type": "Liability", + }, + { + "name": "Credit Card", + "parent_path": "/Accounts/Liability/", + "type": "Liability", + }, + { + "name": "Salary", + "parent_path": None, + "type": "Income", + }, + { + "name": "Other Income", + "parent_path": None, + "type": "Income", + }, + { + "name": "Auto", + "parent_path": None, + "type": "Expense", + }, + { + "name": "Home", + "parent_path": None, + "type": "Expense", + }, + { + "name": "Rent", + "parent_path": "/Categories/Expense/Home", + "type": "Expense", + }, + { + "name": "Electricity", + "parent_path": "/Categories/Expense/Home", + "type": "Expense", + }, + { + "name": "Entertainment", + "parent_path": None, + "type": "Expense", + }, + { + "name": "Groceries", + "parent_path": None, + "type": "Expense", + }, +] + + + +""" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +""" \ No newline at end of file diff --git a/api/app/account/models.py b/api/app/account/models.py index 75be0e8..5b35a70 100644 --- a/api/app/account/models.py +++ b/api/app/account/models.py @@ -27,6 +27,14 @@ class Account(AccountBaseId, table=True): def is_category(self): return self.family in [v.value for v in CategoryFamily] + @classmethod + def get_by_path(cls, session, path): + if not path: + return None + + return session.exec(select(cls).where(cls.path == path)) + + def get_parent(self, session): if self.parent_account_id is None: return None diff --git a/api/app/db.py b/api/app/db.py index f347d35..0ba4938 100644 --- a/api/app/db.py +++ b/api/app/db.py @@ -14,6 +14,9 @@ engine = create_engine(sqlite_url, connect_args=connect_args) def create_db_and_tables(): SQLModel.metadata.create_all(engine) +def drop_tables(): + SQLModel.metadata.create_all(engine) + def get_session() -> Session: with Session(engine) as session: yield session diff --git a/api/app/initialize_db.py b/api/app/initialize_db.py new file mode 100644 index 0000000..e7cc9c7 --- /dev/null +++ b/api/app/initialize_db.py @@ -0,0 +1,12 @@ +import main + +from account.fixtures import inject_fixtures as account_inject_fixtures +from db import create_db_and_tables, get_session, drop_tables +from user import create_admin_account + +drop_tables() +create_db_and_tables() + +session = get_session().__next__() +create_admin_account(session) +account_inject_fixtures(session) diff --git a/api/app/main.py b/api/app/main.py index 12552be..b4afcc2 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -16,8 +16,7 @@ from transaction.routes import router as transaction_router @asynccontextmanager async def lifespan(app: FastAPI): - create_db_and_tables() - create_admin_account() + #do something before startup yield #do something before end diff --git a/api/app/user/manager.py b/api/app/user/manager.py index 6fd0630..7294873 100644 --- a/api/app/user/manager.py +++ b/api/app/user/manager.py @@ -2,12 +2,11 @@ import uuid from sqlmodel import select from fastapi import Depends -from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin, models, exceptions, schemas +from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin from fastapi_users.authentication import BearerTransport, AuthenticationBackend from fastapi_users.authentication.strategy.db import AccessTokenDatabase, DatabaseStrategy -from user.models import User, get_user_db, AccessToken, get_access_token_db, UserRead, UserUpdate, UserCreate -from db import get_session +from user.models import User, get_user_db, AccessToken, get_access_token_db SECRET = "SECRET" TOKEN_LIFETIME = 3600 @@ -49,8 +48,7 @@ reset_password_router = fastapi_users.get_reset_password_router() auth_router = fastapi_users.get_auth_router(auth_backend) -def create_admin_account(): - session = get_session().__next__() +def create_admin_account(session): admin_email = 'root@root.fr' statement = select(User).where(User.email == admin_email).limit(1) admin_user = session.exec(statement).first()