完善: CSRF安全修复 + 后台任务管理 + 压测全模块覆盖 + 文档
This commit is contained in:
+286
@@ -0,0 +1,286 @@
|
||||
import sys
|
||||
sys.path.insert(0, '.')
|
||||
from orm import init_db
|
||||
from auth import User
|
||||
from models import Announcement, ForumTopic, ForumReply, KnowledgeArticle, Task, TaskSubmission, Category
|
||||
|
||||
init_db('db/liteadmin.db', [User, Announcement, ForumTopic, ForumReply, KnowledgeArticle, Task, TaskSubmission, Category])
|
||||
|
||||
# 任务一:批量创建测试账号
|
||||
departments = {
|
||||
'生产部': ('shengchan', 10),
|
||||
'技术部': ('jishu', 10),
|
||||
'质量部': ('zhiliang', 10),
|
||||
}
|
||||
|
||||
for dept_name, (prefix, count) in departments.items():
|
||||
for i in range(1, count + 1):
|
||||
username = f'{prefix}{i}'
|
||||
existing = User.filter(username=username).first()
|
||||
if existing:
|
||||
continue
|
||||
role = 'editor' if i == 1 else 'viewer'
|
||||
hashed = User.hash_password('123456')
|
||||
User.create(
|
||||
username=username,
|
||||
password=hashed,
|
||||
display_name=f'{dept_name}员工{i}',
|
||||
department=dept_name,
|
||||
role=role,
|
||||
level='lv1',
|
||||
status=1
|
||||
)
|
||||
print(f'创建用户: {username} ({dept_name}, {role})')
|
||||
|
||||
print('测试账号创建完成!')
|
||||
|
||||
# 任务三:创建测试内容数据
|
||||
import random
|
||||
|
||||
# 获取各部门 editor 用户
|
||||
shengchan_editor = User.filter(username='shengchan1').first()
|
||||
jishu_editor = User.filter(username='jishu1').first()
|
||||
zhiliang_editor = User.filter(username='zhiliang1').first()
|
||||
admin = User.filter(username='admin').first()
|
||||
|
||||
# 3 条公告(1 条置顶)
|
||||
announcements_data = [
|
||||
{'title': '平台正式上线公告', 'content': '欢迎大家使用社区平台!本平台提供论坛交流、知识库、任务管理等功能。', 'is_pinned': 1},
|
||||
{'title': '系统维护通知', 'content': '本周六凌晨 2:00-4:00 进行系统维护,届时服务将暂时不可用。', 'is_pinned': 0},
|
||||
{'title': '新功能上线预告', 'content': '下个版本将推出更多实用功能,敬请期待!', 'is_pinned': 0},
|
||||
]
|
||||
for ann in announcements_data:
|
||||
existing = Announcement.filter(title=ann['title']).first()
|
||||
if not existing:
|
||||
Announcement.create(
|
||||
title=ann['title'],
|
||||
content=ann['content'],
|
||||
status=1,
|
||||
is_pinned=ann['is_pinned'],
|
||||
created_by=admin.id if admin else 1,
|
||||
)
|
||||
print(f'创建公告: {ann["title"]}')
|
||||
|
||||
# 每个部门 3 个论坛话题(共 9 条,1 条置顶)
|
||||
forum_topics_data = {
|
||||
'生产部': [
|
||||
{'title': '【置顶】生产部工作交流区', 'content': '本版块用于生产部同事交流工作经验。', 'category': '综合', 'is_pinned': 1},
|
||||
{'title': '设备故障报修流程', 'content': '请大家按照标准流程报修设备故障。', 'category': '技术', 'is_pinned': 0},
|
||||
{'title': '生产安全注意事项', 'content': '安全生产,人人有责。', 'category': '生活', 'is_pinned': 0},
|
||||
],
|
||||
'技术部': [
|
||||
{'title': '【置顶】技术部技术讨论区', 'content': '本版块用于技术部同事技术交流。', 'category': '技术', 'is_pinned': 1},
|
||||
{'title': '新技术分享', 'content': '最近学习了一些新技术,分享给大家。', 'category': '学习', 'is_pinned': 0},
|
||||
{'title': '代码审查规范', 'content': '请大家遵守代码审查规范。', 'category': '技术', 'is_pinned': 0},
|
||||
],
|
||||
'质量部': [
|
||||
{'title': '【置顶】质量部工作交流区', 'content': '本版块用于质量部同事交流工作。', 'category': '综合', 'is_pinned': 1},
|
||||
{'title': '质量检测标准讨论', 'content': '关于最新质量检测标准的讨论。', 'category': '技术', 'is_pinned': 0},
|
||||
{'title': '质量改进建议', 'content': '欢迎大家提出质量改进建议。', 'category': '生活', 'is_pinned': 0},
|
||||
],
|
||||
}
|
||||
dept_users = {
|
||||
'生产部': shengchan_editor,
|
||||
'技术部': jishu_editor,
|
||||
'质量部': zhiliang_editor,
|
||||
}
|
||||
for dept_name, topics in forum_topics_data.items():
|
||||
author = dept_users[dept_name]
|
||||
for topic in topics:
|
||||
existing = ForumTopic.filter(title=topic['title']).first()
|
||||
if not existing:
|
||||
ForumTopic.create(
|
||||
title=topic['title'],
|
||||
content=topic['content'],
|
||||
category=topic['category'],
|
||||
author_id=author.id if author else 1,
|
||||
author_name=author.username if author else 'admin',
|
||||
status=1,
|
||||
is_pinned=topic['is_pinned'],
|
||||
)
|
||||
print(f'创建论坛话题: {topic["title"]}')
|
||||
|
||||
# 每个部门 3 篇知识文章(共 9 条,1 条置顶)
|
||||
knowledge_data = {
|
||||
'生产部': [
|
||||
{'title': '【置顶】生产部操作手册', 'content': '生产部标准操作手册,请各位同事认真学习。', 'category': '技术', 'is_pinned': 1},
|
||||
{'title': '设备维护指南', 'content': '定期设备维护指南。', 'category': '技术', 'is_pinned': 0},
|
||||
{'title': '安全生产规范', 'content': '安全生产规范文档。', 'category': '学习', 'is_pinned': 0},
|
||||
],
|
||||
'技术部': [
|
||||
{'title': '【置顶】技术部开发规范', 'content': '技术部开发规范文档。', 'category': '技术', 'is_pinned': 1},
|
||||
{'title': '系统架构文档', 'content': '系统整体架构说明。', 'category': '技术', 'is_pinned': 0},
|
||||
{'title': 'API 接口文档', 'content': 'API 接口使用说明。', 'category': '学习', 'is_pinned': 0},
|
||||
],
|
||||
'质量部': [
|
||||
{'title': '【置顶】质量部检验标准', 'content': '质量检验标准文档。', 'category': '技术', 'is_pinned': 1},
|
||||
{'title': '质量数据分析方法', 'content': '质量数据分析方法介绍。', 'category': '学习', 'is_pinned': 0},
|
||||
{'title': '质量管理体系', 'content': '质量管理体系文件。', 'category': '综合', 'is_pinned': 0},
|
||||
],
|
||||
}
|
||||
for dept_name, articles in knowledge_data.items():
|
||||
author = dept_users[dept_name]
|
||||
for article in articles:
|
||||
existing = KnowledgeArticle.filter(title=article['title']).first()
|
||||
if not existing:
|
||||
KnowledgeArticle.create(
|
||||
title=article['title'],
|
||||
content=article['content'],
|
||||
category=article['category'],
|
||||
author_id=author.id if author else 1,
|
||||
author_name=author.username if author else 'admin',
|
||||
status=1,
|
||||
is_pinned=article['is_pinned'],
|
||||
)
|
||||
print(f'创建知识文章: {article["title"]}')
|
||||
|
||||
# 每个部门 2 个任务(共 6 个)
|
||||
tasks_data = {
|
||||
'生产部': [
|
||||
{'name': '生产线巡检', 'description': '对生产线进行日常巡检,检查设备运行状态。'},
|
||||
{'name': '生产数据统计', 'description': '统计本月生产数据,提交生产报表。'},
|
||||
],
|
||||
'技术部': [
|
||||
{'name': '系统性能优化', 'description': '对系统进行性能分析和优化。'},
|
||||
{'name': '技术文档编写', 'description': '编写技术文档和接口说明。'},
|
||||
],
|
||||
'质量部': [
|
||||
{'name': '质量抽检', 'description': '对本月产品进行质量抽检。'},
|
||||
{'name': '质量报告编写', 'description': '编写本月质量分析报告。'},
|
||||
],
|
||||
}
|
||||
for dept_name, tasks in tasks_data.items():
|
||||
creator = dept_users[dept_name]
|
||||
for task in tasks:
|
||||
existing = Task.filter(name=task['name']).first()
|
||||
if not existing:
|
||||
Task.create(
|
||||
name=task['name'],
|
||||
description=task['description'],
|
||||
creator_id=creator.id if creator else 1,
|
||||
creator_name=creator.username if creator else 'admin',
|
||||
department=dept_name,
|
||||
)
|
||||
print(f'创建任务: {task["name"]}')
|
||||
|
||||
# 每个部门的 editor 账号对同部门任务提交信息
|
||||
for dept_name, tasks in tasks_data.items():
|
||||
editor = dept_users[dept_name]
|
||||
for task_data in tasks:
|
||||
task = Task.filter(name=task_data['name']).first()
|
||||
if task:
|
||||
existing_sub = TaskSubmission.filter(task_id=task.id, user_id=editor.id).first()
|
||||
if not existing_sub:
|
||||
TaskSubmission.create(
|
||||
task_id=task.id,
|
||||
user_id=editor.id,
|
||||
user_name=editor.username,
|
||||
department=dept_name,
|
||||
content=f'{editor.display_name} 已完成任务:{task.name}',
|
||||
)
|
||||
print(f'创建任务提交: {editor.username} -> {task.name}')
|
||||
|
||||
print('测试内容数据创建完成!')
|
||||
|
||||
# 批量生成更多数据用于观察翻页效果
|
||||
print('正在生成翻页测试数据...')
|
||||
|
||||
# 额外论坛话题 30 条
|
||||
for i in range(1, 31):
|
||||
title = '论坛话题 #{}:这是一篇关于技术交流的讨论帖'.format(10 + i)
|
||||
existing = ForumTopic.filter(title=title).first()
|
||||
if not existing:
|
||||
dept_name = random.choice(['生产部', '技术部', '质量部'])
|
||||
author = dept_users[dept_name]
|
||||
ForumTopic.create(
|
||||
title=title,
|
||||
content='这是论坛话题第{}条的内容,用于测试分页效果。欢迎大家参与讨论,分享经验。'.format(10 + i),
|
||||
category=random.choice(['综合', '技术', '生活', '学习']),
|
||||
author_id=author.id if author else 1,
|
||||
author_name=author.username if author else 'admin',
|
||||
status=1,
|
||||
is_pinned=0,
|
||||
)
|
||||
|
||||
# 额外知识文章 30 篇
|
||||
for i in range(1, 31):
|
||||
title = '知识文章 #{}:技术规范与操作指南'.format(10 + i)
|
||||
existing = KnowledgeArticle.filter(title=title).first()
|
||||
if not existing:
|
||||
dept_name = random.choice(['生产部', '技术部', '质量部'])
|
||||
author = dept_users[dept_name]
|
||||
KnowledgeArticle.create(
|
||||
title=title,
|
||||
content='这是知识文章第{}条的内容,用于测试分页效果。包含详细的技术说明和操作步骤。'.format(10 + i),
|
||||
category=random.choice(['综合', '技术', '学习', '生活']),
|
||||
author_id=author.id if author else 1,
|
||||
author_name=author.username if author else 'admin',
|
||||
status=1,
|
||||
is_pinned=0,
|
||||
)
|
||||
|
||||
# 额外公告 25 条
|
||||
for i in range(1, 26):
|
||||
title = '公告 #{}:系统通知'.format(4 + i)
|
||||
existing = Announcement.filter(title=title).first()
|
||||
if not existing:
|
||||
Announcement.create(
|
||||
title=title,
|
||||
content='这是第{}条公告内容,用于测试公告列表的分页效果。'.format(4 + i),
|
||||
status=1,
|
||||
is_pinned=0,
|
||||
created_by=admin.id if admin else 1,
|
||||
)
|
||||
|
||||
# 给置顶话题添加 30 条回复用于测试回复翻页
|
||||
pinned_topics = ForumTopic.filter(is_pinned=1).all()
|
||||
if pinned_topics:
|
||||
topic = pinned_topics[0]
|
||||
existing_replies = ForumReply.filter(topic_id=topic.id).count()
|
||||
if existing_replies < 5:
|
||||
for i in range(1, 31):
|
||||
dept_name = random.choice(['生产部', '技术部', '质量部'])
|
||||
prefix_map = {'生产部': 'shengchan', '技术部': 'jishu', '质量部': 'zhiliang'}
|
||||
prefix = prefix_map[dept_name]
|
||||
username = '{}{}'.format(prefix, random.randint(1, 10))
|
||||
user = User.filter(username=username).first()
|
||||
if user:
|
||||
ForumReply.create(
|
||||
topic_id=topic.id,
|
||||
content='这是第{}条回复,用于测试回复分页效果。大家多多交流!'.format(i),
|
||||
author_id=user.id,
|
||||
author_name=user.username,
|
||||
)
|
||||
# 更新回复计数
|
||||
reply_count = ForumReply.filter(topic_id=topic.id).count()
|
||||
topic.update(reply_count=reply_count)
|
||||
|
||||
print('翻页测试数据生成完成!')
|
||||
|
||||
# 创建默认分类标签
|
||||
categories_data = {
|
||||
'forum': [
|
||||
{'name': '综合', 'sort_order': 0},
|
||||
{'name': '技术', 'sort_order': 1},
|
||||
{'name': '生活', 'sort_order': 2},
|
||||
{'name': '学习', 'sort_order': 3},
|
||||
],
|
||||
'knowledge': [
|
||||
{'name': '综合', 'sort_order': 0},
|
||||
{'name': '技术', 'sort_order': 1},
|
||||
{'name': '学习', 'sort_order': 2},
|
||||
{'name': '生活', 'sort_order': 3},
|
||||
],
|
||||
}
|
||||
for module, tags in categories_data.items():
|
||||
for tag in tags:
|
||||
existing = Category.filter(module=module, name=tag['name']).first()
|
||||
if not existing:
|
||||
Category.create(
|
||||
module=module,
|
||||
name=tag['name'],
|
||||
sort_order=tag['sort_order'],
|
||||
)
|
||||
print(f'创建分类标签: {module}/{tag["name"]}')
|
||||
|
||||
print('分类标签创建完成!')
|
||||
Reference in New Issue
Block a user