Replaced querying the accounts later with the "selectinload" query option in the journal and search reports, and restored the lazy setting in the account relationship of the JournalEntry data model.

This commit is contained in:
依瑪貓 2023-03-09 07:21:01 +08:00
parent 21b3320e66
commit 13e3ef5875
4 changed files with 48 additions and 47 deletions

View File

@ -617,7 +617,7 @@ class JournalEntry(db.Model):
onupdate="CASCADE"), onupdate="CASCADE"),
nullable=False) nullable=False)
"""The account ID.""" """The account ID."""
account = db.relationship(Account, back_populates="entries") account = db.relationship(Account, back_populates="entries", lazy=False)
"""The account.""" """The account."""
summary = db.Column(db.String, nullable=True) summary = db.Column(db.String, nullable=True)
"""The summary.""" """The summary."""

View File

@ -22,6 +22,7 @@ from decimal import Decimal
import sqlalchemy as sa import sqlalchemy as sa
from flask import render_template, Response from flask import render_template, Response
from sqlalchemy.orm import selectinload
from accounting import db from accounting import db
from accounting.locale import gettext from accounting.locale import gettext
@ -64,6 +65,7 @@ class ReportEntry:
"""The amount.""" """The amount."""
if entry is not None: if entry is not None:
self.entry = entry self.entry = entry
self.account = entry.account
self.summary = entry.summary self.summary = entry.summary
self.debit = entry.amount if entry.is_debit else None self.debit = entry.amount if entry.is_debit else None
self.credit = None if entry.is_debit else entry.amount self.credit = None if entry.is_debit else entry.amount
@ -161,15 +163,11 @@ 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}))}
accounts: dict[int, Account] \
= {x.id: x for x in Account.query.filter(
Account.id.in_({x.entry.account_id for x in entries}))}
currencies: dict[int, Currency] \ currencies: dict[int, Currency] \
= {x.code: x for x in Currency.query.filter( = {x.code: x for x in Currency.query.filter(
Currency.code.in_({x.entry.currency_code for x in entries}))} 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.account = accounts[entry.entry.account_id]
entry.currency = currencies[entry.entry.currency_code] entry.currency = currencies[entry.entry.currency_code]
@ -218,7 +216,8 @@ class Journal(BaseReport):
.query(JournalEntry).join(Transaction).filter(*conditions) .query(JournalEntry).join(Transaction).filter(*conditions)
.order_by(Transaction.date, .order_by(Transaction.date,
JournalEntry.is_debit.desc(), JournalEntry.is_debit.desc(),
JournalEntry.no).all()] JournalEntry.no)
.options(selectinload(JournalEntry.account)).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

@ -22,6 +22,7 @@ from decimal import Decimal
import sqlalchemy as sa import sqlalchemy as sa
from flask import Response, render_template, request from flask import Response, render_template, request
from sqlalchemy.orm import selectinload
from accounting.locale import gettext from accounting.locale import gettext
from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \
@ -67,7 +68,8 @@ class EntryCollector:
except ArithmeticError: except ArithmeticError:
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))]
@staticmethod @staticmethod
def __get_account_condition(k: str) -> sa.Select: def __get_account_condition(k: str) -> sa.Select: