nex_docus/backend/scripts/create_nex_design_project.py

155 lines
4.7 KiB
Python
Raw Normal View History

2025-12-20 11:18:59 +00:00
"""
创建 NEX Design 项目
"""
import pymysql
import uuid
import os
from pathlib import Path
from datetime import datetime
# 数据库配置
DB_CONFIG = {
'host': '10.100.51.51',
'port': 3306,
'user': 'root',
'password': 'Unis@123',
'database': 'nex_docus',
'charset': 'utf8mb4',
}
# 项目信息
PROJECT_INFO = {
'name': 'NEX Design',
'description': 'NEX 设计文档库',
'owner_id': 1, # admin 用户的 ID
}
# 文件存储根目录
STORAGE_ROOT = '/data/nex_docus_store/projects'
def create_project():
"""创建项目"""
try:
# 生成 UUID
storage_key = str(uuid.uuid4())
print(f"生成项目 UUID: {storage_key}")
# 连接数据库
print("正在连接数据库...")
connection = pymysql.connect(**DB_CONFIG)
try:
with connection.cursor() as cursor:
# 1. 插入项目记录
print("创建项目记录...")
insert_project_sql = """
INSERT INTO `projects`
(`name`, `description`, `storage_key`, `owner_id`, `is_public`, `status`, `created_at`)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(insert_project_sql, (
PROJECT_INFO['name'],
PROJECT_INFO['description'],
storage_key,
PROJECT_INFO['owner_id'],
0, # 私有项目
1, # 活跃状态
datetime.now()
))
project_id = cursor.lastrowid
print(f"✓ 项目ID: {project_id}")
# 2. 添加项目成员admin 作为管理员)
print("添加项目管理员...")
insert_member_sql = """
INSERT INTO `project_members`
(`project_id`, `user_id`, `role`, `joined_at`)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(insert_member_sql, (
project_id,
PROJECT_INFO['owner_id'],
'admin',
datetime.now()
))
print("✓ 管理员已添加")
# 提交事务
connection.commit()
print("✓ 数据库记录创建成功")
finally:
connection.close()
# 3. 创建物理文件夹结构
print("\n创建项目文件夹...")
project_path = Path(STORAGE_ROOT) / storage_key
try:
# 创建项目根目录
project_path.mkdir(parents=True, exist_ok=True)
print(f"✓ 创建目录: {project_path}")
# 创建 _assets 目录
assets_dir = project_path / "_assets"
assets_dir.mkdir(exist_ok=True)
(assets_dir / "images").mkdir(exist_ok=True)
(assets_dir / "files").mkdir(exist_ok=True)
print(f"✓ 创建资源目录")
# 创建默认 README.md
readme_path = project_path / "README.md"
with open(readme_path, "w", encoding="utf-8") as f:
f.write(f"""# {PROJECT_INFO['name']}
{PROJECT_INFO['description']}
## 欢迎使用 NEX Docus
这是您的项目首页您可以在这里编写项目介绍使用说明等内容
### 快速开始
1. 在左侧目录树中创建文件夹和文档
2. 支持 Markdown 语法编写文档
3. 支持图片和附件上传
---
创建时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
""")
print(f"✓ 创建 README.md")
except Exception as e:
print(f"⚠️ 文件夹创建警告: {e}")
print(f" 请确保目录 {STORAGE_ROOT} 存在并有写入权限")
print(f" 或者修改 backend/.env 中的 STORAGE_ROOT 配置")
print("\n" + "="*60)
print("✅ 项目创建成功!")
print("="*60)
print(f"项目名称: {PROJECT_INFO['name']}")
print(f"项目ID: {project_id}")
print(f"存储路径: {project_path}")
print(f"UUID: {storage_key}")
print("="*60)
print("\n你现在可以:")
print("1. 启动后端服务: cd backend && python main.py")
2025-12-25 04:22:35 +00:00
print("2. 启动前端服务: cd frontend && npm run dev")
2025-12-20 11:18:59 +00:00
print("3. 登录系统 (admin / admin@123)")
print("4. 在项目中添加你的文档")
print()
except pymysql.Error as e:
print(f"❌ 数据库错误: {e}")
except Exception as e:
print(f"❌ 未知错误: {e}")
if __name__ == "__main__":
print("=" * 60)
print("创建 NEX Design 项目")
print("=" * 60)
print()
create_project()