From 00734afa3ebc72d7652e9f789e86255f1aab4313 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Mon, 22 Apr 2024 23:25:30 +0900 Subject: [PATCH 1/2] init db in "migrate" cli command --- app/__init__.py | 25 ++++++++++++------------- manage.py | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 6ea9e53..5a186aa 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -90,6 +90,18 @@ def create_app(): connect_db(app.config) # 注册api蓝本 register_apis(app) + # 初始化插件 + babel.init_app(app) + apikit.init_app(app) + + logger.info("-" * 50) + logger.info("站点支持语言: " + str([str(i) for i in babel.list_translations()])) + oss.init(app.config) # 文件储存 + + return app + + +def init_db(app: Flask): # 初始化角色,语言 from app.models.language import Language from app.models.project import ProjectRole @@ -100,22 +112,9 @@ def create_app(): ProjectRole.init_system_roles() Language.init_system_languages() SiteSetting.init_site_setting() - # 初始化插件 - babel.init_app(app) - apikit.init_app(app) - - logger.info("-" * 50) - logger.info("站点支持语言: " + str([str(i) for i in babel.list_translations()])) - oss.init(app.config) # 文件储存 - admin_user = create_or_override_default_admin(app) create_default_team(admin_user) - # from app.tasks.ocr import recover_ocr_tasks - - # recover_ocr_tasks() - return app - def create_celery() -> Celery: # 为celery创建app diff --git a/manage.py b/manage.py index b5cb1e7..4a9fe7d 100644 --- a/manage.py +++ b/manage.py @@ -2,7 +2,18 @@ import click -from app import create_app +import logging + +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(levelname)s %(name)s %(message)s', datefmt='%Y-%m-%dT%H:%M:%S%z', + force=True +) + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +from app import create_app, init_db @click.group() @@ -19,12 +30,12 @@ def docs(): @click.command() -def run(): +def migrate(): """ - 运行开发服务器 + Initialize the database """ app = create_app() - app.run(host="0.0.0.0", port=5001) + init_db(app) @click.command() @@ -79,9 +90,9 @@ def local(action): ) -main.add_command(run) main.add_command(local) main.add_command(docs) +main.add_command(migrate) if __name__ == "__main__": main() From 20d329bdd7f943a811f891ee515c947835e754dd Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Mon, 22 Apr 2024 23:32:22 +0900 Subject: [PATCH 2/2] make ruff happy --- manage.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/manage.py b/manage.py index 4a9fe7d..0f6b6e3 100644 --- a/manage.py +++ b/manage.py @@ -4,17 +4,18 @@ import logging +from app import create_app, init_db + logging.basicConfig( level=logging.INFO, - format='%(asctime)s %(levelname)s %(name)s %(message)s', datefmt='%Y-%m-%dT%H:%M:%S%z', - force=True + format="%(asctime)s %(levelname)s %(name)s %(message)s", + datefmt="%Y-%m-%dT%H:%M:%S%z", + force=True, ) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) -from app import create_app, init_db - @click.group() def main():