Skip to content

Commit

Permalink
[Add] GID support
Browse files Browse the repository at this point in the history
  • Loading branch information
ileodo committed Aug 14, 2023
1 parent a5822c9 commit 0193e40
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/moneywiz_api/managers/account_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
InvestmentAccount,
ForexAccount,
)
from moneywiz_api.types import ID
from moneywiz_api.types import ID, GID


class AccountManager(RecordManager[Account]):
Expand Down
13 changes: 12 additions & 1 deletion src/moneywiz_api/managers/category_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from moneywiz_api.model.category import Category
from moneywiz_api.managers.record_manager import RecordManager
from moneywiz_api.types import ID
from moneywiz_api.types import ID, GID


class CategoryManager(RecordManager[Category]):
Expand All @@ -26,6 +26,17 @@ def get_name_chain(self, category_id: ID) -> List[str]:
current = self.get(current.parentId)
return ret

def get_name_chain_by_gid(self, category_gid: GID) -> List[str]:
ret: List[str] = []
current = self.get_by_gid(category_gid)
while current:
ret.insert(0, current.name)
if not current.parentId:
break
else:
current = self.get(current.parentId)
return ret

def get_categories_for_user(self, user_id: ID) -> List[Category]:
return sorted(
[x for _, x in self.records().items() if x.user == user_id],
Expand Down
6 changes: 6 additions & 0 deletions src/moneywiz_api/managers/investment_holding_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ def ents(self) -> Dict[str, Callable]:

def get_holdings_for_account(self, account_id: ID) -> List[InvestmentHolding]:
return [x for _, x in self.records().items() if x.account == account_id]

def update_last_price(self, latest_price: float):
raise NotImplementedError()

def update_price_table(self, latest_price: float):
raise NotImplementedError()
12 changes: 11 additions & 1 deletion src/moneywiz_api/managers/record_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from moneywiz_api.database_accessor import DatabaseAccessor
from moneywiz_api.model.record import Record
from moneywiz_api.types import ID
from moneywiz_api.types import ID, GID


T = TypeVar("T", bound=Record)
Expand All @@ -12,6 +12,7 @@
class RecordManager(ABC, Generic[T]):
def __init__(self):
self._records: Dict[ID, T] = {}
self._gid_to_id: Dict[GID, ID] = {}

@property
@abstractmethod
Expand All @@ -29,10 +30,19 @@ def load(self, db_accessor: DatabaseAccessor) -> None:

def add(self, record: T) -> None:
self._records[record.id] = record
if record.gid in self._gid_to_id:
raise Exception(
f"Duplicate gid for {record}, existing record Id {self._gid_to_id[record.gid]}"
)

self._gid_to_id[record.gid] = record.id

def get(self, record_id: ID) -> T | None:
return self._records.get(record_id)

def get_by_gid(self, gid: GID) -> T | None:
return self._records.get(self._gid_to_id.get(gid))

def records(self) -> Dict[ID, T]:
return self._records

Expand Down
58 changes: 58 additions & 0 deletions src/moneywiz_api/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from moneywiz_api.model.record import Record

from moneywiz_api.model.account import (
Account,
InvestmentAccount,
CashAccount,
LoanAccount,
ForexAccount,
BankChequeAccount,
BankSavingAccount,
CreditCardAccount,
)

from moneywiz_api.model.category import Category
from moneywiz_api.model.investment_holding import InvestmentHolding
from moneywiz_api.model.payee import Payee
from moneywiz_api.model.transaction import (
RefundTransaction,
Transaction,
WithdrawTransaction,
DepositTransaction,
TransferDepositTransaction,
TransferBudgetTransaction,
TransferWithdrawTransaction,
InvestmentTransaction,
InvestmentBuyTransaction,
InvestmentSellTransaction,
InvestmentExchangeTransaction,
ReconcileTransaction,
)


__all__ = [
"Record",
"Account",
"InvestmentAccount",
"CashAccount",
"LoanAccount",
"ForexAccount",
"BankChequeAccount",
"BankSavingAccount",
"CreditCardAccount",
"Category",
"InvestmentHolding",
"Payee",
"RefundTransaction",
"Transaction",
"WithdrawTransaction",
"DepositTransaction",
"TransferDepositTransaction",
"TransferBudgetTransaction",
"TransferWithdrawTransaction",
"InvestmentTransaction",
"InvestmentBuyTransaction",
"InvestmentSellTransaction",
"InvestmentExchangeTransaction",
"ReconcileTransaction",
]
5 changes: 5 additions & 0 deletions src/moneywiz_api/model/investment_holding.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class InvestmentHolding(Record):
holding_type: Optional[str]
description: str

price_per_share_available_online: bool

"""
Unsure about the usage.
value can be 0,1
Expand All @@ -46,6 +48,9 @@ def __init__(self, row):
self.symbol = row["ZSYMBOL"]
self.holding_type = row["ZHOLDINGTYPE"]
self.description = row["ZDESC"]
self.price_per_share_available_online = (
row["ZISPRICEPERSHAREAVAILABLEONLINE"] == 1
)

self._investment_object_type = row["ZINVESTMENTOBJECTTYPE"]
self._cost_basis_of_missing_ob_shares = row["ZISFROMONLINEBANKING"]
Expand Down
7 changes: 3 additions & 4 deletions src/moneywiz_api/model/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class Record:
_raw: Dict[str, Any] = field(repr=False)
_ent: ENT_ID = field(repr=False)
_created_at: float = field(repr=False)
_gid: str = field(repr=False)
gid: str = field(repr=False)
id: ID

def __init__(self, row):
self._raw = row
self._ent = row["Z_ENT"]
self._created_at = row["ZOBJECTCREATIONDATE"]
self._gid = row["ZGID"]
self.gid = row["ZGID"]
self.id = row["Z_PK"]

# Validate
assert self._raw
assert self._ent
assert self._created_at
assert self._gid
assert self.gid
assert self.id

def ent(self) -> ENT_ID:
Expand Down Expand Up @@ -64,5 +64,4 @@ def as_dict(self) -> Dict[str, Any]:
del original["_raw"]
del original["_ent"]
del original["_created_at"]
del original["_gid"]
return original
2 changes: 1 addition & 1 deletion src/moneywiz_api/model/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def validate(self):
assert self.price_per_share >= 0
assert (
self.number_of_shares * self.price_per_share - self.fee
) == pytest.approx(self.amount, abs=0.001)
) == pytest.approx(self.amount, abs=0.001), self.as_dict()


@dataclass
Expand Down
1 change: 1 addition & 0 deletions src/moneywiz_api/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Literal

ID = int
GID = str
ENT_ID = int

CategoryType = Literal["Expenses", "Income"]
2 changes: 1 addition & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from moneywiz_api import MoneywizApi

from tests.test_config import (
from data.test_config_my import (
TEST_DB_PATH,
CASH_BALANCES,
HOLDINGS_BALANCES,
Expand Down

0 comments on commit 0193e40

Please sign in to comment.