From a67158f8f64b966a9c6e50fb53a064782f68cac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Tue, 14 Mar 2023 21:48:11 +0800 Subject: [PATCH] Moved the CodeUnique validator from an inner class of the CurrencyForm form to an independent class, and removed the annotation future import from the "accounting.currency.forms" module. --- src/accounting/currency/forms.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/accounting/currency/forms.py b/src/accounting/currency/forms.py index 8e0ca7c..b3dff4d 100644 --- a/src/accounting/currency/forms.py +++ b/src/accounting/currency/forms.py @@ -17,8 +17,6 @@ """The forms for the currency management. """ -from __future__ import annotations - from flask_wtf import FlaskForm from wtforms import StringField, ValidationError from wtforms.validators import DataRequired, Regexp, NoneOf @@ -30,22 +28,25 @@ from accounting.utils.strip_text import strip_text from accounting.utils.user import get_current_user_pk +class CodeUnique: + """The validator to check if the code is unique.""" + + def __call__(self, form: FlaskForm, field: StringField) -> None: + if field.data == "": + return + if not isinstance(form, CurrencyForm): + return + if form.obj_code is not None and form.obj_code == field.data: + return + if db.session.get(Currency, field.data) is not None: + raise ValidationError(lazy_gettext( + "Code conflicts with another currency.")) + + class CurrencyForm(FlaskForm): """The form to create or edit a currency.""" CODE_BLOCKLIST: list[str] = ["create", "store", "exists-code"] """The reserved codes that are not available.""" - - class CodeUnique: - """The validator to check if the code is unique.""" - def __call__(self, form: CurrencyForm, field: StringField) -> None: - if field.data == "": - return - if form.obj_code is not None and form.obj_code == field.data: - return - if db.session.get(Currency, field.data) is not None: - raise ValidationError(lazy_gettext( - "Code conflicts with another currency.")) - code = StringField( filters=[strip_text], validators=[DataRequired(lazy_gettext("Please fill in the code.")),