Removed the redundant post_update methods from the AccountForm and CurrencyForm forms.

This commit is contained in:
依瑪貓 2023-02-24 17:17:18 +08:00
parent 3f63fb0bda
commit d68aa91c33
4 changed files with 12 additions and 20 deletions

View File

@ -76,14 +76,15 @@ class AccountForm(FlaskForm):
:return: None. :return: None.
""" """
is_new: bool = obj.id is None is_new: bool = obj.id is None
prev_base_code: str | None = obj.base_code
if is_new: if is_new:
obj.id = new_id(Account) obj.id = new_id(Account)
obj.base_code = self.base_code.data if obj.base_code != self.base_code.data:
if prev_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( max_no: int | None = db.session.scalars(
sa.select(sa.func.max(Account.no)) sa.select(sa.func.max(Account.no))
.filter(Account.base_code == self.base_code.data)).one() .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.no = 1 if max_no is None else max_no + 1
obj.title = self.title.data obj.title = self.title.data
obj.is_pay_off_needed = self.is_pay_off_needed.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() current_user_pk: int = get_current_user_pk()
obj.created_by_id = current_user_pk obj.created_by_id = current_user_pk
obj.updated_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: def post_update(self, obj: Account) -> None:
"""The post-processing after the update. """The post-processing after the update.

View File

@ -19,6 +19,7 @@
""" """
from urllib.parse import parse_qsl, urlencode from urllib.parse import parse_qsl, urlencode
import sqlalchemy as sa
from flask import Blueprint, render_template, session, redirect, flash, \ from flask import Blueprint, render_template, session, redirect, flash, \
url_for, request url_for, request
from werkzeug.datastructures import ImmutableMultiDict 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.next_uri import inherit_next, or_next
from accounting.utils.pagination import Pagination from accounting.utils.pagination import Pagination
from accounting.utils.permission import can_view, has_permission, can_edit 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 .forms import AccountForm, sort_accounts_in, AccountReorderForm
from .query import get_account_query 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") flash(lazy_gettext("The account was not modified."), "success")
return redirect(inherit_next(url_for("accounting.account.detail", return redirect(inherit_next(url_for("accounting.account.detail",
account=account))) account=account)))
form.post_update(account) account.updated_by_id = get_current_user_pk()
account.updated_at = sa.func.now()
db.session.commit() db.session.commit()
flash(lazy_gettext("The account is updated successfully."), "success") flash(lazy_gettext("The account is updated successfully."), "success")
return redirect(inherit_next(url_for("accounting.account.detail", return redirect(inherit_next(url_for("accounting.account.detail",

View File

@ -19,7 +19,6 @@
""" """
from __future__ import annotations from __future__ import annotations
import sqlalchemy as sa
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, ValidationError from wtforms import StringField, ValidationError
from wtforms.validators import DataRequired, Regexp, NoneOf from wtforms.validators import DataRequired, Regexp, NoneOf
@ -82,13 +81,3 @@ class CurrencyForm(FlaskForm):
current_user_pk: int = get_current_user_pk() current_user_pk: int = get_current_user_pk()
obj.created_by_id = current_user_pk obj.created_by_id = current_user_pk
obj.updated_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()

View File

@ -19,6 +19,7 @@
""" """
from urllib.parse import urlencode, parse_qsl from urllib.parse import urlencode, parse_qsl
import sqlalchemy as sa
from flask import Blueprint, render_template, redirect, session, request, \ from flask import Blueprint, render_template, redirect, session, request, \
flash, url_for flash, url_for
from werkzeug.datastructures import ImmutableMultiDict 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.next_uri import inherit_next, or_next
from accounting.utils.pagination import Pagination from accounting.utils.pagination import Pagination
from accounting.utils.permission import has_permission, can_view, can_edit from accounting.utils.permission import has_permission, can_view, can_edit
from accounting.utils.user import get_current_user_pk
from .forms import CurrencyForm from .forms import CurrencyForm
bp: Blueprint = Blueprint("currency", __name__) bp: Blueprint = Blueprint("currency", __name__)
@ -146,7 +148,8 @@ def update_currency(currency: Currency) -> redirect:
flash(lazy_gettext("The currency was not modified."), "success") flash(lazy_gettext("The currency was not modified."), "success")
return redirect(inherit_next(url_for("accounting.currency.detail", return redirect(inherit_next(url_for("accounting.currency.detail",
currency=currency))) currency=currency)))
form.post_update(currency) currency.updated_by_id = get_current_user_pk()
currency.updated_at = sa.func.now()
db.session.commit() db.session.commit()
flash(lazy_gettext("The currency is updated successfully."), "success") flash(lazy_gettext("The currency is updated successfully."), "success")
return redirect(inherit_next(url_for("accounting.currency.detail", return redirect(inherit_next(url_for("accounting.currency.detail",