Replaced querying the transactions later with the "selectinload" query option in the income and expenses log. Retired the unused populate_entries function from the "accounting.report.reports.income_expenses" module.

This commit is contained in:
依瑪貓 2023-03-09 07:47:54 +08:00
parent ff3dd28cd7
commit c6487bf9d4

View File

@ -71,10 +71,13 @@ class ReportEntry:
"""The note.""" """The note."""
if entry is not None: if entry is not None:
self.entry = entry self.entry = entry
self.transaction = entry.transaction
self.date = entry.transaction.date
self.account = entry.account self.account = entry.account
self.summary = entry.summary self.summary = entry.summary
self.income = None if entry.is_debit else entry.amount self.income = None if entry.is_debit else entry.amount
self.expense = entry.amount if entry.is_debit else None self.expense = entry.amount if entry.is_debit else None
self.note = entry.transaction.note
class EntryCollector: class EntryCollector:
@ -159,7 +162,8 @@ class EntryCollector:
.order_by(Transaction.date, .order_by(Transaction.date,
JournalEntry.is_debit, JournalEntry.is_debit,
JournalEntry.no) JournalEntry.no)
.options(selectinload(JournalEntry.account))] .options(selectinload(JournalEntry.account),
selectinload(JournalEntry.transaction))]
@property @property
def __account_condition(self) -> sa.BinaryExpression: def __account_condition(self) -> sa.BinaryExpression:
@ -364,23 +368,6 @@ class PageParams(BasePageParams):
return options return options
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
if x.entry is not None}))}
for entry in entries:
if entry.entry is not None:
entry.transaction = transactions[entry.entry.transaction_id]
entry.date = entry.transaction.date
entry.note = entry.transaction.note
class IncomeExpenses(BaseReport): class IncomeExpenses(BaseReport):
"""The income and expenses log.""" """The income and expenses log."""
@ -422,7 +409,6 @@ class IncomeExpenses(BaseReport):
:return: The CSV rows. :return: The CSV rows.
""" """
populate_entries(self.__entries)
rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Account"), rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Account"),
gettext("Summary"), gettext("Income"), gettext("Summary"), gettext("Income"),
gettext("Expense"), gettext("Balance"), gettext("Expense"), gettext("Balance"),
@ -459,7 +445,6 @@ class IncomeExpenses(BaseReport):
= Pagination[ReportEntry](all_entries) = Pagination[ReportEntry](all_entries)
page_entries: list[ReportEntry] = pagination.list page_entries: list[ReportEntry] = pagination.list
has_data: bool = len(page_entries) > 0 has_data: bool = len(page_entries) > 0
populate_entries(page_entries)
brought_forward: ReportEntry | None = None brought_forward: ReportEntry | None = None
if len(page_entries) > 0 and page_entries[0].is_brought_forward: if len(page_entries) > 0 and page_entries[0].is_brought_forward:
brought_forward = page_entries[0] brought_forward = page_entries[0]