Adding fixture system
This commit is contained in:
166
api/app/account/fixtures.py
Normal file
166
api/app/account/fixtures.py
Normal file
@@ -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",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
<accounts>
|
||||||
|
<account type="9" name="">
|
||||||
|
<account type="9" name="Current Assets">
|
||||||
|
<account type="3" name="Cash in Wallet"/>
|
||||||
|
<account type="1" name="Checking Account"/>
|
||||||
|
<account type="1" name="Savings Account"/>
|
||||||
|
</account>
|
||||||
|
</account>
|
||||||
|
<account type="16" name="">
|
||||||
|
<account type="16" name="Opening Balances">
|
||||||
|
<flag name="OpeningBalanceAccount" value="Yes"/>
|
||||||
|
</account>
|
||||||
|
</account>
|
||||||
|
<account type="13" name="">
|
||||||
|
<account type="13" name="Adjustment"/>
|
||||||
|
<account type="13" name="Auto">
|
||||||
|
<account type="13" name="Fees"/>
|
||||||
|
<account type="13" name="Fuel"/>
|
||||||
|
<account type="13" name="Parking"/>
|
||||||
|
<account type="13" name="Repair and Maintenance"/>
|
||||||
|
</account>
|
||||||
|
<account type="13" name="Bank Service Charge"/>
|
||||||
|
<account type="13" name="Books"/>
|
||||||
|
<account type="13" name="Cable"/>
|
||||||
|
<account type="13" name="Charity"/>
|
||||||
|
<account type="13" name="Clothes"/>
|
||||||
|
<account type="13" name="Computer"/>
|
||||||
|
<account type="13" name="Dining"/>
|
||||||
|
<account type="13" name="Education"/>
|
||||||
|
<account type="13" name="Entertainment">
|
||||||
|
<account type="13" name="Music/Movies"/>
|
||||||
|
<account type="13" name="Recreation"/>
|
||||||
|
<account type="13" name="Travel"/>
|
||||||
|
</account>
|
||||||
|
<account type="13" name="Gifts"/>
|
||||||
|
<account type="13" name="Groceries"/>
|
||||||
|
<account type="13" name="Hobbies"/>
|
||||||
|
<account type="13" name="Insurance">
|
||||||
|
<account type="13" name="Auto Insurance"/>
|
||||||
|
<account type="13" name="Health Insurance"/>
|
||||||
|
<account type="13" name="Life Insurance"/>
|
||||||
|
</account>
|
||||||
|
<account type="13" name="Laundry/Dry Cleaning"/>
|
||||||
|
<account type="13" name="Medical Expenses"/>
|
||||||
|
<account type="13" name="Miscellaneous"/>
|
||||||
|
<account type="13" name="Online Services"/>
|
||||||
|
<account type="13" name="Phone"/>
|
||||||
|
<account type="13" name="Public Transportation"/>
|
||||||
|
<account type="13" name="Subscriptions"/>
|
||||||
|
<account type="13" name="Supplies"/>
|
||||||
|
<account type="13" name="Taxes">
|
||||||
|
<account type="13" name="Federal"/>
|
||||||
|
<account type="13" name="Medicare"/>
|
||||||
|
<account type="13" name="Other Tax"/>
|
||||||
|
<account type="13" name="Social Security"/>
|
||||||
|
<account type="13" name="State/Province"/>
|
||||||
|
</account>
|
||||||
|
<account type="13" name="Utilities">
|
||||||
|
<account type="13" name="Electric"/>
|
||||||
|
<account type="13" name="Garbage collection"/>
|
||||||
|
<account type="13" name="Gas"/>
|
||||||
|
<account type="13" name="Water"/>
|
||||||
|
</account>
|
||||||
|
</account>
|
||||||
|
<account type="12" name="">
|
||||||
|
<account type="12" name="Bonus"/>
|
||||||
|
<account type="12" name="Gifts Received"/>
|
||||||
|
<account type="12" name="Interest Income">
|
||||||
|
<account type="12" name="Checking Interest"/>
|
||||||
|
<account type="12" name="Other Interest"/>
|
||||||
|
<account type="12" name="Savings Interest"/>
|
||||||
|
</account>
|
||||||
|
<account type="12" name="Other Income"/>
|
||||||
|
<account type="12" name="Salary"/>
|
||||||
|
</account>
|
||||||
|
<account type="10" name="">
|
||||||
|
<account type="4" name="Credit Card"/>
|
||||||
|
</account>
|
||||||
|
</accounts>
|
||||||
|
"""
|
||||||
@@ -27,6 +27,14 @@ class Account(AccountBaseId, table=True):
|
|||||||
def is_category(self):
|
def is_category(self):
|
||||||
return self.family in [v.value for v in CategoryFamily]
|
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):
|
def get_parent(self, session):
|
||||||
if self.parent_account_id is None:
|
if self.parent_account_id is None:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ engine = create_engine(sqlite_url, connect_args=connect_args)
|
|||||||
def create_db_and_tables():
|
def create_db_and_tables():
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
|
def drop_tables():
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
def get_session() -> Session:
|
def get_session() -> Session:
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
yield session
|
yield session
|
||||||
|
|||||||
12
api/app/initialize_db.py
Normal file
12
api/app/initialize_db.py
Normal file
@@ -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)
|
||||||
@@ -16,8 +16,7 @@ from transaction.routes import router as transaction_router
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
create_db_and_tables()
|
#do something before startup
|
||||||
create_admin_account()
|
|
||||||
yield
|
yield
|
||||||
#do something before end
|
#do something before end
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ import uuid
|
|||||||
|
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
from fastapi import Depends
|
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 import BearerTransport, AuthenticationBackend
|
||||||
from fastapi_users.authentication.strategy.db import AccessTokenDatabase, DatabaseStrategy
|
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 user.models import User, get_user_db, AccessToken, get_access_token_db
|
||||||
from db import get_session
|
|
||||||
|
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
TOKEN_LIFETIME = 3600
|
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)
|
auth_router = fastapi_users.get_auth_router(auth_backend)
|
||||||
|
|
||||||
|
|
||||||
def create_admin_account():
|
def create_admin_account(session):
|
||||||
session = get_session().__next__()
|
|
||||||
admin_email = 'root@root.fr'
|
admin_email = 'root@root.fr'
|
||||||
statement = select(User).where(User.email == admin_email).limit(1)
|
statement = select(User).where(User.email == admin_email).limit(1)
|
||||||
admin_user = session.exec(statement).first()
|
admin_user = session.exec(statement).first()
|
||||||
|
|||||||
Reference in New Issue
Block a user