添加chapter和lesson
This commit is contained in:
@@ -33,7 +33,7 @@ def login_user(username: str, password: str, *, require_teacher: bool=False):
|
||||
|
||||
# 设置会话 + 关联映射
|
||||
user_uuid = "user_" + str(uuid.uuid4())
|
||||
session["user_id"] = user_uuid
|
||||
session["user_uuid"] = user_uuid
|
||||
|
||||
username2uuid = current_app.extensions["username2uuid"]
|
||||
uuid2username = current_app.extensions["uuid2username"]
|
||||
@@ -43,4 +43,4 @@ def login_user(username: str, password: str, *, require_teacher: bool=False):
|
||||
return True, "登录成功"
|
||||
|
||||
def logout_user():
|
||||
session.pop("user_id", None)
|
||||
session.pop("user_uuid", None)
|
||||
|
||||
10
Html/apps/services/cos_service.py
Normal file
10
Html/apps/services/cos_service.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from flask import current_app
|
||||
|
||||
def upload_file(rb_file, file_name):
|
||||
cos_client = current_app.extensions["cos_client"]
|
||||
cos_client.put_object(
|
||||
Bucket=current_app.config["TENCENT_COS"]["bucket"],
|
||||
Body=rb_file,
|
||||
Key=file_name,
|
||||
)
|
||||
return f"https://{current_app.config['TENCENT_COS']['bucket']}.cos.{current_app.config['TENCENT_COS']['region']}.myqcloud.com/{file_name}"
|
||||
@@ -1,5 +1,10 @@
|
||||
# myapp/services/course_service.py
|
||||
from typing import Dict, List
|
||||
from flask import current_app
|
||||
from datetime import datetime
|
||||
from bson import ObjectId
|
||||
|
||||
from ..models.material import Material
|
||||
|
||||
# 假设你已有这两个 loader
|
||||
from db.course import load_course_from_json
|
||||
@@ -18,3 +23,82 @@ def user_selected_course_briefs(user_obj):
|
||||
brief = course_list.get_course_brief_info(cid, course_obj)
|
||||
briefs.append(brief)
|
||||
return briefs
|
||||
|
||||
def create_material(teacher_id, material_name, description, chapters, image_url):
|
||||
material = Material(
|
||||
name=material_name,
|
||||
description=description,
|
||||
teacher_id=teacher_id,
|
||||
created_at=datetime.now(),
|
||||
updated_at=datetime.now(),
|
||||
chapters=chapters,
|
||||
image_url=image_url
|
||||
)
|
||||
mongo = current_app.extensions["mongo"]
|
||||
material_id = mongo.db.materials.insert_one(material.model_dump()).inserted_id
|
||||
return str(material_id)
|
||||
|
||||
def load_material(material_id):
|
||||
mongo = current_app.extensions["mongo"]
|
||||
material = mongo.db.materials.find_one({"_id": ObjectId(material_id)})
|
||||
return Material(**material)
|
||||
|
||||
def update_material(material_id, chapters):
|
||||
mongo = current_app.extensions["mongo"]
|
||||
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
||||
if material:
|
||||
mongo.db.materials.update_one(
|
||||
{'_id': ObjectId(material_id)},
|
||||
{'$set': {'chapters': chapters, 'updated_at': datetime.now()}}
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
def add_material_chapter(material_id, chapter_name, teacher_id):
|
||||
mongo = current_app.extensions["mongo"]
|
||||
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
||||
if material['teacher_id'] != teacher_id:
|
||||
return False
|
||||
if material:
|
||||
mongo.db.materials.update_one(
|
||||
{'_id': ObjectId(material_id)},
|
||||
{'$push': {'chapters': {'chapter_name': chapter_name, 'lessons': []}}})
|
||||
return True
|
||||
|
||||
def add_material_chapter_lesson(material_id, chapter_name, lesson_name, teacher_id):
|
||||
mongo = current_app.extensions["mongo"]
|
||||
|
||||
lesson_doc = {
|
||||
"lesson_name": lesson_name or "",
|
||||
"markdown_lesson_file_link": "",
|
||||
"markdown_prompt_file_name": "",
|
||||
"markdown_score_prompt_file_link": "",
|
||||
}
|
||||
|
||||
res = mongo.db.materials.update_one(
|
||||
{
|
||||
'_id': ObjectId(material_id),
|
||||
'teacher_id': teacher_id,
|
||||
},
|
||||
{
|
||||
'$push': {'chapters.$[c].lessons': lesson_doc},
|
||||
'$set': {'updated_at': datetime.now()},
|
||||
},
|
||||
array_filters=[{'c.chapter_name': chapter_name}] # 命名占位符 c 指向命中的 chapter
|
||||
)
|
||||
return res.modified_count == 1
|
||||
|
||||
|
||||
def get_materials_by_teacher(teacher_id)->List[Material]:
|
||||
mongo = current_app.extensions["mongo"]
|
||||
materials = mongo.db.materials.find({'teacher_id': teacher_id})
|
||||
return [Material(**material) for material in materials]
|
||||
|
||||
def get_materials_by_teacher_dict(teacher_id)->List[Dict]:
|
||||
mongo = current_app.extensions["mongo"]
|
||||
materials = mongo.db.materials.find({'teacher_id': teacher_id})
|
||||
result = []
|
||||
for material in materials:
|
||||
material['_id'] = str(material['_id'])
|
||||
result.append(material)
|
||||
return result
|
||||
Reference in New Issue
Block a user