From d68aa91c33174e9e01fa7b7e591e14ca3f6070ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Fri, 24 Feb 2023 17:17:18 +0800 Subject: [PATCH] Removed the redundant post_update methods from the AccountForm and CurrencyForm forms. --- src/accounting/account/forms.py | 11 ++++------- src/accounting/account/views.py | 5 ++++- src/accounting/currency/forms.py | 11 ----------- src/accounting/currency/views.py | 5 ++++- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/accounting/account/forms.py b/src/accounting/account/forms.py index a2b82a1..113149a 100644 --- a/src/accounting/account/forms.py +++ b/src/accounting/account/forms.py @@ -76,14 +76,15 @@ class AccountForm(FlaskForm): :return: None. """ is_new: bool = obj.id is None - prev_base_code: str | None = obj.base_code if is_new: obj.id = new_id(Account) - obj.base_code = self.base_code.data - if prev_base_code != self.base_code.data: + if obj.base_code != self.base_code.data: + if obj.base_code is not None: + sort_accounts_in(obj.base_code, obj.id) max_no: int | None = db.session.scalars( sa.select(sa.func.max(Account.no)) .filter(Account.base_code == self.base_code.data)).one() + obj.base_code = self.base_code.data obj.no = 1 if max_no is None else max_no + 1 obj.title = self.title.data obj.is_pay_off_needed = self.is_pay_off_needed.data @@ -91,10 +92,6 @@ class AccountForm(FlaskForm): current_user_pk: int = get_current_user_pk() obj.created_by_id = current_user_pk obj.updated_by_id = current_user_pk - if prev_base_code is not None \ - and prev_base_code != self.base_code.data: - setattr(self, "__post_update", - lambda: sort_accounts_in(prev_base_code, obj.id)) def post_update(self, obj: Account) -> None: """The post-processing after the update. diff --git a/src/accounting/account/views.py b/src/accounting/account/views.py index a1c0b7f..4054bd9 100644 --- a/src/accounting/account/views.py +++ b/src/accounting/account/views.py @@ -19,6 +19,7 @@ """ from urllib.parse import parse_qsl, urlencode +import sqlalchemy as sa from flask import Blueprint, render_template, session, redirect, flash, \ url_for, request from werkzeug.datastructures import ImmutableMultiDict @@ -29,6 +30,7 @@ from accounting.models import Account, BaseAccount from accounting.utils.next_uri import inherit_next, or_next from accounting.utils.pagination import Pagination from accounting.utils.permission import can_view, has_permission, can_edit +from accounting.utils.user import get_current_user_pk from .forms import AccountForm, sort_accounts_in, AccountReorderForm from .query import get_account_query @@ -143,7 +145,8 @@ def update_account(account: Account) -> redirect: flash(lazy_gettext("The account was not modified."), "success") return redirect(inherit_next(url_for("accounting.account.detail", account=account))) - form.post_update(account) + account.updated_by_id = get_current_user_pk() + account.updated_at = sa.func.now() db.session.commit() flash(lazy_gettext("The account is updated successfully."), "success") return redirect(inherit_next(url_for("accounting.account.detail", diff --git a/src/accounting/currency/forms.py b/src/accounting/currency/forms.py index 3dce475..8e0ca7c 100644 --- a/src/accounting/currency/forms.py +++ b/src/accounting/currency/forms.py @@ -19,7 +19,6 @@ """ from __future__ import annotations -import sqlalchemy as sa from flask_wtf import FlaskForm from wtforms import StringField, ValidationError from wtforms.validators import DataRequired, Regexp, NoneOf @@ -82,13 +81,3 @@ class CurrencyForm(FlaskForm): current_user_pk: int = get_current_user_pk() obj.created_by_id = current_user_pk obj.updated_by_id = current_user_pk - - def post_update(self, obj: Currency) -> None: - """The post-processing after the update. - - :param obj: The currency object. - :return: None - """ - current_user_pk: int = get_current_user_pk() - obj.updated_by_id = current_user_pk - obj.updated_at = sa.func.now() diff --git a/src/accounting/currency/views.py b/src/accounting/currency/views.py index e173334..f1323db 100644 --- a/src/accounting/currency/views.py +++ b/src/accounting/currency/views.py @@ -19,6 +19,7 @@ """ from urllib.parse import urlencode, parse_qsl +import sqlalchemy as sa from flask import Blueprint, render_template, redirect, session, request, \ flash, url_for from werkzeug.datastructures import ImmutableMultiDict @@ -29,6 +30,7 @@ from accounting.models import Currency 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.user import get_current_user_pk from .forms import CurrencyForm bp: Blueprint = Blueprint("currency", __name__) @@ -146,7 +148,8 @@ def update_currency(currency: Currency) -> redirect: flash(lazy_gettext("The currency was not modified."), "success") return redirect(inherit_next(url_for("accounting.currency.detail", currency=currency))) - form.post_update(currency) + currency.updated_by_id = get_current_user_pk() + currency.updated_at = sa.func.now() db.session.commit() flash(lazy_gettext("The currency is updated successfully."), "success") return redirect(inherit_next(url_for("accounting.currency.detail",