MaxKB/apps/system_manage/migrations/0005_resourcemapping.py
shaohuzhang1 78be028a0f
Some checks failed
sync2gitee / repo-sync (push) Has been cancelled
Typos Check / Spell Check with Typos (push) Has been cancelled
feat: Add resource mapping table (#4546)
2025-12-22 14:52:22 +08:00

75 lines
3.9 KiB
Python

# Generated by Django 5.2.8 on 2025-12-19 09:37
import uuid_utils.compat
from django.db import migrations, models
from knowledge.models import Knowledge
def resource_mapping(apps, schema_editor):
from system_manage.models.resource_mapping import ResourceMapping
from django.db.models import QuerySet
from application.flow.tools import get_workflow_resource, get_node_handle_callback, \
get_instance_resource
from system_manage.models.resource_mapping import ResourceType
from application.models import Application
from knowledge.models import KnowledgeWorkflow
resource_mapping_list = []
for application in QuerySet(Application):
workflow_mapping = get_workflow_resource(application.work_flow,
get_node_handle_callback(ResourceType.APPLICATION,
application.id))
instance_mapping = get_instance_resource(application, ResourceType.APPLICATION, str(application.id),
ResourceType.MODEL,
[lambda i: i.tts_model_id, lambda i: i.stt_model_id, ])
resource_mapping_list += workflow_mapping
resource_mapping_list += instance_mapping
knowledge_workflow_dict = {str(kw.knowledge_id): kw for kw in QuerySet(KnowledgeWorkflow)}
for knowledge in QuerySet(Knowledge):
knowledge_workflow = knowledge_workflow_dict.get(str(knowledge.id))
if knowledge_workflow:
workflow_mapping = get_workflow_resource(knowledge_workflow.work_flow,
get_node_handle_callback(ResourceType.KNOWLEDGE,
str(knowledge_workflow.knowledge_id)))
resource_mapping_list += workflow_mapping
instance_mapping = get_instance_resource(knowledge, ResourceType.KNOWLEDGE, str(knowledge.id),
ResourceType.MODEL,
[lambda i: i.embedding_model_id])
resource_mapping_list += instance_mapping
QuerySet(ResourceMapping).bulk_create(resource_mapping_list)
class Migration(migrations.Migration):
dependencies = [
('system_manage', '0004_alter_systemsetting_type_and_more'),
('knowledge', '0007_remove_knowledgeworkflowversion_workflow_and_more'),
('application', '0003_application_stt_model_params_setting_and_more'),
]
operations = [
migrations.CreateModel(
name='ResourceMapping',
fields=[
('create_time', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, db_index=True, verbose_name='修改时间')),
('id',
models.UUIDField(default=uuid_utils.compat.uuid7, editable=False, primary_key=True, serialize=False,
verbose_name='主键id')),
('source_type', models.CharField(
choices=[('KNOWLEDGE', '知识库'), ('APPLICATION', '应用'), ('TOOL', '工具'), ('MODEL', '模型')],
db_index=True, verbose_name='关联资源类型')),
('target_type', models.CharField(
choices=[('KNOWLEDGE', '知识库'), ('APPLICATION', '应用'), ('TOOL', '工具'), ('MODEL', '模型')],
db_index=True, verbose_name='被关联资源类型')),
('source_id', models.CharField(db_index=True, max_length=128, verbose_name='关联资源id')),
('target_id', models.CharField(db_index=True, max_length=128, verbose_name='被关联资源id')),
],
options={
'db_table': 'resource_mapping',
},
),
migrations.RunPython(resource_mapping)
]