Added the get_ledger_url utility to replace the common codes to retrieve the URL of a ledger.

This commit is contained in:
依瑪貓 2023-03-09 11:18:36 +08:00
parent 6d1e705e4b
commit 74b695c089
7 changed files with 65 additions and 76 deletions

View File

@ -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()

View File

@ -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:

View File

@ -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()]

View File

@ -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]),

View File

@ -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)

View File

@ -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):

View File

@ -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")