From 74b695c089e2eda9285e77f81bb4c8186baaaaeb 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:18:36 +0800 Subject: [PATCH] Added the get_ledger_url utility to replace the common codes to retrieve the URL of a ledger. --- .../report/reports/balance_sheet.py | 19 ++------- .../report/reports/income_statement.py | 19 ++------- src/accounting/report/reports/ledger.py | 25 ++++-------- .../report/reports/trial_balance.py | 19 ++------- .../report/reports/utils/get_url.py | 40 +++++++++++++++++++ .../report/reports/utils/period_choosers.py | 8 +--- .../report/reports/utils/report_chooser.py | 11 ++--- 7 files changed, 65 insertions(+), 76 deletions(-) create mode 100644 src/accounting/report/reports/utils/get_url.py diff --git a/src/accounting/report/reports/balance_sheet.py b/src/accounting/report/reports/balance_sheet.py index daf93b3..7323da0 100644 --- a/src/accounting/report/reports/balance_sheet.py +++ b/src/accounting/report/reports/balance_sheet.py @@ -29,6 +29,7 @@ from accounting.models import Currency, BaseAccount, Account, Transaction, \ from accounting.report.period import Period from .utils.base_report import BaseReport from .utils.csv_export import BaseCSVRow, csv_download, period_spec +from .utils.get_url import get_ledger_url from .utils.option_link import OptionLink from .utils.base_page_params import BasePageParams from .utils.period_choosers import BalanceSheetPeriodChooser @@ -144,24 +145,12 @@ class AccountCollector: Account.base_code == "3353")).all() account_by_id: dict[int, Account] \ = {x.id: x for x in self.__all_accounts} - - def get_url(account: Account) -> str: - """Returns the ledger URL of an account. - - :param account: The account. - :return: The ledger URL of the account. - """ - if self.__period.is_default: - return url_for("accounting.report.ledger-default", - currency=self.__currency, account=account) - return url_for("accounting.report.ledger", - currency=self.__currency, account=account, - period=self.__period) - self.accounts: list[ReportAccount] \ = [ReportAccount(account=account_by_id[x.id], amount=x.balance, - url=get_url(account_by_id[x.id])) + url=get_ledger_url(self.__currency, + account_by_id[x.id], + self.__period)) for x in account_balances] self.__add_accumulated() self.__add_current_period() diff --git a/src/accounting/report/reports/income_statement.py b/src/accounting/report/reports/income_statement.py index cd788c3..1198ec7 100644 --- a/src/accounting/report/reports/income_statement.py +++ b/src/accounting/report/reports/income_statement.py @@ -29,6 +29,7 @@ from accounting.models import Currency, BaseAccount, Account, Transaction, \ from accounting.report.period import Period from .utils.base_report import BaseReport from .utils.csv_export import BaseCSVRow, csv_download, period_spec +from .utils.get_url import get_ledger_url from .utils.option_link import OptionLink from .utils.base_page_params import BasePageParams from .utils.period_choosers import IncomeStatementPeriodChooser @@ -278,23 +279,11 @@ class IncomeStatement(BaseReport): accounts: dict[int, Account] \ = {x.id: x for x in Account.query .filter(Account.id.in_([x.id for x in balances])).all()} - - def get_url(account: Account) -> str: - """Returns the ledger URL of an account. - - :param account: The account. - :return: The ledger URL of the account. - """ - if self.__period.is_default: - return url_for("accounting.report.ledger-default", - currency=self.__currency, account=account) - return url_for("accounting.report.ledger", - currency=self.__currency, account=account, - period=self.__period) - return [ReportAccount(account=accounts[x.id], amount=x.balance, - url=get_url(accounts[x.id])) + url=get_ledger_url(self.__currency, + accounts[x.id], + self.__period)) for x in balances] def csv(self) -> Response: diff --git a/src/accounting/report/reports/ledger.py b/src/accounting/report/reports/ledger.py index ce208c2..87d0baf 100644 --- a/src/accounting/report/reports/ledger.py +++ b/src/accounting/report/reports/ledger.py @@ -31,6 +31,7 @@ from accounting.report.period import Period from accounting.utils.pagination import Pagination from .utils.base_report import BaseReport from .utils.csv_export import BaseCSVRow, csv_download, period_spec +from .utils.get_url import get_ledger_url from .utils.option_link import OptionLink from .utils.base_page_params import BasePageParams from .utils.period_choosers import LedgerPeriodChooser @@ -290,15 +291,9 @@ class PageParams(BasePageParams): :return: The currency options. """ - def get_url(currency: Currency): - if self.period.is_default: - return url_for("accounting.report.ledger-default", - currency=currency, account=self.account) - return url_for("accounting.report.ledger", - 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_ledger_url(x, self.account, self.period), + self.currency) @property def account_options(self) -> list[OptionLink]: @@ -306,18 +301,12 @@ class PageParams(BasePageParams): :return: The account options. """ - def get_url(account: Account): - if self.period.is_default: - return url_for("accounting.report.ledger-default", - currency=self.currency, account=account) - return url_for("accounting.report.ledger", - currency=self.currency, account=account, - period=self.period) - in_use: sa.Select = sa.Select(JournalEntry.account_id)\ .filter(JournalEntry.currency_code == self.currency.code)\ .group_by(JournalEntry.account_id) - return [OptionLink(str(x), get_url(x), x.id == self.account.id) + return [OptionLink(str(x), get_ledger_url(self.currency, 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/trial_balance.py b/src/accounting/report/reports/trial_balance.py index 3ec0cf7..3b609ef 100644 --- a/src/accounting/report/reports/trial_balance.py +++ b/src/accounting/report/reports/trial_balance.py @@ -28,6 +28,7 @@ from accounting.models import Currency, Account, Transaction, JournalEntry from accounting.report.period import Period from .utils.base_report import BaseReport from .utils.csv_export import BaseCSVRow, csv_download, period_spec +from .utils.get_url import get_ledger_url from .utils.option_link import OptionLink from .utils.base_page_params import BasePageParams from .utils.period_choosers import TrialBalancePeriodChooser @@ -200,23 +201,11 @@ class TrialBalance(BaseReport): accounts: dict[int, Account] \ = {x.id: x for x in Account.query .filter(Account.id.in_([x.id for x in balances])).all()} - - def get_url(account: Account) -> str: - """Returns the ledger URL of an account. - - :param account: The account. - :return: The ledger URL of the account. - """ - if self.__period.is_default: - return url_for("accounting.report.ledger-default", - currency=self.__currency, account=account) - return url_for("accounting.report.ledger", - currency=self.__currency, account=account, - period=self.__period) - self.__accounts = [ReportAccount(account=accounts[x.id], amount=x.balance, - url=get_url(accounts[x.id])) + url=get_ledger_url(self.__currency, + accounts[x.id], + self.__period)) for x in balances] self.__total = Total( sum([x.debit for x in self.__accounts if x.debit is not None]), diff --git a/src/accounting/report/reports/utils/get_url.py b/src/accounting/report/reports/utils/get_url.py new file mode 100644 index 0000000..ef69d5b --- /dev/null +++ b/src/accounting/report/reports/utils/get_url.py @@ -0,0 +1,40 @@ +# The Mia! Accounting Flask Project. +# Author: imacat@mail.imacat.idv.tw (imacat), 2023/3/9 + +# Copyright (c) 2023 imacat. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""The utilities to get the ledger URL. + +""" +from flask import url_for + +from accounting.models import Currency, Account +from accounting.report.period import Period + + +def get_ledger_url(currency: Currency, account: Account, period: Period) \ + -> str: + """Returns the URL of a ledger. + + :param currency: The currency. + :param account: The account. + :param period: The period. + :return: The URL of the ledger. + """ + if period.is_default: + return url_for("accounting.report.ledger-default", + currency=currency, account=account) + return url_for("accounting.report.ledger", + 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 cc7c71b..a1b8e1c 100644 --- a/src/accounting/report/reports/utils/period_choosers.py +++ b/src/accounting/report/reports/utils/period_choosers.py @@ -30,6 +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 class PeriodChooser(ABC): @@ -131,12 +132,7 @@ class LedgerPeriodChooser(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.ledger-default", - currency=self.currency, account=self.account) - return url_for("accounting.report.ledger", - currency=self.currency, account=self.account, - period=period) + return get_ledger_url(self.currency, self.account, period) class IncomeExpensesPeriodChooser(PeriodChooser): diff --git a/src/accounting/report/reports/utils/report_chooser.py b/src/accounting/report/reports/utils/report_chooser.py index 64906fb..42c0208 100644 --- a/src/accounting/report/reports/utils/report_chooser.py +++ b/src/accounting/report/reports/utils/report_chooser.py @@ -31,6 +31,7 @@ from accounting.locale import gettext from accounting.models import Currency, Account from accounting.report.period import Period from accounting.template_globals import default_currency_code +from .get_url import get_ledger_url from .option_link import OptionLink from .report_type import ReportType @@ -98,13 +99,9 @@ class ReportChooser: :return: The ledger. """ - url: str = url_for("accounting.report.ledger-default", - currency=self.__currency, account=self.__account) \ - if self.__period.is_default \ - else url_for("accounting.report.ledger", - currency=self.__currency, account=self.__account, - period=self.__period) - return OptionLink(gettext("Ledger"), url, + return OptionLink(gettext("Ledger"), + get_ledger_url(self.__currency, self.__account, + self.__period), self.__active_report == ReportType.LEDGER, fa_icon="fa-solid fa-clipboard")