From 7ccc96bda0c7cc49c98c0b3a670329e16365ea5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 22 Mar 2023 23:12:21 +0800 Subject: [PATCH] Added the CurrentAccountExists and AccountNotCurrent validators to the default_ie_account_code field of the OptionForm form, to validate that the current account exists and is a current account. --- src/accounting/option/forms.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/accounting/option/forms.py b/src/accounting/option/forms.py index ac49cd7..ebea1d1 100644 --- a/src/accounting/option/forms.py +++ b/src/accounting/option/forms.py @@ -32,6 +32,28 @@ from accounting.utils.options import Options from accounting.utils.strip_text import strip_text +class CurrentAccountExists: + """The validator to check that the current account exists.""" + + def __call__(self, form: FlaskForm, field: StringField) -> None: + if field.data is None or field.data == CurrentAccount.CURRENT_AL_CODE: + return + if Account.find_by_code(field.data) is None: + raise ValidationError(lazy_gettext( + "The current account does not exist.")) + + +class AccountNotCurrent: + """The validator to check that the account is a current account.""" + + def __call__(self, form: FlaskForm, field: StringField) -> None: + if field.data is None or field.data == CurrentAccount.CURRENT_AL_CODE: + return + if field.data[:2] not in {"11", "12", "21", "22"}: + raise ValidationError(lazy_gettext( + "The current account does not exist.")) + + class NotStartPayableFromExpense: """The validator to check that a payable line item does not start from expense.""" @@ -219,7 +241,9 @@ class OptionForm(FlaskForm): validators=[ DataRequired(lazy_gettext( "Please select the default account" - " for the income and expenses log."))]) + " for the income and expenses log.")), + CurrentAccountExists(), + AccountNotCurrent()]) """The default account code for the income and expenses log.""" recurring = FormField(RecurringForm) """The recurring expenses and incomes."""