heavy refactor
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.db import get_session
|
||||
from src.ingest._common import _reader_raw, _upsert
|
||||
from src.models.b4w import B4wDb, B4wExport, Category, CategoryDb
|
||||
|
||||
|
||||
def upsert_category(
|
||||
session: Session, category: Category | None, do_commit=True
|
||||
) -> CategoryDb | None:
|
||||
if category is None:
|
||||
return None
|
||||
parameters = {
|
||||
"name": category.name,
|
||||
"parent": upsert_category(session, category.parent),
|
||||
}
|
||||
return _upsert(
|
||||
session,
|
||||
CategoryDb,
|
||||
parameters,
|
||||
parameters,
|
||||
do_update=False,
|
||||
do_commit=do_commit,
|
||||
)
|
||||
|
||||
|
||||
def upsert_b4w(
|
||||
session: Session, data: B4wExport, do_update=False, do_commit=True
|
||||
) -> B4wDb | None:
|
||||
category_db = upsert_category(session, data.category)
|
||||
|
||||
parameters_existing = {"hash_id": data.hash_id}
|
||||
|
||||
parameters_new = {
|
||||
"id_b4w": data.id,
|
||||
"hash_id": data.hash_id,
|
||||
"account_iban": data.account_iban,
|
||||
"account_currency": data.account_currency.value,
|
||||
"date_valid": data.date_valid,
|
||||
"date_book": data.date_book,
|
||||
"amount": data.amount_signed,
|
||||
"amount_currency": data.amount_currency.value,
|
||||
"text": data.reference,
|
||||
"other_name": data.other_name,
|
||||
"other_iban": data.other_iban,
|
||||
"category": category_db,
|
||||
"notes": data.notes,
|
||||
}
|
||||
|
||||
return _upsert(
|
||||
session,
|
||||
B4wDb,
|
||||
parameters_existing,
|
||||
parameters_new,
|
||||
do_update=do_update,
|
||||
do_commit=do_commit,
|
||||
)
|
||||
|
||||
|
||||
def ingest_b4w(path: Path):
|
||||
data = reader_b4w(path)
|
||||
with get_session() as session:
|
||||
for row in data:
|
||||
upsert_b4w(session, row, do_commit=False)
|
||||
session.commit()
|
||||
|
||||
|
||||
def reader_b4w(path: Path) -> list[B4wExport]:
|
||||
raw = _reader_raw(path)
|
||||
return list(map(B4wExport.model_validate, raw))
|
||||
Reference in New Issue
Block a user