test
This commit is contained in:
1 parent
8d26a04095
commit
d3a8e8493c
13 files changed
+686
-69
No files matched your search
+26
-3
@@ -10,15 +10,24 @@ from domain.transaction import Transaction
|
||||
|
||||
|
||||
class FinancialModel(BaseModel):
|
||||
"""Корневая модель финансового плана.
|
||||
"""Корень финансовой модели.
|
||||
|
||||
Версии схемы:
|
||||
|
||||
- **v0** — legacy, файлы без поля ``version``;
|
||||
- **v1** — 2026-10: слои + pydantic, у денежных величин нет валюты;
|
||||
- **v2** — 2026-11: у каждой денежной величины есть валюта (``currency``),
|
||||
появилась ``display_currency`` (валюта вывода по умолчанию).
|
||||
|
||||
Сериализация (save/load) вынесена в `application.repositories.ModelRepository` —
|
||||
JsonFileRepository и ExcelRepository. Сам класс хранит только данные.
|
||||
"""
|
||||
|
||||
SCHEMA_VERSION: int = 1 # NB: не Field — это class-level metadata, не pydantic field
|
||||
SCHEMA_VERSION: int = 2 # NB: не Field — это class-level metadata, не pydantic field
|
||||
|
||||
base_currency: str = "RUB"
|
||||
# Валюта вывода по умолчанию. Пусто = base_currency.
|
||||
display_currency: str = ""
|
||||
accounts: list[Account] = Field(default_factory=list)
|
||||
transactions: list[Transaction] = Field(default_factory=list)
|
||||
recurring: list[RecurringCashflow] = Field(default_factory=list)
|
||||
@@ -29,11 +38,17 @@ class FinancialModel(BaseModel):
|
||||
default_factory=lambda: [r.model_copy() for r in DEFAULT_RATES]
|
||||
)
|
||||
|
||||
@property
|
||||
def output_currency(self) -> str:
|
||||
"""Валюта, в которой модель показывается по умолчанию."""
|
||||
return (self.display_currency or self.base_currency or "RUB").upper()
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Сериализация в dict с version и UUID-as-str (для JSON)."""
|
||||
return {
|
||||
"version": self.SCHEMA_VERSION,
|
||||
"base_currency": self.base_currency,
|
||||
"display_currency": self.display_currency,
|
||||
"accounts": [a.model_dump(mode="json") for a in self.accounts],
|
||||
"transactions": [t.model_dump(mode="json") for t in self.transactions],
|
||||
"recurring": [r.model_dump(mode="json") for r in self.recurring],
|
||||
@@ -50,12 +65,15 @@ class FinancialModel(BaseModel):
|
||||
return cls._from_legacy(data)
|
||||
if version == 1:
|
||||
return cls._from_v1(data)
|
||||
if version == 2:
|
||||
return cls._from_v2(data)
|
||||
raise ValueError(f"Unsupported FinancialModel version: {version}")
|
||||
|
||||
@classmethod
|
||||
def _from_v1(cls, data: dict) -> "FinancialModel":
|
||||
def _from_v2(cls, data: dict) -> "FinancialModel":
|
||||
return cls(
|
||||
base_currency=data.get("base_currency", "RUB"),
|
||||
display_currency=data.get("display_currency", ""),
|
||||
accounts=[Account.model_validate(a) for a in data.get("accounts", [])],
|
||||
transactions=[Transaction.model_validate(t) for t in data.get("transactions", [])],
|
||||
recurring=[RecurringCashflow.model_validate(r) for r in data.get("recurring", [])],
|
||||
@@ -65,6 +83,11 @@ class FinancialModel(BaseModel):
|
||||
exchange_rates=[ExchangeRate.model_validate(r) for r in data.get("exchange_rates", [])],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_v1(cls, data: dict) -> "FinancialModel":
|
||||
"""v1 → v2: операции не знали своей валюты — валюта наследуется от счёта."""
|
||||
return cls._from_v2(data)
|
||||
|
||||
@classmethod
|
||||
def _from_legacy(cls, data: dict) -> "FinancialModel":
|
||||
# Legacy: файлы без version. Структура совпадает с v1.
|
||||
|
||||
Reference in new issue
Block a user