Added the TransactionTypeEnum in the new "accounting.utils.txn_types" module to remove the dependency from the "accounting.report" module to the "accounting.transaction" module.

This commit is contained in:
2023-03-04 19:30:04 +08:00
parent 7d412b20d7
commit 9833bac6e4
8 changed files with 94 additions and 79 deletions

View File

@ -32,8 +32,9 @@ from accounting.utils.flash_errors import flash_form_errors
from accounting.utils.next_uri import inherit_next, or_next
from accounting.utils.pagination import Pagination
from accounting.utils.permission import has_permission, can_view, can_edit
from accounting.utils.txn_types import TransactionTypeEnum
from accounting.utils.user import get_current_user_pk
from .dispatcher import TransactionType, get_txn_type, TXN_TYPE_OBJ
from .dispatcher import TransactionType, TXN_ENUM_TO_OP, get_txn_type_op
from .forms import sort_transactions_in, TransactionReorderForm
from .queries import get_transaction_query
from .template_filters import with_type, to_transfer, format_amount_input, \
@ -59,37 +60,39 @@ def list_transactions() -> str:
pagination: Pagination = Pagination[Transaction](transactions)
return render_template("accounting/transaction/list.html",
list=pagination.list, pagination=pagination,
types=TXN_TYPE_OBJ)
txn_types=TransactionTypeEnum)
@bp.get("/create/<transactionType:txn_type>", endpoint="create")
@has_permission(can_edit)
def show_add_transaction_form(txn_type: TransactionType) -> str:
def show_add_transaction_form(txn_type: TransactionTypeEnum) -> str:
"""Shows the form to add a transaction.
:param txn_type: The transaction type.
:return: The form to add a transaction.
"""
form: txn_type.form
txn_type_op: TransactionType = TXN_ENUM_TO_OP[txn_type]
form: txn_type_op.form
if "form" in session:
form = txn_type.form(ImmutableMultiDict(parse_qsl(session["form"])))
form = txn_type_op.form(ImmutableMultiDict(parse_qsl(session["form"])))
del session["form"]
form.validate()
else:
form = txn_type.form()
return txn_type.render_create_template(form)
form = txn_type_op.form()
return txn_type_op.render_create_template(form)
@bp.post("/store/<transactionType:txn_type>", endpoint="store")
@has_permission(can_edit)
def add_transaction(txn_type: TransactionType) -> redirect:
def add_transaction(txn_type: TransactionTypeEnum) -> redirect:
"""Adds a transaction.
:param txn_type: The transaction type.
:return: The redirection to the transaction detail on success, or the
transaction creation form on error.
"""
form: txn_type.form = txn_type.form(request.form)
txn_type_op: TransactionType = TXN_ENUM_TO_OP[txn_type]
form: txn_type_op.form = txn_type_op.form(request.form)
if not form.validate():
flash_form_errors(form)
session["form"] = urlencode(list(request.form.items()))
@ -111,8 +114,8 @@ def show_transaction_detail(txn: Transaction) -> str:
:param txn: The transaction.
:return: The detail.
"""
txn_type: TransactionType = get_txn_type(txn)
return txn_type.render_detail_template(txn)
txn_type_op: TransactionType = get_txn_type_op(txn)
return txn_type_op.render_detail_template(txn)
@bp.get("/<transaction:txn>/edit", endpoint="edit")
@ -123,15 +126,15 @@ def show_transaction_edit_form(txn: Transaction) -> str:
:param txn: The transaction.
:return: The form to edit the transaction.
"""
txn_type: TransactionType = get_txn_type(txn)
form: txn_type.form
txn_type_op: TransactionType = get_txn_type_op(txn)
form: txn_type_op.form
if "form" in session:
form = txn_type.form(ImmutableMultiDict(parse_qsl(session["form"])))
form = txn_type_op.form(ImmutableMultiDict(parse_qsl(session["form"])))
del session["form"]
form.validate()
else:
form = txn_type.form(obj=txn)
return txn_type.render_edit_template(txn, form)
form = txn_type_op.form(obj=txn)
return txn_type_op.render_edit_template(txn, form)
@bp.post("/<transaction:txn>/update", endpoint="update")
@ -143,8 +146,8 @@ def update_transaction(txn: Transaction) -> redirect:
:return: The redirection to the transaction detail on success, or the
transaction edit form on error.
"""
txn_type: TransactionType = get_txn_type(txn)
form: txn_type.form = txn_type.form(request.form)
txn_type_op: TransactionType = get_txn_type_op(txn)
form: txn_type_op.form = txn_type_op.form(request.form)
if not form.validate():
flash_form_errors(form)
session["form"] = urlencode(list(request.form.items()))