testing new features
This commit is contained in:
+28
-1
@@ -3,7 +3,15 @@ from uuid import UUID
|
||||
|
||||
from openpyxl import Workbook, load_workbook
|
||||
|
||||
from cashflow_model import Account, Asset, FinancialModel, Liability, RecurringCashflow, Transaction
|
||||
from cashflow_model import (
|
||||
Account,
|
||||
Asset,
|
||||
ExchangeRate,
|
||||
FinancialModel,
|
||||
Liability,
|
||||
RecurringCashflow,
|
||||
Transaction,
|
||||
)
|
||||
|
||||
|
||||
class SyncError(Exception):
|
||||
@@ -31,6 +39,10 @@ _SHEET_CONFIG = {
|
||||
"fields": ["id", "name", "balance", "interest", "payment"],
|
||||
"cls": Liability,
|
||||
},
|
||||
"ExchangeRates": {
|
||||
"fields": ["from_currency", "to_currency", "rate"],
|
||||
"cls": ExchangeRate,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +54,7 @@ class ExcelSync:
|
||||
|
||||
wb = load_workbook(path, read_only=True, data_only=True)
|
||||
model = FinancialModel()
|
||||
model.exchange_rates = []
|
||||
|
||||
for sheet_name, config in _SHEET_CONFIG.items():
|
||||
if sheet_name not in wb.sheetnames:
|
||||
@@ -62,6 +75,13 @@ class ExcelSync:
|
||||
data[header] = str(val) if not isinstance(val, (int, float)) else val
|
||||
self._add_to_model(model, sheet_name, data)
|
||||
|
||||
if "ModelInfo" in wb.sheetnames:
|
||||
ws = wb["ModelInfo"]
|
||||
rows = list(ws.iter_rows(values_only=True))
|
||||
for row in rows:
|
||||
if row[0] and str(row[0]).strip().lower() == "base_currency" and len(row) > 1:
|
||||
model.base_currency = str(row[1]).strip()
|
||||
|
||||
wb.close()
|
||||
return model
|
||||
|
||||
@@ -76,6 +96,7 @@ class ExcelSync:
|
||||
"Recurring": model.recurring,
|
||||
"Assets": model.assets,
|
||||
"Liabilities": model.liabilities,
|
||||
"ExchangeRates": model.exchange_rates,
|
||||
}
|
||||
|
||||
for sheet_name, items in collections.items():
|
||||
@@ -90,6 +111,10 @@ class ExcelSync:
|
||||
]
|
||||
ws.append(row)
|
||||
|
||||
ws_info = wb.create_sheet(title="ModelInfo")
|
||||
ws_info.append(["Property", "Value"])
|
||||
ws_info.append(["base_currency", model.base_currency])
|
||||
|
||||
wb.save(path)
|
||||
|
||||
def _add_to_model(self, model: FinancialModel, sheet_name: str, data: dict) -> None:
|
||||
@@ -104,5 +129,7 @@ class ExcelSync:
|
||||
model.assets.append(Asset.from_dict(data))
|
||||
elif sheet_name == "Liabilities":
|
||||
model.liabilities.append(Liability.from_dict(data))
|
||||
elif sheet_name == "ExchangeRates":
|
||||
model.exchange_rates.append(ExchangeRate.from_dict(data))
|
||||
except Exception as e:
|
||||
raise SyncError(f"Failed to parse row in {sheet_name}: {e}") from e
|
||||
|
||||
Reference in New Issue
Block a user