OK much
This commit is contained in:
@@ -1,105 +1,62 @@
|
||||
import os
|
||||
from tinydb import TinyDB, Query, where
|
||||
|
||||
import json
|
||||
import copy
|
||||
import re
|
||||
class CourseList:
|
||||
def __init__(self, db_file='db/data/course/course_list.json'):
|
||||
"""初始化课程列表,自动打开并加载 JSON 数据文件。"""
|
||||
self.db_file = db_file
|
||||
self.db = TinyDB(self.db_file)
|
||||
self.course_query = Query()
|
||||
with open(self.db_file, 'r') as f:
|
||||
self.db = json.load(f)
|
||||
|
||||
def add_course(self, course_data):
|
||||
"""添加新的课程到课程列表。"""
|
||||
if not self.db.search(self.course_query.course_id == course_data['course_id']):
|
||||
self.db.insert(course_data)
|
||||
else:
|
||||
print(f"Course with ID {course_data['course_id']} already exists.")
|
||||
def add_course(self, course_id, course_name, course_create_date, course_update_data, course_img_path):
|
||||
"""添加新的用户登录信息"""
|
||||
# 检查是否已经有该用户
|
||||
assert course_id not in self.db, f"Course {course_name} with ID {course_id} already exists."
|
||||
self.db[course_id] = {}
|
||||
self.db[course_id]['course_name'] = course_name
|
||||
self.db[course_id]['course_create_date'] = course_create_date
|
||||
self.db[course_id]['course_update_data'] = course_update_data
|
||||
self.db[course_id]['course_img_path'] = course_img_path
|
||||
|
||||
def get_course(self, course_id):
|
||||
"""获取指定 course_id 的课程信息。"""
|
||||
result = self.db.search(self.course_query.course_id == course_id)
|
||||
return result[0] if result else None
|
||||
with open(self.db_file, 'w') as f:
|
||||
json.dump(self.db, f, indent=4)
|
||||
|
||||
def update_course(self, course_id, updated_data):
|
||||
"""更新指定 course_id 的课程信息。"""
|
||||
self.db.update(updated_data, self.course_query.course_id == course_id)
|
||||
|
||||
def get_course_brief_info(self, course_id, course) -> dict:
|
||||
assert course_id in self.db, f"Course ID {course_id} does not exist."
|
||||
result = copy.deepcopy(self.db[course_id])
|
||||
result["lessons"]=[]
|
||||
for lesson in course.lessons:
|
||||
d={}
|
||||
d['lesson_id'] = lesson['lesson_id']
|
||||
d["lesson_name"]=lesson['lesson_name']
|
||||
print(lesson["markdown"])
|
||||
h3_titles = re.findall(r'[\s]###\s+(.*)', lesson["markdown"])
|
||||
print(h3_titles)
|
||||
d["subChapters"]=h3_titles
|
||||
result["lessons"].append(d)
|
||||
return result
|
||||
|
||||
def update_course_brief_info(self, course_id, course_name, course_create_date, course_update_data, course_img_path):
|
||||
assert course_id in self.db, f"Course ID {course_id} does not exist."
|
||||
|
||||
self.db[course_id]['course_name'] = course_name
|
||||
self.db[course_id]['course_create_date'] = course_create_date
|
||||
self.db[course_id]['course_update_data'] = course_update_data
|
||||
self.db[course_id]['course_img_path'] = course_img_path
|
||||
|
||||
with open(self.db_file, 'w') as f:
|
||||
json.dump(self.db, f, indent=4)
|
||||
|
||||
def delete_course(self, course_id):
|
||||
"""删除指定 course_id 的课程。"""
|
||||
self.db.remove(self.course_query.course_id == course_id)
|
||||
|
||||
def add_lesson_to_course(self, course_id, lesson_data):
|
||||
"""为指定课程添加课时。"""
|
||||
course = self.get_course(course_id)
|
||||
if course:
|
||||
if 'lessons' not in course:
|
||||
course['lessons'] = []
|
||||
course['lessons'].append(lesson_data)
|
||||
self.update_course(course_id, {'lessons': course['lessons']})
|
||||
else:
|
||||
print(f"Course with ID {course_id} not found.")
|
||||
|
||||
def get_lessons_of_course(self, course_id):
|
||||
"""获取指定课程的所有课时信息。"""
|
||||
course = self.get_course(course_id)
|
||||
return course['lessons'] if course else None
|
||||
|
||||
assert course_id in self.db, f"Course ID {course_id} does not exist."
|
||||
self.db.remove(course_id)
|
||||
|
||||
def get_all_courses(self):
|
||||
"""获取所有课程。"""
|
||||
return self.db.all()
|
||||
return list(self.db)
|
||||
|
||||
|
||||
# # 课程数据结构示例
|
||||
# course_data = {
|
||||
# 'course_id': 'CS101',
|
||||
# 'course_name': 'Computer Science 101',
|
||||
# 'course_img_path': 'images/cs101.png',
|
||||
# 'lessons': [
|
||||
# {
|
||||
# 'lesson_id': 'L001',
|
||||
# 'lesson_name': 'Introduction to Computer Science',
|
||||
# 'markdown': 'This is the introduction lesson.',
|
||||
# 'markdown_prompt': 'What is Computer Science?',
|
||||
# 'score_prompt': 'Please provide an overview of CS.'
|
||||
# },
|
||||
# {
|
||||
# 'lesson_id': 'L002',
|
||||
# 'lesson_name': 'Data Structures',
|
||||
# 'markdown': 'In this lesson, we cover Data Structures.',
|
||||
# 'markdown_prompt': 'What is an array?',
|
||||
# 'score_prompt': 'Explain how linked lists work.'
|
||||
# }
|
||||
# ]
|
||||
# }
|
||||
|
||||
# lesson_data = {
|
||||
# 'lesson_id': 'L003',
|
||||
# 'lesson_name': 'Algorithms',
|
||||
# 'markdown': 'In this lesson, we will discuss algorithms.',
|
||||
# 'markdown_prompt': 'What is a sorting algorithm?',
|
||||
# 'score_prompt': 'Explain the difference between merge sort and quicksort.'
|
||||
# }
|
||||
|
||||
# # 创建 CourseList 实例
|
||||
# course_list = CourseList()
|
||||
|
||||
# # 添加课程
|
||||
# course_list.add_course(course_data)
|
||||
|
||||
# # 添加课时到现有课程
|
||||
# course_list.add_lesson_to_course('CS101', lesson_data)
|
||||
|
||||
# # 获取并显示所有课程
|
||||
# courses = course_list.get_all_courses()
|
||||
# print(courses)
|
||||
|
||||
# # 获取某个课程的课时
|
||||
# lessons = course_list.get_lessons_of_course('CS101')
|
||||
# print(lessons)
|
||||
|
||||
# # 更新课程信息
|
||||
# updated_data = {'course_name': 'Computer Science 101 - Updated'}
|
||||
# course_list.update_course('CS101', updated_data)
|
||||
|
||||
# # 删除课程
|
||||
# course_list.delete_course('CS101')
|
||||
def to_json(self):
|
||||
return json.dumps(self.db)
|
||||
|
||||
def items(self):
|
||||
assert type(self.db) is dict, "db is not a dict"
|
||||
return self.db.items()
|
||||
Reference in New Issue
Block a user