Added the UnappliedAccountConverter path converter to only allow the accounts that need offsets.

This commit is contained in:
依瑪貓 2023-04-08 07:15:58 +08:00
parent c984d2d596
commit e9824808ec
3 changed files with 30 additions and 2 deletions

View File

@ -27,9 +27,11 @@ def init_app(app: Flask, url_prefix: str) -> None:
:param url_prefix: The URL prefix of the accounting application. :param url_prefix: The URL prefix of the accounting application.
:return: None. :return: None.
""" """
from .converters import PeriodConverter, CurrentAccountConverter from .converters import PeriodConverter, CurrentAccountConverter, \
UnappliedAccountConverter
app.url_map.converters["period"] = PeriodConverter app.url_map.converters["period"] = PeriodConverter
app.url_map.converters["currentAccount"] = CurrentAccountConverter app.url_map.converters["currentAccount"] = CurrentAccountConverter
app.url_map.converters["unappliedAccount"] = UnappliedAccountConverter
from .views import bp as report_bp from .views import bp as report_bp
app.register_blueprint(report_bp, url_prefix=url_prefix) app.register_blueprint(report_bp, url_prefix=url_prefix)

View File

@ -77,3 +77,29 @@ class CurrentAccountConverter(BaseConverter):
:return: Its code. :return: Its code.
""" """
return value.code return value.code
class UnappliedAccountConverter(BaseConverter):
"""The converter to convert the unapplied original line item account code
from and to the corresponding account in the routes."""
def to_python(self, value: str) -> Account:
"""Converts an account code to an account.
:param value: The account code.
:return: The corresponding account.
"""
account: Account | None = Account.find_by_code(value)
if account is None:
abort(404)
if not account.is_need_offset:
abort(404)
return account
def to_url(self, value: Account) -> str:
"""Converts an account to account code.
:param value: The account.
:return: Its code.
"""
return value.code

View File

@ -286,7 +286,7 @@ def __get_balance_sheet(currency: Currency, period: Period) \
return report.html() return report.html()
@bp.get("unapplied/<account:account>", endpoint="unapplied") @bp.get("unapplied/<unappliedAccount:account>", endpoint="unapplied")
@has_permission(can_view) @has_permission(can_view)
def get_unapplied(account: Account) -> str | Response: def get_unapplied(account: Account) -> str | Response:
"""Returns the unapplied original line items. """Returns the unapplied original line items.