Replaced querying the currencies later with the "selectinload" query option in the journal and search reports.

This commit is contained in:
依瑪貓 2023-03-09 07:39:07 +08:00
parent 672fcbcbdf
commit a14ffa93ed
2 changed files with 5 additions and 7 deletions

View File

@ -49,7 +49,7 @@ class ReportEntry:
"""The journal entry.""" """The journal entry."""
self.transaction: Transaction | None = None self.transaction: Transaction | None = None
"""The transaction.""" """The transaction."""
self.currency: Currency | None = None self.currency: Currency = entry.currency
"""The account.""" """The account."""
self.account: Account = entry.account self.account: Account = entry.account
"""The account.""" """The account."""
@ -154,12 +154,8 @@ def populate_entries(entries: list[ReportEntry]) -> None:
transactions: dict[int, Transaction] \ transactions: dict[int, Transaction] \
= {x.id: x for x in Transaction.query.filter( = {x.id: x for x in Transaction.query.filter(
Transaction.id.in_({x.entry.transaction_id for x in entries}))} Transaction.id.in_({x.entry.transaction_id for x in entries}))}
currencies: dict[int, Currency] \
= {x.code: x for x in Currency.query.filter(
Currency.code.in_({x.entry.currency_code for x in entries}))}
for entry in entries: for entry in entries:
entry.transaction = transactions[entry.entry.transaction_id] entry.transaction = transactions[entry.entry.transaction_id]
entry.currency = currencies[entry.entry.currency_code]
def get_csv_rows(entries: list[ReportEntry]) -> list[CSVRow]: def get_csv_rows(entries: list[ReportEntry]) -> list[CSVRow]:
@ -208,7 +204,8 @@ class Journal(BaseReport):
.order_by(Transaction.date, .order_by(Transaction.date,
JournalEntry.is_debit.desc(), JournalEntry.is_debit.desc(),
JournalEntry.no) JournalEntry.no)
.options(selectinload(JournalEntry.account)).all()] .options(selectinload(JournalEntry.account),
selectinload(JournalEntry.currency)).all()]
def csv(self) -> Response: def csv(self) -> Response:
"""Returns the report as CSV for download. """Returns the report as CSV for download.

View File

@ -69,7 +69,8 @@ class EntryCollector:
pass pass
conditions.append(sa.or_(*sub_conditions)) conditions.append(sa.or_(*sub_conditions))
return [ReportEntry(x) for x in JournalEntry.query.filter(*conditions) return [ReportEntry(x) for x in JournalEntry.query.filter(*conditions)
.options(selectinload(JournalEntry.account))] .options(selectinload(JournalEntry.account),
selectinload(JournalEntry.currency))]
@staticmethod @staticmethod
def __get_account_condition(k: str) -> sa.Select: def __get_account_condition(k: str) -> sa.Select: