Renamed the TransactionTypeEnum enum to TransactionType.

This commit is contained in:
依瑪貓 2023-03-04 19:39:13 +08:00
parent 10fbc3f638
commit 6bed180790
5 changed files with 29 additions and 29 deletions

View File

@ -28,7 +28,7 @@ from flask_sqlalchemy.query import Query
from accounting import db
from accounting.models import JournalEntry, Transaction, Account, Currency
from accounting.utils.pagination import Pagination
from accounting.utils.txn_types import TransactionTypeEnum
from accounting.utils.txn_types import TransactionType
from .period import Period
from .period_choosers import PeriodChooser, \
JournalPeriodChooser
@ -105,12 +105,12 @@ class JournalEntryReport(ABC):
"""
@property
def txn_types(self) -> t.Type[TransactionTypeEnum]:
def txn_types(self) -> t.Type[TransactionType]:
"""Returns the transaction types.
:return: The transaction types.
"""
return TransactionTypeEnum
return TransactionType
def as_csv_download(self) -> Response:
"""Returns the journal entries as CSV download.

View File

@ -24,7 +24,7 @@ from werkzeug.routing import BaseConverter
from accounting import db
from accounting.models import Transaction
from accounting.utils.txn_types import TransactionTypeEnum
from accounting.utils.txn_types import TransactionType
class TransactionConverter(BaseConverter):
@ -55,20 +55,20 @@ class TransactionTypeConverter(BaseConverter):
"""The transaction converter to convert the transaction type ID from and to
the corresponding transaction type in the routes."""
def to_python(self, value: str) -> TransactionTypeEnum:
def to_python(self, value: str) -> TransactionType:
"""Converts a transaction ID to a transaction.
:param value: The transaction ID.
:return: The corresponding transaction.
"""
type_dict: dict[str, TransactionTypeEnum] \
= {x.value: x for x in TransactionTypeEnum}
txn_type: TransactionTypeEnum | None = type_dict.get(value)
type_dict: dict[str, TransactionType] \
= {x.value: x for x in TransactionType}
txn_type: TransactionType | None = type_dict.get(value)
if txn_type is None:
abort(404)
return txn_type
def to_url(self, value: TransactionTypeEnum) -> str:
def to_url(self, value: TransactionType) -> str:
"""Converts a transaction type to its ID.
:param value: The transaction type.

View File

@ -25,7 +25,7 @@ from flask_wtf import FlaskForm
from accounting.models import Transaction
from accounting.template_globals import default_currency_code
from accounting.utils.txn_types import TransactionTypeEnum
from accounting.utils.txn_types import TransactionType
from .forms import TransactionForm, IncomeTransactionForm, \
ExpenseTransactionForm, TransferTransactionForm
@ -111,7 +111,7 @@ class IncomeTransaction(TransactionOperator):
"""
return render_template("accounting/transaction/income/create.html",
form=form,
txn_type=TransactionTypeEnum.CASH_INCOME,
txn_type=TransactionType.CASH_INCOME,
currency_template=self.__currency_template,
entry_template=self._entry_template)
@ -180,7 +180,7 @@ class ExpenseTransaction(TransactionOperator):
"""
return render_template("accounting/transaction/expense/create.html",
form=form,
txn_type=TransactionTypeEnum.CASH_EXPENSE,
txn_type=TransactionType.CASH_EXPENSE,
currency_template=self.__currency_template,
entry_template=self._entry_template)
@ -249,7 +249,7 @@ class TransferTransaction(TransactionOperator):
"""
return render_template("accounting/transaction/transfer/create.html",
form=form,
txn_type=TransactionTypeEnum.TRANSFER,
txn_type=TransactionType.TRANSFER,
currency_template=self.__currency_template,
entry_template=self._entry_template)
@ -297,10 +297,10 @@ class TransferTransaction(TransactionOperator):
debit_total="-", credit_total="-")
TXN_ENUM_TO_OP: dict[TransactionTypeEnum, TransactionOperator] \
= {TransactionTypeEnum.CASH_INCOME: IncomeTransaction(),
TransactionTypeEnum.CASH_EXPENSE: ExpenseTransaction(),
TransactionTypeEnum.TRANSFER: TransferTransaction()}
TXN_TYPE_TO_OP: dict[TransactionType, TransactionOperator] \
= {TransactionType.CASH_INCOME: IncomeTransaction(),
TransactionType.CASH_EXPENSE: ExpenseTransaction(),
TransactionType.TRANSFER: TransferTransaction()}
"""The map from the transaction types to their operators."""
@ -313,12 +313,12 @@ def get_txn_op(txn: Transaction) -> TransactionOperator:
:return: None.
"""
if "as" in request.args:
type_dict: dict[str, TransactionTypeEnum] \
= {x.value: x for x in TransactionTypeEnum}
type_dict: dict[str, TransactionType] \
= {x.value: x for x in TransactionType}
if request.args["as"] not in type_dict:
abort(404)
return TXN_ENUM_TO_OP[type_dict[request.args["as"]]]
for txn_type in sorted(TXN_ENUM_TO_OP.values(),
return TXN_TYPE_TO_OP[type_dict[request.args["as"]]]
for txn_type in sorted(TXN_TYPE_TO_OP.values(),
key=lambda x: x.CHECK_ORDER):
if txn_type.is_my_type(txn):
return txn_type

View File

@ -32,9 +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.txn_types import TransactionType
from accounting.utils.user import get_current_user_pk
from .operators import TransactionOperator, TXN_ENUM_TO_OP, get_txn_op
from .operators import TransactionOperator, TXN_TYPE_TO_OP, get_txn_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, \
@ -60,18 +60,18 @@ def list_transactions() -> str:
pagination: Pagination = Pagination[Transaction](transactions)
return render_template("accounting/transaction/list.html",
list=pagination.list, pagination=pagination,
txn_types=TransactionTypeEnum)
txn_types=TransactionType)
@bp.get("/create/<transactionType:txn_type>", endpoint="create")
@has_permission(can_edit)
def show_add_transaction_form(txn_type: TransactionTypeEnum) -> str:
def show_add_transaction_form(txn_type: TransactionType) -> str:
"""Shows the form to add a transaction.
:param txn_type: The transaction type.
:return: The form to add a transaction.
"""
txn_op: TransactionOperator = TXN_ENUM_TO_OP[txn_type]
txn_op: TransactionOperator = TXN_TYPE_TO_OP[txn_type]
form: txn_op.form
if "form" in session:
form = txn_op.form(ImmutableMultiDict(parse_qsl(session["form"])))
@ -84,14 +84,14 @@ def show_add_transaction_form(txn_type: TransactionTypeEnum) -> str:
@bp.post("/store/<transactionType:txn_type>", endpoint="store")
@has_permission(can_edit)
def add_transaction(txn_type: TransactionTypeEnum) -> redirect:
def add_transaction(txn_type: TransactionType) -> 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.
"""
txn_op: TransactionOperator = TXN_ENUM_TO_OP[txn_type]
txn_op: TransactionOperator = TXN_TYPE_TO_OP[txn_type]
form: txn_op.form = txn_op.form(request.form)
if not form.validate():
flash_form_errors(form)

View File

@ -20,7 +20,7 @@
from enum import Enum
class TransactionTypeEnum(Enum):
class TransactionType(Enum):
"""The transaction types."""
CASH_INCOME: str = "income"
"""The cash income transaction."""