a3eb5c7b2d
从百度网盘 /apps/bypy/dofile/ 下载的完整项目结构,包含: - dofile/: Flask 后端(端口 5000)+ 静态前端(端口 6002) - officefile/: 项目相关文档 - .gitignore: Python venv / 日志 / 缓存 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
127 lines
4.9 KiB
Python
127 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""初始化数据库并添加示例数据"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
# 确保可以导入app
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from app import app, db
|
|
from models import User, Article, Resource, TeamMember
|
|
from datetime import datetime
|
|
|
|
def init_data():
|
|
# 确保data目录存在
|
|
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
data_dir = os.path.join(base_dir, 'data')
|
|
os.makedirs(data_dir, exist_ok=True)
|
|
|
|
with app.app_context():
|
|
# 创建数据库表
|
|
db.create_all()
|
|
|
|
# 创建默认管理员
|
|
if not User.query.filter_by(username='admin').first():
|
|
admin = User(username='admin', email='admin@dael.edu.cn', is_admin=True)
|
|
admin.set_password('admin123')
|
|
db.session.add(admin)
|
|
print("Created admin user: admin/admin123")
|
|
|
|
# 创建示例用户
|
|
if not User.query.filter_by(username='user').first():
|
|
user = User(username='user', email='user@dael.edu.cn', is_admin=False)
|
|
user.set_password('user123')
|
|
db.session.add(user)
|
|
print("Created test user: user/user123")
|
|
|
|
# 创建示例文章(受保护)
|
|
if Article.query.count() == 0:
|
|
articles = [
|
|
Article(
|
|
title='实验室内部研究进展报告',
|
|
content='这是实验室的内部研究进展报告,包含详细的研究数据和成果分析...',
|
|
summary='2024年度实验室内部研究进展总结',
|
|
category='news',
|
|
is_protected=True,
|
|
author_id=1,
|
|
published=True
|
|
),
|
|
Article(
|
|
title='内部技术文档:生成式生态学算法详解',
|
|
content='本文档详细介绍了生成式生态学算法的实现原理和应用案例...',
|
|
summary='内部技术文档,仅供实验室成员查阅',
|
|
category='publication',
|
|
is_protected=True,
|
|
author_id=1,
|
|
published=True
|
|
)
|
|
]
|
|
for article in articles:
|
|
db.session.add(article)
|
|
print("Created sample protected articles")
|
|
|
|
# 创建示例资源(受保护)
|
|
if Resource.query.count() == 0:
|
|
resources = [
|
|
Resource(
|
|
name='内部数据集 v2.0',
|
|
description='实验室内部使用的完整数据集,包含所有研究数据',
|
|
file_path='/uploads/internal_dataset.zip',
|
|
file_size=1024 * 1024 * 500, # 500MB
|
|
file_type='zip',
|
|
category='dataset',
|
|
is_protected=True
|
|
),
|
|
Resource(
|
|
name='内部研究报告模板',
|
|
description='实验室内部研究报告的标准模板',
|
|
file_path='/uploads/report_template.docx',
|
|
file_size=1024 * 50, # 50KB
|
|
file_type='docx',
|
|
category='document',
|
|
is_protected=True
|
|
)
|
|
]
|
|
for resource in resources:
|
|
db.session.add(resource)
|
|
print("Created sample protected resources")
|
|
|
|
# 创建团队成员
|
|
if TeamMember.query.count() == 0:
|
|
members = [
|
|
TeamMember(
|
|
name='Dr. Lin Chen',
|
|
title='Principal Investigator / 实验室主任',
|
|
title_en='Principal Investigator',
|
|
bio='博士毕业于MIT Media Lab,专注于计算设计与人机交互领域的研究。',
|
|
bio_en='PhD from MIT Media Lab, focusing on computational design and HCI.',
|
|
role='professor',
|
|
email='lchen@dael.edu.cn',
|
|
order=1
|
|
),
|
|
TeamMember(
|
|
name='Sarah Wu',
|
|
title='Lead Ecologist / 生态学顾问',
|
|
title_en='Lead Ecologist',
|
|
bio='城市生态学专家,研究重点为城市微气候与生物多样性修复。',
|
|
bio_en='Urban ecology expert, focusing on microclimate and biodiversity restoration.',
|
|
role='faculty',
|
|
email='swu@dael.edu.cn',
|
|
order=2
|
|
)
|
|
]
|
|
for member in members:
|
|
db.session.add(member)
|
|
print("Created sample team members")
|
|
|
|
db.session.commit()
|
|
print("\nDatabase initialized successfully!")
|
|
print("\nDefault accounts:")
|
|
print(" Admin: admin / admin123")
|
|
print(" User: user / user123")
|
|
|
|
if __name__ == '__main__':
|
|
init_data()
|
|
|