1 Star 0 Fork 33

潘志群/码多多 全能知识库(Python 版)

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
database.py 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
zero 提交于 2025-05-26 15:35 +08:00 . init
# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import os
import importlib
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise
__all__ = ["register_db"]
def __loading_db_configs():
""" Load Db configuration """
configs = {}
try:
package = importlib.import_module("config")
clz = getattr(package, "GlobalSetting", None)
if not clz:
return configs
obj = clz().dict()
db_config = obj.get("DATABASES", {})
if db_config.get("apps"):
for k, v in db_config.get("apps").items():
if v and v.get("models") and isinstance(v.get("models"), str):
db_config["apps"][k]["models"] = __loading_model_files(v.get("models"))
return db_config
except ModuleNotFoundError:
return configs
def __loading_model_files(path: str):
""" Load Db models """
root_path: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + os.sep
mode_path: str = root_path + path.replace(".", os.sep)
all_files = []
for root, dirs, filenames in os.walk(mode_path):
dirs[:] = [d for d in dirs if not d.startswith("__")]
path_parts = os.path.relpath(root, mode_path).split(os.sep)
path_parts = [p for p in path_parts if not p.startswith("__")]
file_base_path = ".".join(path_parts) if path_parts else ""
for filename in filenames:
if not filename.startswith("__"):
filename_without_ext = os.path.splitext(filename)[0]
if file_base_path and file_base_path.endswith("."):
file_base_path = file_base_path[:-1]
file_path = f"{file_base_path}.{filename_without_ext}" if file_base_path else filename_without_ext
if not (file_path.startswith(".") and file_path != "."):
all_files.append(path + "." + file_path)
return all_files
def register_db(app: FastAPI):
""" Connect Databases """
register_tortoise(
app,
config=__loading_db_configs(),
generate_schemas=False,
add_exception_handlers=False,
)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pan_codee/ai-python.git
git@gitee.com:pan_codee/ai-python.git
pan_codee
ai-python
码多多 全能知识库(Python 版)
master

搜索帮助