xuyonghao
2024-12-26 4faa732b1bd55335b142d5454833a8d5432675fe
resources_type表同步
1个文件已修改
1个文件已添加
51 ■■■■■ 已修改文件
app/config/env_conf/resource_type.json 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/task/fetch_agent.py 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/config/env_conf/resource_type.json
New file
@@ -0,0 +1,23 @@
[
  {
    "id": "0",
    "created_at": null,
    "updated_at": null,
    "name": "菜单",
    "description": "菜单类型会显示在系统首页左侧菜单中"
  },
  {
    "id": "1",
    "created_at": null,
    "updated_at": null,
    "name": "功能",
    "description": "功能类型不会显示在系统首页左侧菜单中"
  },
  {
    "id": "3",
    "created_at": null,
    "updated_at": null,
    "name": "目录",
    "description": null
  }
]
app/task/fetch_agent.py
@@ -14,7 +14,7 @@
from app.models.user_model import UserAppModel
from app.models.agent_model import AgentModel
from app.models.base_model import SessionLocal, Base
from app.models.resource_model import ResourceModel
from app.models.resource_model import ResourceModel, ResourceTypeModel
from app.service.v2.app_register import AppRegisterDao
# 创建数据库引擎和会话工厂
@@ -431,6 +431,18 @@
        db.close()
def import_type_table(session: Session, node: dict, parent=None):
    resource_type = ResourceTypeModel(
        id=node['id'],
        name=node['name'],
        description=node.get('description')
    )
    if parent:
        resource_type.parent = parent
    session.add(resource_type)
    session.commit()
def import_tree(session: Session, node: dict, parent=None):
    resource = ResourceModel(
        id=node['id'],
@@ -460,6 +472,18 @@
def sync_resources_from_json():
    db = SessionLocal()
    try:
        if db.query(ResourceTypeModel).count() == 0:
            with open(os.path.join(ENV_CONF_PATH, "resource_type.json"), 'r', encoding='utf-8') as file:
                type_json_data = json.load(file)
            db.query(ResourceTypeModel).delete()
            db.commit()
            for node in type_json_data:
                import_type_table(db, node)
            print("add resourceType record successfully")
        else:
            print("sync resources successfully")
        if db.query(ResourceModel).count() == 0:
            with open(os.path.join(ENV_CONF_PATH, "resource.json"), 'r', encoding='utf-8') as file:
                json_data = json.load(file)
@@ -473,7 +497,7 @@
        else:
            print("sync resources successfully")
    except Exception as e:
        print(f"Failed to sync resources: {str(e)}")
        print(f"Failed to sync resources or resource type: {str(e)}")
    finally:
        db.close()