Added the _get_currency_options method to the BasePageParams class, and applied it to the currency_options pseudo property of the PageParams classes of the ledger, income and expenses log, trial balance, income statement, and balance sheet reports.

This commit is contained in:
依瑪貓 2023-03-08 18:23:49 +08:00
parent d74c62dbb7
commit 5687852dfb
6 changed files with 25 additions and 30 deletions

View File

@ -362,12 +362,7 @@ class PageParams(BasePageParams):
return url_for("accounting.report.balance-sheet", return url_for("accounting.report.balance-sheet",
currency=currency, period=self.period) currency=currency, period=self.period)
in_use: set[str] = set(db.session.scalars( return self._get_currency_options(get_url, self.currency)
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == self.currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]
class BalanceSheet(BaseReport): class BalanceSheet(BaseReport):

View File

@ -325,12 +325,7 @@ class PageParams(BasePageParams):
currency=currency, account=self.account, currency=currency, account=self.account,
period=self.period) period=self.period)
in_use: set[str] = set(db.session.scalars( return self._get_currency_options(get_url, self.currency)
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == self.currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]
@property @property
def account_options(self) -> list[OptionLink]: def account_options(self) -> list[OptionLink]:

View File

@ -193,12 +193,7 @@ class PageParams(BasePageParams):
return url_for("accounting.report.income-statement", return url_for("accounting.report.income-statement",
currency=currency, period=self.period) currency=currency, period=self.period)
in_use: set[str] = set(db.session.scalars( return self._get_currency_options(get_url, self.currency)
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == self.currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]
class IncomeStatement(BaseReport): class IncomeStatement(BaseReport):

View File

@ -297,12 +297,7 @@ class PageParams(BasePageParams):
currency=currency, account=self.account, currency=currency, account=self.account,
period=self.period) period=self.period)
in_use: set[str] = set(db.session.scalars( return self._get_currency_options(get_url, self.currency)
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == self.currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]
@property @property
def account_options(self) -> list[OptionLink]: def account_options(self) -> list[OptionLink]:

View File

@ -155,12 +155,7 @@ class PageParams(BasePageParams):
return url_for("accounting.report.trial-balance", return url_for("accounting.report.trial-balance",
currency=currency, period=self.period) currency=currency, period=self.period)
in_use: set[str] = set(db.session.scalars( return self._get_currency_options(get_url, self.currency)
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == self.currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]
class TrialBalance(BaseReport): class TrialBalance(BaseReport):

View File

@ -22,9 +22,13 @@ from abc import ABC, abstractmethod
from urllib.parse import urlparse, ParseResult, parse_qsl, urlencode, \ from urllib.parse import urlparse, ParseResult, parse_qsl, urlencode, \
urlunparse urlunparse
import sqlalchemy as sa
from flask import request from flask import request
from accounting import db
from accounting.models import Currency, JournalEntry
from accounting.utils.txn_types import TransactionType from accounting.utils.txn_types import TransactionType
from .option_link import OptionLink
from .report_chooser import ReportChooser from .report_chooser import ReportChooser
@ -66,3 +70,19 @@ class BasePageParams(ABC):
parts: list[str] = list(uri_p) parts: list[str] = list(uri_p)
parts[4] = urlencode(params) parts[4] = urlencode(params)
return urlunparse(parts) return urlunparse(parts)
@staticmethod
def _get_currency_options(get_url: t.Callable[[Currency], str],
active_currency: Currency) -> list[OptionLink]:
"""Returns the currency options.
:param get_url: The callback to return the URL of a currency.
:param active_currency: The active currency.
:return: The currency options.
"""
in_use: set[str] = set(db.session.scalars(
sa.select(JournalEntry.currency_code)
.group_by(JournalEntry.currency_code)).all())
return [OptionLink(str(x), get_url(x), x.code == active_currency.code)
for x in Currency.query.filter(Currency.code.in_(in_use))
.order_by(Currency.code).all()]