Renamed the TransactionType class to TransactionOperator.

This commit is contained in:
依瑪貓 2023-03-04 19:36:07 +08:00
parent 9833bac6e4
commit f65dc6fc42
2 changed files with 33 additions and 33 deletions

View File

@ -14,7 +14,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""The view dispatcher for different transaction types. """The operators for different transaction types.
""" """
import typing as t import typing as t
@ -30,10 +30,10 @@ from .forms import TransactionForm, IncomeTransactionForm, \
ExpenseTransactionForm, TransferTransactionForm ExpenseTransactionForm, TransferTransactionForm
class TransactionType(ABC): class TransactionOperator(ABC):
"""An abstract transaction type.""" """The base transaction operator."""
CHECK_ORDER: int = -1 CHECK_ORDER: int = -1
"""The order when checking the transaction type.""" """The order when checking the transaction operator."""
@property @property
@abstractmethod @abstractmethod
@ -90,10 +90,10 @@ class TransactionType(ABC):
entry_index="ENTRY_INDEX") entry_index="ENTRY_INDEX")
class IncomeTransaction(TransactionType): class IncomeTransaction(TransactionOperator):
"""An income transaction.""" """An income transaction."""
CHECK_ORDER: int = 2 CHECK_ORDER: int = 2
"""The order when checking the transaction type.""" """The order when checking the transaction operator."""
@property @property
def form(self) -> t.Type[TransactionForm]: def form(self) -> t.Type[TransactionForm]:
@ -159,10 +159,10 @@ class IncomeTransaction(TransactionType):
credit_total="-") credit_total="-")
class ExpenseTransaction(TransactionType): class ExpenseTransaction(TransactionOperator):
"""An expense transaction.""" """An expense transaction."""
CHECK_ORDER: int = 1 CHECK_ORDER: int = 1
"""The order when checking the transaction type.""" """The order when checking the transaction operator."""
@property @property
def form(self) -> t.Type[TransactionForm]: def form(self) -> t.Type[TransactionForm]:
@ -228,10 +228,10 @@ class ExpenseTransaction(TransactionType):
debit_total="-") debit_total="-")
class TransferTransaction(TransactionType): class TransferTransaction(TransactionOperator):
"""A transfer transaction.""" """A transfer transaction."""
CHECK_ORDER: int = 3 CHECK_ORDER: int = 3
"""The order when checking the transaction type.""" """The order when checking the transaction operator."""
@property @property
def form(self) -> t.Type[TransactionForm]: def form(self) -> t.Type[TransactionForm]:
@ -297,17 +297,17 @@ class TransferTransaction(TransactionType):
debit_total="-", credit_total="-") debit_total="-", credit_total="-")
TXN_ENUM_TO_OP: dict[TransactionTypeEnum, TransactionType] \ TXN_ENUM_TO_OP: dict[TransactionTypeEnum, TransactionOperator] \
= {TransactionTypeEnum.CASH_INCOME: IncomeTransaction(), = {TransactionTypeEnum.CASH_INCOME: IncomeTransaction(),
TransactionTypeEnum.CASH_EXPENSE: ExpenseTransaction(), TransactionTypeEnum.CASH_EXPENSE: ExpenseTransaction(),
TransactionTypeEnum.TRANSFER: TransferTransaction()} TransactionTypeEnum.TRANSFER: TransferTransaction()}
"""The map from the transaction type enum to its operator.""" """The map from the transaction types to their operators."""
def get_txn_type_op(txn: Transaction) -> TransactionType: def get_txn_op(txn: Transaction) -> TransactionOperator:
"""Returns the transaction type operator that may be specified in the "as" """Returns the transaction operator that may be specified in the "as" query
query parameter. If it is not specified, check the transaction type from parameter. If it is not specified, check the transaction type from the
the transaction. transaction.
:param txn: The transaction. :param txn: The transaction.
:return: None. :return: None.

View File

@ -34,7 +34,7 @@ from accounting.utils.pagination import Pagination
from accounting.utils.permission import has_permission, can_view, can_edit from accounting.utils.permission import has_permission, can_view, can_edit
from accounting.utils.txn_types import TransactionTypeEnum from accounting.utils.txn_types import TransactionTypeEnum
from accounting.utils.user import get_current_user_pk from accounting.utils.user import get_current_user_pk
from .dispatcher import TransactionType, TXN_ENUM_TO_OP, get_txn_type_op from .dispatcher import TransactionOperator, TXN_ENUM_TO_OP, get_txn_op
from .forms import sort_transactions_in, TransactionReorderForm from .forms import sort_transactions_in, TransactionReorderForm
from .queries import get_transaction_query from .queries import get_transaction_query
from .template_filters import with_type, to_transfer, format_amount_input, \ from .template_filters import with_type, to_transfer, format_amount_input, \
@ -71,15 +71,15 @@ def show_add_transaction_form(txn_type: TransactionTypeEnum) -> str:
:param txn_type: The transaction type. :param txn_type: The transaction type.
:return: The form to add a transaction. :return: The form to add a transaction.
""" """
txn_type_op: TransactionType = TXN_ENUM_TO_OP[txn_type] txn_op: TransactionOperator = TXN_ENUM_TO_OP[txn_type]
form: txn_type_op.form form: txn_op.form
if "form" in session: if "form" in session:
form = txn_type_op.form(ImmutableMultiDict(parse_qsl(session["form"]))) form = txn_op.form(ImmutableMultiDict(parse_qsl(session["form"])))
del session["form"] del session["form"]
form.validate() form.validate()
else: else:
form = txn_type_op.form() form = txn_op.form()
return txn_type_op.render_create_template(form) return txn_op.render_create_template(form)
@bp.post("/store/<transactionType:txn_type>", endpoint="store") @bp.post("/store/<transactionType:txn_type>", endpoint="store")
@ -91,8 +91,8 @@ def add_transaction(txn_type: TransactionTypeEnum) -> redirect:
:return: The redirection to the transaction detail on success, or the :return: The redirection to the transaction detail on success, or the
transaction creation form on error. transaction creation form on error.
""" """
txn_type_op: TransactionType = TXN_ENUM_TO_OP[txn_type] txn_op: TransactionOperator = TXN_ENUM_TO_OP[txn_type]
form: txn_type_op.form = txn_type_op.form(request.form) form: txn_op.form = txn_op.form(request.form)
if not form.validate(): if not form.validate():
flash_form_errors(form) flash_form_errors(form)
session["form"] = urlencode(list(request.form.items())) session["form"] = urlencode(list(request.form.items()))
@ -114,8 +114,8 @@ def show_transaction_detail(txn: Transaction) -> str:
:param txn: The transaction. :param txn: The transaction.
:return: The detail. :return: The detail.
""" """
txn_type_op: TransactionType = get_txn_type_op(txn) txn_op: TransactionOperator = get_txn_op(txn)
return txn_type_op.render_detail_template(txn) return txn_op.render_detail_template(txn)
@bp.get("/<transaction:txn>/edit", endpoint="edit") @bp.get("/<transaction:txn>/edit", endpoint="edit")
@ -126,15 +126,15 @@ def show_transaction_edit_form(txn: Transaction) -> str:
:param txn: The transaction. :param txn: The transaction.
:return: The form to edit the transaction. :return: The form to edit the transaction.
""" """
txn_type_op: TransactionType = get_txn_type_op(txn) txn_op: TransactionOperator = get_txn_op(txn)
form: txn_type_op.form form: txn_op.form
if "form" in session: if "form" in session:
form = txn_type_op.form(ImmutableMultiDict(parse_qsl(session["form"]))) form = txn_op.form(ImmutableMultiDict(parse_qsl(session["form"])))
del session["form"] del session["form"]
form.validate() form.validate()
else: else:
form = txn_type_op.form(obj=txn) form = txn_op.form(obj=txn)
return txn_type_op.render_edit_template(txn, form) return txn_op.render_edit_template(txn, form)
@bp.post("/<transaction:txn>/update", endpoint="update") @bp.post("/<transaction:txn>/update", endpoint="update")
@ -146,8 +146,8 @@ def update_transaction(txn: Transaction) -> redirect:
:return: The redirection to the transaction detail on success, or the :return: The redirection to the transaction detail on success, or the
transaction edit form on error. transaction edit form on error.
""" """
txn_type_op: TransactionType = get_txn_type_op(txn) txn_op: TransactionOperator = get_txn_op(txn)
form: txn_type_op.form = txn_type_op.form(request.form) form: txn_op.form = txn_op.form(request.form)
if not form.validate(): if not form.validate():
flash_form_errors(form) flash_form_errors(form)
session["form"] = urlencode(list(request.form.items())) session["form"] = urlencode(list(request.form.items()))