Added AccountConverter and apply it to the URL patterns in the accounting application.

This commit is contained in:
依瑪貓 2020-08-07 10:10:33 +08:00
parent 3b522af41b
commit 1a2bada988
2 changed files with 36 additions and 5 deletions

View File

@ -108,6 +108,36 @@ class DateConverter:
return value.strftime("%Y-%m-%d") return value.strftime("%Y-%m-%d")
class AccountConverter:
"""The path converter for the account."""
regex = "[1-9]{1,5}"
def to_python(self, value):
"""Returns the account by the account code.
Args:
value (str): The account code.
Returns:
Account: The account.
"""
try:
return Account.objects.get(code=value)
except Account.DoesNotExist:
raise ValueError
def to_url(self, value):
"""Returns the code of an account.
Args:
value (Account): The account.
Returns:
str: The account code.
"""
return value.code
class CashAccountConverter: class CashAccountConverter:
"""The path converter for the cash account.""" """The path converter for the cash account."""
regex = "0|(11|12|21|22)[1-9]{1,3}" regex = "0|(11|12|21|22)[1-9]{1,3}"

View File

@ -28,6 +28,7 @@ from mia_core.digest_auth import login_required
from . import converters, views from . import converters, views
register_converter(converters.PeriodConverter, "period") register_converter(converters.PeriodConverter, "period")
register_converter(converters.AccountConverter, "account")
register_converter(converters.CashAccountConverter, "cash-account") register_converter(converters.CashAccountConverter, "cash-account")
register_converter(converters.LedgerAccountConverter, "ledger-account") register_converter(converters.LedgerAccountConverter, "ledger-account")
register_converter(converters.TransactionTypeConverter, "txn-type") register_converter(converters.TransactionTypeConverter, "txn-type")
@ -91,7 +92,7 @@ urlpatterns = [
views.txn_sort, name="transactions.sort"), views.txn_sort, name="transactions.sort"),
# TODO: To be done # TODO: To be done
path("accounts", path("accounts",
mia_core_views.todo, name="accounts"), views.AccountListView.as_view(), name="accounts"),
# TODO: To be done # TODO: To be done
path("accounts/create", path("accounts/create",
mia_core_views.todo, name="accounts.create"), mia_core_views.todo, name="accounts.create"),
@ -101,15 +102,15 @@ urlpatterns = [
path("accounts/options", path("accounts/options",
views.account_options, name="accounts.options"), views.account_options, name="accounts.options"),
# TODO: To be done # TODO: To be done
path("accounts/<str:account_code>", path("accounts/<account:account>",
mia_core_views.todo, name="accounts.show"), mia_core_views.todo, name="accounts.show"),
# TODO: To be done # TODO: To be done
path("accounts/<str:account_code>/edit", path("accounts/<account:account>/edit",
mia_core_views.todo, name="accounts.edit"), mia_core_views.todo, name="accounts.edit"),
# TODO: To be done # TODO: To be done
path("accounts/<str:account_code>/update", path("accounts/<account:account>/update",
mia_core_views.todo, name="accounts.update"), mia_core_views.todo, name="accounts.update"),
# TODO: To be done # TODO: To be done
path("accounts/<str:account_code>/delete", path("accounts/<account:account>/delete",
mia_core_views.todo, name="accounts.delete"), mia_core_views.todo, name="accounts.delete"),
] ]