From 1c740b9bbcd86bd5a09cc44f7a2b6264e8efb837 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 11:14:58 +0800 Subject: [PATCH] Added the get_income_expenses_url utility to replace the common codes to retrieve the URL of an income and expenses log. --- .../report/reports/income_expenses.py | 31 +++++++------------ .../report/reports/utils/get_url.py | 18 +++++++++++ .../report/reports/utils/period_choosers.py | 9 ++---- .../report/reports/utils/report_chooser.py | 15 +++++---- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/accounting/report/reports/income_expenses.py b/src/accounting/report/reports/income_expenses.py index b0ef5d8..31e9632 100644 --- a/src/accounting/report/reports/income_expenses.py +++ b/src/accounting/report/reports/income_expenses.py @@ -33,6 +33,7 @@ from accounting.utils.pagination import Pagination from .utils.base_page_params import BasePageParams from .utils.base_report import BaseReport from .utils.csv_export import BaseCSVRow, csv_download, period_spec +from .utils.get_url import get_income_expenses_url from .utils.option_link import OptionLink from .utils.period_choosers import IncomeExpensesPeriodChooser from .utils.report_chooser import ReportChooser @@ -322,15 +323,9 @@ class PageParams(BasePageParams): :return: The currency options. """ - def get_url(currency: Currency): - if self.period.is_default: - return url_for("accounting.report.income-expenses-default", - currency=currency, account=self.account) - return url_for("accounting.report.income-expenses", - currency=currency, account=self.account, - period=self.period) - - return self._get_currency_options(get_url, self.currency) + return self._get_currency_options( + lambda x: get_income_expenses_url(x, self.account, self.period), + self.currency) @property def account_options(self) -> list[OptionLink]: @@ -338,18 +333,12 @@ class PageParams(BasePageParams): :return: The account options. """ - def get_url(account: IncomeExpensesAccount): - if self.period.is_default: - return url_for("accounting.report.income-expenses-default", - currency=self.currency, account=account) - return url_for("accounting.report.income-expenses", - currency=self.currency, account=account, - period=self.period) - current_al: IncomeExpensesAccount \ = IncomeExpensesAccount.current_assets_and_liabilities() options: list[OptionLink] \ - = [OptionLink(str(current_al), get_url(current_al), + = [OptionLink(str(current_al), + get_income_expenses_url(self.currency, current_al, + self.period), self.account.id == 0)] in_use: sa.Select = sa.Select(JournalEntry.account_id)\ .join(Account)\ @@ -359,7 +348,11 @@ class PageParams(BasePageParams): Account.base_code.startswith("21"), Account.base_code.startswith("22")))\ .group_by(JournalEntry.account_id) - options.extend([OptionLink(str(x), get_url(IncomeExpensesAccount(x)), + options.extend([OptionLink(str(x), + get_income_expenses_url( + self.currency, + IncomeExpensesAccount(x), + self.period), x.id == self.account.id) for x in Account.query.filter(Account.id.in_(in_use)) .order_by(Account.base_code, Account.no).all()]) diff --git a/src/accounting/report/reports/utils/get_url.py b/src/accounting/report/reports/utils/get_url.py index ef69d5b..4828d05 100644 --- a/src/accounting/report/reports/utils/get_url.py +++ b/src/accounting/report/reports/utils/get_url.py @@ -20,6 +20,7 @@ from flask import url_for from accounting.models import Currency, Account +from accounting.report.income_expense_account import IncomeExpensesAccount from accounting.report.period import Period @@ -38,3 +39,20 @@ def get_ledger_url(currency: Currency, account: Account, period: Period) \ return url_for("accounting.report.ledger", currency=currency, account=account, period=period) + + +def get_income_expenses_url(currency: Currency, account: IncomeExpensesAccount, + period: Period) -> str: + """Returns the URL of an income and expenses log. + + :param currency: The currency. + :param account: The account. + :param period: The period. + :return: The URL of the income and expenses log. + """ + if period.is_default: + return url_for("accounting.report.income-expenses-default", + currency=currency, account=account) + return url_for("accounting.report.income-expenses", + currency=currency, account=account, + period=period) diff --git a/src/accounting/report/reports/utils/period_choosers.py b/src/accounting/report/reports/utils/period_choosers.py index a1b8e1c..eb468a7 100644 --- a/src/accounting/report/reports/utils/period_choosers.py +++ b/src/accounting/report/reports/utils/period_choosers.py @@ -30,7 +30,7 @@ from accounting.report.income_expense_account import IncomeExpensesAccount from accounting.report.period import YearPeriod, Period, ThisMonth, \ LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \ TemplatePeriod -from .get_url import get_ledger_url +from .get_url import get_ledger_url, get_income_expenses_url class PeriodChooser(ABC): @@ -149,12 +149,7 @@ class IncomeExpensesPeriodChooser(PeriodChooser): super().__init__(None if first is None else first.date) def _url_for(self, period: Period) -> str: - if period.is_default: - return url_for("accounting.report.income-expenses-default", - currency=self.currency, account=self.account) - return url_for("accounting.report.income-expenses", - currency=self.currency, account=self.account, - period=period) + return get_income_expenses_url(self.currency, self.account, period) class TrialBalancePeriodChooser(PeriodChooser): diff --git a/src/accounting/report/reports/utils/report_chooser.py b/src/accounting/report/reports/utils/report_chooser.py index 42c0208..b741ac5 100644 --- a/src/accounting/report/reports/utils/report_chooser.py +++ b/src/accounting/report/reports/utils/report_chooser.py @@ -29,9 +29,10 @@ from flask_babel import LazyString from accounting import db from accounting.locale import gettext from accounting.models import Currency, Account +from accounting.report.income_expense_account import IncomeExpensesAccount from accounting.report.period import Period from accounting.template_globals import default_currency_code -from .get_url import get_ledger_url +from .get_url import get_ledger_url, get_income_expenses_url from .option_link import OptionLink from .report_type import ReportType @@ -114,13 +115,11 @@ class ReportChooser: account: Account = self.__account if not re.match(r"[12][12]", account.base_code): account: Account = Account.cash() - url: str = url_for("accounting.report.income-expenses-default", - currency=self.__currency, account=account) \ - if self.__period.is_default \ - else url_for("accounting.report.income-expenses", - currency=self.__currency, account=account, - period=self.__period) - return OptionLink(gettext("Income and Expenses Log"), url, + return OptionLink(gettext("Income and Expenses Log"), + get_income_expenses_url( + self.__currency, + IncomeExpensesAccount(account), + self.__period), self.__active_report == ReportType.INCOME_EXPENSES, fa_icon="fa-solid fa-money-bill-wave")