From bfb08cf5fc70b39b4ae445cf2594de0da183c855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 6 Mar 2023 01:35:04 +0800 Subject: [PATCH] Changed the format_amount_input filter to accept None, and return an empty string if the value is None. --- .../transaction/expense/include/form-currency-item.html | 2 +- .../transaction/income/include/form-currency-item.html | 2 +- .../transaction/transfer/include/form-currency-item.html | 4 ++-- src/accounting/transaction/template_filters.py | 4 +++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/accounting/templates/accounting/transaction/expense/include/form-currency-item.html b/src/accounting/templates/accounting/transaction/expense/include/form-currency-item.html index 7187192..8a6a650 100644 --- a/src/accounting/templates/accounting/transaction/expense/include/form-currency-item.html +++ b/src/accounting/templates/accounting/transaction/expense/include/form-currency-item.html @@ -55,7 +55,7 @@ First written: 2023/2/25 account_text = entry_form.account_text, summary_data = "" if entry_form.summary.data is none else entry_form.summary.data, summary_errors = entry_form.summary.errors, - amount_data = "" if entry_form.amount.data is none else entry_form.amount.data|accounting_txn_format_amount_input, + amount_data = entry_form.amount.data|accounting_txn_format_amount_input, amount_errors = entry_form.amount.errors, amount_text = entry_form.amount.data|accounting_format_amount, entry_errors = entry_form.all_errors %} diff --git a/src/accounting/templates/accounting/transaction/income/include/form-currency-item.html b/src/accounting/templates/accounting/transaction/income/include/form-currency-item.html index c1d1217..aac5b6a 100644 --- a/src/accounting/templates/accounting/transaction/income/include/form-currency-item.html +++ b/src/accounting/templates/accounting/transaction/income/include/form-currency-item.html @@ -55,7 +55,7 @@ First written: 2023/2/25 account_text = entry_form.account_text, summary_data = "" if entry_form.summary.data is none else entry_form.summary.data, summary_errors = entry_form.summary.errors, - amount_data = "" if entry_form.amount.data is none else entry_form.amount.data|accounting_txn_format_amount_input, + amount_data = entry_form.amount.data|accounting_txn_format_amount_input, amount_errors = entry_form.amount.errors, amount_text = entry_form.amount.data|accounting_format_amount, entry_errors = entry_form.all_errors %} diff --git a/src/accounting/templates/accounting/transaction/transfer/include/form-currency-item.html b/src/accounting/templates/accounting/transaction/transfer/include/form-currency-item.html index 0278d76..d43afdc 100644 --- a/src/accounting/templates/accounting/transaction/transfer/include/form-currency-item.html +++ b/src/accounting/templates/accounting/transaction/transfer/include/form-currency-item.html @@ -57,7 +57,7 @@ First written: 2023/2/25 account_text = entry_form.account_text, summary_data = "" if entry_form.summary.data is none else entry_form.summary.data, summary_errors = entry_form.summary.errors, - amount_data = "" if entry_form.amount.data is none else entry_form.amount.data|accounting_txn_format_amount_input, + amount_data = entry_form.amount.data|accounting_txn_format_amount_input, amount_errors = entry_form.amount.errors, amount_text = entry_form.amount.data|accounting_format_amount, entry_errors = entry_form.all_errors %} @@ -97,7 +97,7 @@ First written: 2023/2/25 account_text = entry_form.account_text, summary_data = "" if entry_form.summary.data is none else entry_form.summary.data, summary_errors = entry_form.summary.errors, - amount_data = "" if entry_form.amount.data is none else entry_form.amount.data|accounting_txn_format_amount_input, + amount_data = entry_form.amount.data|accounting_txn_format_amount_input, amount_errors = entry_form.amount.errors, amount_text = entry_form.amount.data|accounting_format_amount, entry_errors = entry_form.all_errors %} diff --git a/src/accounting/transaction/template_filters.py b/src/accounting/transaction/template_filters.py index 4409e22..e23d8ac 100644 --- a/src/accounting/transaction/template_filters.py +++ b/src/accounting/transaction/template_filters.py @@ -57,12 +57,14 @@ def to_transfer(uri: str) -> str: return urlunparse(parts) -def format_amount_input(value: Decimal) -> str: +def format_amount_input(value: Decimal | None) -> str: """Format an amount for an input value. :param value: The amount. :return: The formatted amount text for an input value. """ + if value is None: + return "" whole: int = int(value) frac: Decimal = (value - whole).normalize() return str(whole) + str(frac)[1:]