Replaced querying the transactions later with the "selectinload" query option in the journal and search reports. Retired the unused populate_entries function from the "accounting.report.reports.journal" module.

This commit is contained in:
依瑪貓 2023-03-09 07:43:25 +08:00
parent a14ffa93ed
commit ff3dd28cd7
2 changed files with 8 additions and 24 deletions

View File

@ -47,7 +47,7 @@ class ReportEntry:
"""
self.entry: JournalEntry = entry
"""The journal entry."""
self.transaction: Transaction | None = None
self.transaction: Transaction = entry.transaction
"""The transaction."""
self.currency: Currency = entry.currency
"""The account."""
@ -145,26 +145,12 @@ class PageParams(BasePageParams):
period=self.period)
def populate_entries(entries: list[ReportEntry]) -> None:
"""Populates the report entries with relative data.
:param entries: The report entries.
:return: 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}))}
for entry in entries:
entry.transaction = transactions[entry.entry.transaction_id]
def get_csv_rows(entries: list[ReportEntry]) -> list[CSVRow]:
"""Composes and returns the CSV rows from the report entries.
:param entries: The report entries.
:return: The CSV rows.
"""
populate_entries(entries)
rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Currency"),
gettext("Account"), gettext("Summary"),
gettext("Debit"), gettext("Credit"),
@ -205,7 +191,8 @@ class Journal(BaseReport):
JournalEntry.is_debit.desc(),
JournalEntry.no)
.options(selectinload(JournalEntry.account),
selectinload(JournalEntry.currency)).all()]
selectinload(JournalEntry.currency),
selectinload(JournalEntry.transaction)).all()]
def csv(self) -> Response:
"""Returns the report as CSV for download.
@ -222,10 +209,8 @@ class Journal(BaseReport):
"""
pagination: Pagination[ReportEntry] \
= Pagination[ReportEntry](self.__entries)
page_entries: list[ReportEntry] = pagination.list
populate_entries(page_entries)
params: PageParams = PageParams(period=self.__period,
pagination=pagination,
entries=page_entries)
entries=pagination.list)
return render_template("accounting/report/journal.html",
report=params)

View File

@ -29,7 +29,7 @@ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \
Transaction, JournalEntry
from accounting.utils.pagination import Pagination
from accounting.utils.query import parse_query_keywords
from .journal import ReportEntry, populate_entries, get_csv_rows
from .journal import ReportEntry, get_csv_rows
from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport
from .utils.csv_export import csv_download
@ -70,7 +70,8 @@ class EntryCollector:
conditions.append(sa.or_(*sub_conditions))
return [ReportEntry(x) for x in JournalEntry.query.filter(*conditions)
.options(selectinload(JournalEntry.account),
selectinload(JournalEntry.currency))]
selectinload(JournalEntry.currency),
selectinload(JournalEntry.transaction))]
@staticmethod
def __get_account_condition(k: str) -> sa.Select:
@ -194,9 +195,7 @@ class Search(BaseReport):
"""
pagination: Pagination[ReportEntry] \
= Pagination[ReportEntry](self.__entries)
page_entries: list[ReportEntry] = pagination.list
populate_entries(page_entries)
params: PageParams = PageParams(pagination=pagination,
entries=page_entries)
entries=pagination.list)
return render_template("accounting/report/search.html",
report=params)