diff --git a/docs/source/accounting.rst b/docs/source/accounting.rst index 86acafd..a28cd51 100644 --- a/docs/source/accounting.rst +++ b/docs/source/accounting.rst @@ -15,14 +15,6 @@ Subpackages Submodules ---------- -accounting.database module --------------------------- - -.. automodule:: accounting.database - :members: - :undoc-members: - :show-inheritance: - accounting.locale module ------------------------ diff --git a/src/accounting/__init__.py b/src/accounting/__init__.py index 41dfbcb..7519d91 100644 --- a/src/accounting/__init__.py +++ b/src/accounting/__init__.py @@ -21,9 +21,12 @@ import typing as t from pathlib import Path from flask import Flask, Blueprint +from flask_sqlalchemy import SQLAlchemy from accounting.utils.user import AbstractUserUtils +db: SQLAlchemy = SQLAlchemy() +"""The database instance.""" data_dir: Path = Path(__file__).parent / "data" """The data directory.""" @@ -45,8 +48,8 @@ def init_app(app: Flask, user_utils: AbstractUserUtils, """ # The database instance must be set before loading everything # in the application. - from .database import set_db - set_db(app.extensions["sqlalchemy"]) + global db + db = app.extensions["sqlalchemy"] from .utils.user import init_user_utils init_user_utils(user_utils) diff --git a/src/accounting/account/commands.py b/src/accounting/account/commands.py index ea4785d..1634b8d 100644 --- a/src/accounting/account/commands.py +++ b/src/accounting/account/commands.py @@ -24,7 +24,7 @@ from secrets import randbelow import click from flask.cli import with_appcontext -from accounting.database import db +from accounting import db from accounting.models import BaseAccount, Account, AccountL10n from accounting.utils.user import has_user, get_user_pk diff --git a/src/accounting/account/forms.py b/src/accounting/account/forms.py index 52accbd..752c7d8 100644 --- a/src/accounting/account/forms.py +++ b/src/accounting/account/forms.py @@ -23,7 +23,7 @@ from flask_wtf import FlaskForm from wtforms import StringField, BooleanField from wtforms.validators import DataRequired, ValidationError -from accounting.database import db +from accounting import db from accounting.locale import lazy_gettext from accounting.models import BaseAccount, Account from accounting.utils.random_id import new_id diff --git a/src/accounting/account/views.py b/src/accounting/account/views.py index d284264..84029f4 100644 --- a/src/accounting/account/views.py +++ b/src/accounting/account/views.py @@ -23,7 +23,7 @@ from flask import Blueprint, render_template, session, redirect, flash, \ url_for, request from werkzeug.datastructures import ImmutableMultiDict -from accounting.database import db +from accounting import db from accounting.locale import lazy_gettext from accounting.models import Account, BaseAccount from accounting.utils.next_url import inherit_next, or_next diff --git a/src/accounting/base_account/commands.py b/src/accounting/base_account/commands.py index 9699cd8..1621108 100644 --- a/src/accounting/base_account/commands.py +++ b/src/accounting/base_account/commands.py @@ -23,7 +23,7 @@ import click from flask.cli import with_appcontext from accounting import data_dir -from accounting.database import db +from accounting import db from accounting.models import BaseAccount, BaseAccountL10n diff --git a/src/accounting/base_account/converters.py b/src/accounting/base_account/converters.py index 10fe5b2..7180ac7 100644 --- a/src/accounting/base_account/converters.py +++ b/src/accounting/base_account/converters.py @@ -20,7 +20,7 @@ from flask import abort from werkzeug.routing import BaseConverter -from accounting.database import db +from accounting import db from accounting.models import BaseAccount diff --git a/src/accounting/currency/commands.py b/src/accounting/currency/commands.py index 6a151fb..5c640cf 100644 --- a/src/accounting/currency/commands.py +++ b/src/accounting/currency/commands.py @@ -24,8 +24,7 @@ import typing as t import click from flask.cli import with_appcontext -from accounting import data_dir -from accounting.database import db +from accounting import db, data_dir from accounting.models import Currency, CurrencyL10n from accounting.utils.user import has_user, get_user_pk diff --git a/src/accounting/currency/converters.py b/src/accounting/currency/converters.py index d696b65..55c6a70 100644 --- a/src/accounting/currency/converters.py +++ b/src/accounting/currency/converters.py @@ -20,7 +20,7 @@ from flask import abort from werkzeug.routing import BaseConverter -from accounting.database import db +from accounting import db from accounting.models import Currency diff --git a/src/accounting/currency/forms.py b/src/accounting/currency/forms.py index f8cbff0..1f0bc3a 100644 --- a/src/accounting/currency/forms.py +++ b/src/accounting/currency/forms.py @@ -24,7 +24,7 @@ from flask_wtf import FlaskForm from wtforms import StringField, ValidationError from wtforms.validators import DataRequired, Regexp, NoneOf -from accounting.database import db +from accounting import db from accounting.locale import lazy_gettext from accounting.models import Currency from accounting.utils.strip_text import strip_text diff --git a/src/accounting/currency/views.py b/src/accounting/currency/views.py index ba76169..a6ee82b 100644 --- a/src/accounting/currency/views.py +++ b/src/accounting/currency/views.py @@ -23,7 +23,7 @@ from flask import Blueprint, render_template, redirect, session, request, \ flash, url_for from werkzeug.datastructures import ImmutableMultiDict -from accounting.database import db +from accounting import db from accounting.locale import lazy_gettext from accounting.models import Currency from accounting.utils.next_url import inherit_next, or_next diff --git a/src/accounting/database.py b/src/accounting/database.py deleted file mode 100644 index 652763b..0000000 --- a/src/accounting/database.py +++ /dev/null @@ -1,39 +0,0 @@ -# The Mia! Accounting Flask Project. -# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/25 - -# Copyright (c) 2023 imacat. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""The database instance factory for the base account management. - -This is to overcome the problem that the database instance needs to be -initialized at compile time, but as a submodule it is only available at run -time. - -""" - -from flask_sqlalchemy import SQLAlchemy - -db: SQLAlchemy = SQLAlchemy() -"""The database instance.""" - - -def set_db(new_db: SQLAlchemy) -> None: - """Sets the database instance. - - :param new_db: The database instance. - :return: None. - """ - global db - db = new_db diff --git a/src/accounting/models.py b/src/accounting/models.py index 942d54a..1371574 100644 --- a/src/accounting/models.py +++ b/src/accounting/models.py @@ -25,7 +25,7 @@ from flask import current_app from flask_babel import get_locale from sqlalchemy import text -from accounting.database import db +from accounting import db from accounting.utils.user import user_cls, user_pk_column diff --git a/src/accounting/utils/random_id.py b/src/accounting/utils/random_id.py index 9c4ecc0..fbb7835 100644 --- a/src/accounting/utils/random_id.py +++ b/src/accounting/utils/random_id.py @@ -22,7 +22,7 @@ This module should not import any other module from the application. import typing as t from secrets import randbelow -from accounting.database import db +from accounting import db def new_id(cls: t.Type): diff --git a/tests/test_account.py b/tests/test_account.py index 92b1090..88c6432 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -75,7 +75,7 @@ class AccountCommandTestCase(unittest.TestCase): runner: FlaskCliRunner = self.app.test_cli_runner() with self.app.app_context(): - from accounting.database import db + from accounting import db from accounting.models import BaseAccount, Account, AccountL10n result: Result result = runner.invoke(args="init-db") @@ -129,7 +129,7 @@ class AccountTestCase(unittest.TestCase): runner: FlaskCliRunner = self.app.test_cli_runner() with self.app.app_context(): - from accounting.database import db + from accounting import db from accounting.models import BaseAccount, Account, AccountL10n result: Result result = runner.invoke(args="init-db") @@ -316,7 +316,7 @@ class AccountTestCase(unittest.TestCase): :return: None. """ - from accounting.database import db + from accounting import db from accounting.models import Account create_uri: str = f"{PREFIX}/create" store_uri: str = f"{PREFIX}/store" @@ -648,7 +648,7 @@ class AccountTestCase(unittest.TestCase): :return: None. """ - from accounting.database import db + from accounting import db from accounting.models import Account response: httpx.Response diff --git a/tests/test_currency.py b/tests/test_currency.py index df186dd..bd5710c 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -71,7 +71,7 @@ class CurrencyCommandTestCase(unittest.TestCase): runner: FlaskCliRunner = self.app.test_cli_runner() with self.app.app_context(): - from accounting.database import db + from accounting import db from accounting.models import Currency, CurrencyL10n result: Result result = runner.invoke(args="init-db") @@ -128,7 +128,7 @@ class CurrencyTestCase(unittest.TestCase): runner: FlaskCliRunner = self.app.test_cli_runner() with self.app.app_context(): - from accounting.database import db + from accounting import db from accounting.models import Currency, CurrencyL10n result: Result result = runner.invoke(args="init-db")