动态类型列表上线
This commit is contained in:
@@ -4,8 +4,8 @@ Django版本的ES连接和操作模块
|
||||
"""
|
||||
from elasticsearch import Elasticsearch
|
||||
from elasticsearch_dsl import connections
|
||||
from .documents import AchievementDocument, UserDocument
|
||||
from .indexes import ACHIEVEMENT_INDEX_NAME, USER_INDEX_NAME
|
||||
from .documents import AchievementDocument, UserDocument, GlobalDocument
|
||||
from .indexes import ACHIEVEMENT_INDEX_NAME, USER_INDEX_NAME, GLOBAL_INDEX_NAME
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
@@ -17,6 +17,7 @@ es = connections.get_connection()
|
||||
|
||||
DATA_INDEX_NAME = ACHIEVEMENT_INDEX_NAME
|
||||
USERS_INDEX_NAME = USER_INDEX_NAME
|
||||
GLOBAL_TYPES_INDEX_NAME = GLOBAL_INDEX_NAME
|
||||
|
||||
def create_index_with_mapping():
|
||||
"""创建索引和映射配置(仅当索引不存在时)"""
|
||||
@@ -36,7 +37,25 @@ def create_index_with_mapping():
|
||||
else:
|
||||
print(f"ℹ️ 索引 {USERS_INDEX_NAME} 已存在,跳过创建")
|
||||
|
||||
# --- 3. 创建默认管理员用户(可选:也可检查用户是否已存在)---
|
||||
# --- 3. 处理全局类型索引 ---
|
||||
if not es.indices.exists(index=GLOBAL_TYPES_INDEX_NAME):
|
||||
GlobalDocument.init()
|
||||
default_types = ['软著', '专利', '奖状']
|
||||
doc = GlobalDocument(type_list=default_types)
|
||||
doc.meta.id = 'types'
|
||||
doc.save()
|
||||
print(f"✅ 创建索引 {GLOBAL_TYPES_INDEX_NAME} 并写入默认类型")
|
||||
else:
|
||||
try:
|
||||
GlobalDocument.get(id='types')
|
||||
except Exception:
|
||||
default_types = ['软著', '专利', '奖状']
|
||||
doc = GlobalDocument(type_list=default_types)
|
||||
doc.meta.id = 'types'
|
||||
doc.save()
|
||||
print("ℹ️ 全局类型文档缺失,已补充默认类型")
|
||||
|
||||
# --- 4. 创建默认管理员用户(可选:也可检查用户是否已存在)---
|
||||
# 这里简单处理:每次初始化都写入(可能重复),建议加唯一性判断
|
||||
admin_user = {
|
||||
"user_id": 0,
|
||||
@@ -57,6 +76,36 @@ def create_index_with_mapping():
|
||||
print(f"❌ 创建索引失败: {str(e)}")
|
||||
# raise # 可选:在 AppConfig 中捕获,这里可以 re-raise 便于调试
|
||||
|
||||
def get_type_list():
|
||||
try:
|
||||
doc = GlobalDocument.get(id='types')
|
||||
lst = [str(t).strip().strip(';') for t in (doc.type_list or [])]
|
||||
return lst
|
||||
except Exception:
|
||||
return ['软著', '专利', '奖状']
|
||||
|
||||
def ensure_type_in_list(type_name: str):
|
||||
if not type_name:
|
||||
return False
|
||||
norm = str(type_name).strip().strip(';')
|
||||
try:
|
||||
try:
|
||||
doc = GlobalDocument.get(id='types')
|
||||
cur = list(doc.type_list or [])
|
||||
except Exception:
|
||||
cur = ['软著', '专利', '奖状']
|
||||
doc = GlobalDocument(type_list=cur)
|
||||
doc.meta.id = 'types'
|
||||
cur_sanitized = {str(t).strip().strip(';') for t in cur}
|
||||
if norm not in cur_sanitized:
|
||||
cur.append(norm)
|
||||
doc.type_list = cur
|
||||
doc.save()
|
||||
return True
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def get_doc_id(data):
|
||||
"""
|
||||
根据数据内容生成唯一ID(用于去重)
|
||||
|
||||
Reference in New Issue
Block a user