更新
This commit is contained in:
@@ -294,6 +294,32 @@ def init_superuser():
|
||||
print(f"超级管理员账号创建成功: {superuser.username}")
|
||||
return superuser
|
||||
|
||||
def init_demo_user():
|
||||
from hertz_studio_django_auth.models import HertzUser, HertzUserRole, HertzRole
|
||||
print("正在初始化普通用户账号...")
|
||||
if HertzUser.objects.filter(username='demo').exists():
|
||||
print("普通用户账号已存在,跳过创建")
|
||||
user = HertzUser.objects.get(username='demo')
|
||||
else:
|
||||
user = HertzUser.objects.create_user(
|
||||
username='demo',
|
||||
email='demo@hertz.com',
|
||||
password='123456',
|
||||
real_name='普通用户',
|
||||
status=1
|
||||
)
|
||||
print(f"普通用户账号创建成功: {user.username}")
|
||||
try:
|
||||
role = HertzRole.objects.get(role_id=3)
|
||||
user_role, created = HertzUserRole.objects.get_or_create(user=user, role=role)
|
||||
if created:
|
||||
print(f"为用户 {user.username} 分配角色ID: {role.role_id}")
|
||||
else:
|
||||
print(f"用户 {user.username} 已拥有角色ID: {role.role_id}")
|
||||
except HertzRole.DoesNotExist:
|
||||
print("角色ID=3不存在,跳过分配")
|
||||
return user
|
||||
|
||||
|
||||
def init_departments():
|
||||
"""
|
||||
@@ -607,41 +633,6 @@ def assign_user_roles(superuser, roles):
|
||||
print(f"用户 {superuser.username} 已拥有角色: {super_admin_role.role_name}")
|
||||
|
||||
|
||||
def init_yolo_data():
|
||||
"""
|
||||
初始化YOLO模块数据
|
||||
"""
|
||||
print("初始化YOLO模块数据...")
|
||||
|
||||
try:
|
||||
from hertz_studio_django_yolo.models import YoloModelConfig
|
||||
|
||||
# 检查是否已存在默认模型配置
|
||||
if YoloModelConfig.objects.filter(is_default=True).exists():
|
||||
print("✅ YOLO默认模型配置已存在")
|
||||
return
|
||||
|
||||
# 创建默认YOLO模型配置
|
||||
default_model = YoloModelConfig.objects.create(
|
||||
model_name="YOLOv12古建筑检测模型",
|
||||
model_path="static/models/yolov12/weights/best.pt",
|
||||
model_version="1.0.0",
|
||||
description="用于古建筑识别的YOLOv12模型",
|
||||
confidence_threshold=0.5,
|
||||
iou_threshold=0.45,
|
||||
max_detections=1000,
|
||||
input_size="640",
|
||||
class_labels='["古塔", "古桥", "古建筑", "传统建筑", "文物建筑"]',
|
||||
is_active=True,
|
||||
is_default=True
|
||||
)
|
||||
|
||||
print(f"✅ 创建默认YOLO模型配置: {default_model.model_name}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 初始化YOLO模块数据失败: {e}")
|
||||
|
||||
|
||||
def sync_generated_menus():
|
||||
"""
|
||||
同步代码生成器生成的菜单权限
|
||||
@@ -883,7 +874,6 @@ def init_database():
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
# 1. 初始化超级管理员
|
||||
superuser = init_superuser()
|
||||
|
||||
# 2. 初始化部门
|
||||
@@ -904,11 +894,11 @@ def init_database():
|
||||
# 7. 为生成的菜单分配权限
|
||||
assign_generated_menu_permissions(generated_menus)
|
||||
|
||||
# 8. 分配用户角色
|
||||
assign_user_roles(superuser, roles)
|
||||
demo_user = init_demo_user()
|
||||
|
||||
# 9. 初始化YOLO模块数据
|
||||
init_yolo_data()
|
||||
# init_yolo_data()
|
||||
|
||||
# 10. 创建菜单生成器命令行工具
|
||||
create_menu_generator_command()
|
||||
@@ -927,13 +917,15 @@ def init_database():
|
||||
print(f"密码: hertz")
|
||||
print(f"邮箱: admin@hertz.com")
|
||||
print("")
|
||||
print("YOLO模型配置:")
|
||||
print(f"模型路径: static/models/yolov12/weights/best.pt")
|
||||
print("")
|
||||
print("菜单生成器工具:")
|
||||
print(f"使用命令: python generate_menu.py crud <模块名> <模型名>")
|
||||
print("")
|
||||
print("请妥善保管管理员账号信息!")
|
||||
print("")
|
||||
print("普通用户账号信息:")
|
||||
print("用户名: demo")
|
||||
print("密码: 123456")
|
||||
|
||||
except Exception as e:
|
||||
print(f"数据库初始化失败: {str(e)}")
|
||||
@@ -1097,7 +1089,6 @@ def check_database_exists():
|
||||
db_path = Path(db_config['NAME'])
|
||||
return db_path.exists()
|
||||
else:
|
||||
# 对于其他数据库类型,尝试连接来检查
|
||||
try:
|
||||
from django.db import connection
|
||||
connection.ensure_connection()
|
||||
@@ -1105,6 +1096,31 @@ def check_database_exists():
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def create_mysql_database_if_missing():
|
||||
from django.conf import settings
|
||||
db = settings.DATABASES['default']
|
||||
if db['ENGINE'] != 'django.db.backends.mysql':
|
||||
return False
|
||||
name = db['NAME']
|
||||
host = db.get('HOST') or 'localhost'
|
||||
user = db.get('USER') or 'root'
|
||||
password = db.get('PASSWORD') or ''
|
||||
port = int(db.get('PORT') or 3306)
|
||||
try:
|
||||
import MySQLdb
|
||||
except Exception:
|
||||
return False
|
||||
try:
|
||||
conn = MySQLdb.connect(host=host, user=user, passwd=password, port=port)
|
||||
cur = conn.cursor()
|
||||
cur.execute(f"CREATE DATABASE IF NOT EXISTS `{name}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
|
||||
conn.commit()
|
||||
cur.close()
|
||||
conn.close()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def run_migrations():
|
||||
"""
|
||||
@@ -1187,6 +1203,7 @@ def main():
|
||||
print("📊 步骤1: 检查数据库状态...")
|
||||
if not check_database_exists():
|
||||
print("❌ 数据库不存在,需要创建")
|
||||
created = create_mysql_database_if_missing()
|
||||
need_migration = True
|
||||
else:
|
||||
print("✅ 数据库文件存在")
|
||||
|
||||
Reference in New Issue
Block a user