Files
budget-forecast/api/app/account/models.py

92 lines
2.9 KiB
Python

from uuid import UUID, uuid4
from enum import Enum
from sqlmodel import Field, SQLModel, select
from pydantic import Field as PydField
from category.models import CategoryRead
class AccountType(Enum):
Asset = "Asset" # < Denotes a generic asset account.
Checkings = "Checkings" # < Standard checking account
Savings = "Savings" # < Typical savings account
Cash = "Cash" # < Denotes a shoe-box or pillowcase stuffed with cash
Liability = "Liability" # < Denotes a generic liability account.
CreditCard = "CreditCard" # < Credit card accounts
Loan = "Loan" # < Loan and mortgage accounts (liability)
CertificateDep = "CertificateDep" # < Certificates of Deposit
Investment = "Investment" # < Investment account
MoneyMarket = "MoneyMarket" # < Money Market Account
Currency = "Currency" # < Denotes a currency trading account.
Income = "Income" # < Denotes an income account
Expense = "Expense" # < Denotes an expense account
AssetLoan = "AssetLoan" # < Denotes a loan (asset of the owner of this object)
Stock = "Stock" # < Denotes an security account as sub-account for an investment
Equity = "Equity" # < Denotes an equity account e.g. opening/closing balance
Payee = "Payee"
class AccountBase(SQLModel):
name: str = Field(index=True)
type: AccountType = Field(index=True)
default_category_id: UUID | None = Field(default=None, foreign_key="category.id")
class AccountBaseId(AccountBase):
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
class Account(AccountBaseId, table=True):
@classmethod
def create(cls, account, session):
account_db = cls.model_validate(account)
session.add(account_db)
session.commit()
session.refresh(account_db)
return account_db
@classmethod
def list(cls):
return select(Account)
@classmethod
def get(cls, session, account_id):
return session.get(Account, account_id)
@classmethod
def update(cls, session, account_db, account_data):
account_db.sqlmodel_update(account_data)
session.add(account_db)
session.commit()
session.refresh(account_db)
return account_db
@classmethod
def delete(cls, session, account):
session.delete(account)
session.commit()
class AccountRead(AccountBaseId):
# default_category: CategoryRead
pass
class AccountWrite(AccountBase):
default_category_id: UUID = PydField(default=None, json_schema_extra={
"foreign_key": {
"reference": {
"resource": "categories",
"schema": "CategoryRead",
"label": "name"
}
}
})
class AccountCreate(AccountWrite):
pass
class AccountUpdate(AccountWrite):
pass