From a14ffa93ed3f3e179d709292a06f880c4ce58323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 9 Mar 2023 07:39:07 +0800 Subject: [PATCH] Replaced querying the currencies later with the "selectinload" query option in the journal and search reports. --- src/accounting/report/reports/journal.py | 9 +++------ src/accounting/report/reports/search.py | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/accounting/report/reports/journal.py b/src/accounting/report/reports/journal.py index 0552d3a..f62cc8b 100644 --- a/src/accounting/report/reports/journal.py +++ b/src/accounting/report/reports/journal.py @@ -49,7 +49,7 @@ class ReportEntry: """The journal entry.""" self.transaction: Transaction | None = None """The transaction.""" - self.currency: Currency | None = None + self.currency: Currency = entry.currency """The account.""" self.account: Account = entry.account """The account.""" @@ -154,12 +154,8 @@ def populate_entries(entries: list[ReportEntry]) -> None: transactions: dict[int, Transaction] \ = {x.id: x for x in Transaction.query.filter( 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: entry.transaction = transactions[entry.entry.transaction_id] - entry.currency = currencies[entry.entry.currency_code] def get_csv_rows(entries: list[ReportEntry]) -> list[CSVRow]: @@ -208,7 +204,8 @@ class Journal(BaseReport): .order_by(Transaction.date, JournalEntry.is_debit.desc(), JournalEntry.no) - .options(selectinload(JournalEntry.account)).all()] + .options(selectinload(JournalEntry.account), + selectinload(JournalEntry.currency)).all()] def csv(self) -> Response: """Returns the report as CSV for download. diff --git a/src/accounting/report/reports/search.py b/src/accounting/report/reports/search.py index 44f76fe..03cd7ca 100644 --- a/src/accounting/report/reports/search.py +++ b/src/accounting/report/reports/search.py @@ -69,7 +69,8 @@ class EntryCollector: pass conditions.append(sa.or_(*sub_conditions)) return [ReportEntry(x) for x in JournalEntry.query.filter(*conditions) - .options(selectinload(JournalEntry.account))] + .options(selectinload(JournalEntry.account), + selectinload(JournalEntry.currency))] @staticmethod def __get_account_condition(k: str) -> sa.Select: