cdn change to local and use markdown and style change
This commit is contained in:
@@ -95,39 +95,52 @@ def add_material_chapter(material_id, chapter_name, teacher_id):
|
||||
def rename_material_structure(material_id, is_chapter, chapter_name, lesson_name, new_name):
|
||||
mongo = current_app.extensions["mongo"]
|
||||
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
||||
# 先检查是否存在
|
||||
if not material:
|
||||
return False # 材料不存在
|
||||
|
||||
# 检查名称是否存在于列表中的辅助函数
|
||||
def name_exists_in_list(item_list, name_key, target_name):
|
||||
return any(item[name_key] == target_name for item in item_list)
|
||||
|
||||
# 查找包含指定章节名的章节
|
||||
def find_chapter_by_name(chapters, target_name):
|
||||
for chapter in chapters:
|
||||
if chapter['chapter_name'] == target_name:
|
||||
return chapter
|
||||
return None
|
||||
|
||||
# 检查章节是否存在
|
||||
target_chapter = find_chapter_by_name(material['chapters'], chapter_name)
|
||||
if not target_chapter:
|
||||
return False
|
||||
|
||||
if is_chapter:
|
||||
for chapter in material['chapters']:
|
||||
if chapter['chapter_name'] == chapter_name:
|
||||
break
|
||||
# 检查新章节名是否已存在
|
||||
if name_exists_in_list(material['chapters'], 'chapter_name', new_name):
|
||||
return False
|
||||
for chapter in material['chapters']:
|
||||
if chapter['chapter_name'] == new_name:
|
||||
return False
|
||||
else:
|
||||
for chapter in material['chapters']:
|
||||
if chapter['chapter_name'] == chapter_name:
|
||||
break
|
||||
return False
|
||||
for lesson in chapter['lessons']:
|
||||
if lesson['lesson_name'] == lesson_name:
|
||||
break
|
||||
for lesson in chapter['lessons']:
|
||||
if lesson['lesson_name'] == new_name:
|
||||
return False
|
||||
|
||||
if is_chapter:
|
||||
|
||||
# 更新章节名
|
||||
mongo.db.materials.update_one(
|
||||
{'_id': ObjectId(material_id)},
|
||||
{'$set': {'chapters.$[c].chapter_name': new_name}},
|
||||
array_filters=[{'c.chapter_name': chapter_name}]
|
||||
)
|
||||
else:
|
||||
# 检查课时是否存在
|
||||
if not name_exists_in_list(target_chapter['lessons'], 'lesson_name', lesson_name):
|
||||
return False
|
||||
|
||||
# 检查新课时名是否已存在
|
||||
if name_exists_in_list(target_chapter['lessons'], 'lesson_name', new_name):
|
||||
return False
|
||||
|
||||
# 更新课时名
|
||||
mongo.db.materials.update_one(
|
||||
{'_id': ObjectId(material_id)},
|
||||
{'$set': {'chapters.$[c].lessons.$[l].lesson_name': new_name}},
|
||||
array_filters=[{'c.chapter_name': chapter_name}, {'l.lesson_name': lesson_name}]
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
def delete_material_chapter_lesson(material_id, chapter_name, lesson_name, teacher_id):
|
||||
@@ -178,33 +191,65 @@ def reorder_material_structure(material_id, order):
|
||||
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
||||
if not material:
|
||||
return False
|
||||
|
||||
# 1. 构建原章节映射(保留章节元信息)和原章节的课时映射
|
||||
chapter_map = {ch["chapter_name"]: ch for ch in material["chapters"]}
|
||||
# 每个章节的课时映射:{章节名: {课时名: 课时对象}}
|
||||
original_chapter_lessons = {
|
||||
ch["chapter_name"]: {ls["lesson_name"]: ls for ls in ch["lessons"]}
|
||||
for ch in material["chapters"]
|
||||
}
|
||||
|
||||
# 2. 收集所有章节“少掉的课时”到全局池(仅包含原章节有但新排序没包含的课时)
|
||||
# 用列表存储,避免同名课时被覆盖(处理可能的重名场景)
|
||||
global_extra_lessons = []
|
||||
for ch_name in chapter_map:
|
||||
original_lessons = original_chapter_lessons[ch_name] # 原章节的所有课时
|
||||
# 找到当前章节在order中的配置(如果没有,默认该章节所有课时都是“少掉的”)
|
||||
chapter_order = next((item for item in order if item["chapter_name"] == ch_name), None)
|
||||
if not chapter_order:
|
||||
# 章节未在order中出现,所有原课时都是“少掉的”
|
||||
global_extra_lessons.extend(original_lessons.values())
|
||||
continue
|
||||
# 章节在order中出现,找出原课时中不在新排序里的(即“少掉的”)
|
||||
new_lesson_names = chapter_order["lessons"]
|
||||
for ls_name, ls in original_lessons.items():
|
||||
if ls_name not in new_lesson_names:
|
||||
global_extra_lessons.append(ls)
|
||||
print("global_extra_lessons:", global_extra_lessons)
|
||||
new_chapters = []
|
||||
for chapter_order in order:
|
||||
ch_name = chapter_order['chapter_name']
|
||||
ch_name = chapter_order["chapter_name"]
|
||||
if ch_name not in chapter_map:
|
||||
continue # 如果前端传了不存在的章节名,可以选择跳过或报错
|
||||
continue # 跳过不存在的章节
|
||||
|
||||
original_ch = chapter_map[ch_name]
|
||||
|
||||
# 建立 lesson_name -> lesson dict
|
||||
lesson_map = {ls["lesson_name"]: ls for ls in original_ch["lessons"]}
|
||||
|
||||
# 按顺序重排 lessons
|
||||
original_lessons = original_chapter_lessons[ch_name] # 本章节原课时
|
||||
new_lessons = []
|
||||
for ls_name in chapter_order['lessons']:
|
||||
if ls_name in lesson_map:
|
||||
new_lessons.append(lesson_map[ls_name])
|
||||
|
||||
# 如果还有遗漏(前端没传的),可以追加到最后,避免丢数据
|
||||
for ls_name, ls in lesson_map.items():
|
||||
if ls not in new_lessons:
|
||||
new_lessons.append(ls)
|
||||
# 3. 构建当前章节的新课时列表
|
||||
for ls_name in chapter_order["lessons"]:
|
||||
# 优先使用本章节原有的课时
|
||||
if ls_name in original_lessons:
|
||||
new_lessons.append(original_lessons[ls_name])
|
||||
continue
|
||||
# 本章节没有,从全局池(其他章节少掉的课时)中找
|
||||
# 处理可能的重名:找到第一个匹配的课时
|
||||
for i, extra_ls in enumerate(global_extra_lessons):
|
||||
if extra_ls["lesson_name"] == ls_name:
|
||||
new_lessons.append(extra_ls)
|
||||
del global_extra_lessons[i] # 从全局池移除,避免重复
|
||||
break
|
||||
|
||||
# 构造新的 chapter
|
||||
# 4. 构造新章节(保留原章节元信息,替换课时列表)
|
||||
new_chapter = {**original_ch, "lessons": new_lessons}
|
||||
new_chapters.append(new_chapter)
|
||||
|
||||
# 更新 material
|
||||
# 5. 处理全局池中剩余的课时(未被任何章节引用的,追加到最后一个章节)
|
||||
if global_extra_lessons and new_chapters:
|
||||
new_chapters[-1]["lessons"].extend(global_extra_lessons)
|
||||
print("new_chapters:", new_chapters)
|
||||
# 更新数据库
|
||||
mongo.db.materials.update_one(
|
||||
{"_id": ObjectId(material_id)},
|
||||
{
|
||||
@@ -216,6 +261,7 @@ def reorder_material_structure(material_id, order):
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def save_materials_markdown_cos(
|
||||
material_id: str,
|
||||
chapter_name: str,
|
||||
|
||||
Reference in New Issue
Block a user