def post(self): data = request.get_json() or request.form practiceId = int(data["practiceId"]) position = data["index"] content = data["content"] choices = data["choices"] answer = data["answer"] problemId = get_id() db = Mysql() # Handle choice list -> json str choices = json.dumps(choices) db.insertOne( """ insert into Problem (`problem_id`,`position`,`content`,`choices`,`answer`,`practice_id`) values (%s,%s,%s,%s,%s,%s);; """, (problemId,position,content,choices,answer,practiceId) ) # Get Students list and reset the status of practice sl = db.getAll( """ select student_id from StudentPractice where practice_id=%s; """, (practiceId,) ) if sl: # Has student for student in sl: id = student["student_id"] db.modify( """ update StudentPractice set status=0 where practice_id=%s and student_id=%s; """, (practiceId, id) ) p = db.getAll( """ select * from Problem where practice_id=%s; """, (practiceId,) ) pl = [] for each in p: ch = json.loads(each["choices"]) d = { "id": str(each["problem_id"]), "problem": each["content"], "choices": [{"cid": i, "value": ch[i]} for i in range(0, len(ch))], "correctAnswer": each["answer"] } pl.append(d) return { "success": True, "problemList": pl }
def delete(self): problemId = int(request.args.get("problemId")) db = Mysql() # Get practice practiceId = db.getOne( """ select practice_id from Problem where problem_id=%s; """, (problemId,) )["practice_id"] # Delete Problem db.delete( """ delete from Problem where problem_id=%s; """, (problemId,) ) # Reset Status of Practice sl = db.getAll( """ select student_id from StudentPractice where practice_id=%s; """, (practiceId,) ) if sl: for student in sl: id = student["student_id"] db.modify( """ update StudentPractice set status=0 where student_id=%s and practice_id=%s; """, (id,practiceId) ) p = db.getAll( """ select * from Problem where practice_id=%s; """, (practiceId,) ) pl = [] if p: for each in p: ch = json.loads(each["choices"]) d = { "id": str(each["problem_id"]), "problem": each["content"], "choices": [{"cid": i, "value": ch[i]} for i in range(0, len(ch))], "correctAnswer": each["answer"] } pl.append(d) return { "success": True, "problemList": pl }
def put(self): data = request.get_json() or request.form practiceId = int(data["practiceId"]) problemId = int(data["problemId"]) position = data["index"] content = data["content"] choices = data["choices"] answer = data["answer"] db = Mysql() # Edit Problem choices = json.dumps(choices) db.modify( """ update Problem set position=%s,content=%s,choices=%s,answer=%s where problem_id=%s; """, (position,content,choices,answer,problemId) ) # Reset Status of Practice sl = db.getAll( """ select student_id from StudentPractice where practice_id=%s; """, (practiceId,) ) if sl: for student in sl: id = student["student_id"] db.modify( """ update StudentPractice set status=0 where student_id=%s and practice_id=%s; """, (id,practiceId) ) p = db.getAll( """ select * from Problem where practice_id=%s; """, (practiceId,) ) pl = [] for each in p: ch = json.loads(each["choices"]) d = { "id": str(each["problem_id"]), "problem": each["content"], "choices": [{"cid": i, "value": ch[i]} for i in range(0, len(ch))], "correctAnswer": each["answer"] } pl.append(d) return { "success": True, "problemList": pl }
def post(self): data = request.get_json() or request.form courseId = int(data["courseId"]) uid = int(data["uid"]) db = Mysql() # Check if the student is exists d = db.getOne( """ select `user_id`,`character` from User where school_id=%s; """, (uid, )) if d and d["character"] == 0: uid = d["user_id"] else: return {"success": False, "reason": "此学生不存在,请检查学号是否输入正确!"} # Initialize course learning process # Get Section List first t = db.getAll( """ select topic_id from Topic where course_id=%s; """, (courseId, )) sections = {} for topic in t: tid = topic["topic_id"] # Get Section List sl = db.getAll( """ select section_id from Section where topic_id=%s; """, (tid, )) for section in sl: sections[str(section["section_id"])] = 0 # Write Data db.insertOne( """ insert into StudentCourse (`student_id`,`course_id`,`status`,`process_detail`) values (%s,%s,0,%s); """, (uid, courseId, str(sections))) db.modify( """ update User set course_count=course_count+1 where user_id=%s; """, (uid, )) db.modify( """ update Course set student_count=student_count+1 where course_id=%s; """, (courseId, )) return {"success": True}
def get(self): courseId = int(request.args.get("courseId")) db = Mysql() # Get Student List d = db.getAll( """ select student_id from StudentCourse where course_id=%s; """, (courseId,) ) students = [] i = 0 if not d: return students for each in d: studentId = db.getOne( """ select school_id from User where user_id=%s; """, (each["student_id"],) )["school_id"] students.append({"number": i,"uid":str(studentId)}) i += 1 return students
def post(self): data = request.get_json() or request.form uid = int(data["uid"]) practiceId = int(data["practiceId"]) p = data["problems"] db = Mysql() # Get correct answer list d = db.getAll( """ select problem_id as id, answer, choices from Problem where practice_id=%s; """, (practiceId, )) cl = {} for problem in d: problem["choices"] = json.loads(problem["choices"]) # value not index cl[problem["id"]] = problem["choices"].index(problem["answer"]) # Handle format of post data & save draft into a dict draft = {} for problem in p: pid = int(problem["id"]) problem["status"] = 2 problem["correctAnswer"] = cl[pid] draft[pid] = problem["draft"] db.modify( """ update StudentPractice set status=2,draft=%s where student_id=%s and practice_id=%s; """, (str(draft), uid, practiceId)) return p
def get(self): topicId = int(request.args.get("topicId")) db = Mysql() sl = db.getAll( """ select * from Section where topic_id=%s; """, (topicId,) ) if not sl: return { "success": True, "sections": [] } sections = [] for section in sl: data = { "value": str(section["section_id"]), "title": section["title"] } sections.append(data) return { "success": True, "sections": sections }
def get(self): courseId = int(request.args.get("courseId")) db = Mysql() tl = db.getAll( """ select * from Topic where course_id=%s; """, (courseId,) ) if not tl: return { "success": True, "topics": [] } topics = [] for topic in tl: data = { "value": str(topic["topic_id"]), "title": topic["title"] } topics.append(data) return { "success": True, "topics": topics }
def get(self): teacherId = int(request.args.get("uid")) db = Mysql() cl = db.getAll( """ select * from Course where teacher_id=%s; """, (teacherId,) ) if not cl: return { "success": True, "courses": [] } courses = [] for course in cl: data = { "value": str(course["course_id"]), "title": course["title"] } courses.append(data) return { "success": True, "courses": courses }
def delete(self): sectionId = int(request.args.get('sectionId')) db = Mysql() # Get Topic and modify section_count topicId = db.getOne( """ select topic_id from Section where section_id=%s; """, (sectionId,) )["topic_id"] db.modify( """ update Topic set section_count=section_count-1 where topic_id=%s; """, (topicId,) ) # Get Course courseId = db.getOne( """ select course_id from Topic where topic_id=%s; """, (topicId,) )["course_id"] db.modify( """ update Course set section_count=section_count-1 where course_id=%s; """, (courseId,) ) # Get All Students s = db.getAll( """ select `student_id`,`process_detail` from StudentCourse where course_id=%s; """, (courseId,) ) if s: for student in s: pd = eval(student["process_detail"]) pd.pop(str(sectionId)) pd = str(pd) db.modify( """ update StudentCourse set process_detail=%s where student_id=%s; """, (pd,student["student_id"]) ) # Delete Section db.delete( """ delete from Section where section_id=%s; """, (sectionId,) ) return { "success": True }
def get(self): uid = int(request.args.get("uid")) db = Mysql() p = db.getAll( """ select * from Practice where teacher_id=%s; """, (uid, )) pl = [] for each in p: # format relation string r = each["relation"] if r == 0: relation = db.getOne( """ select title from Course where course_id=%s; """, (each["course_id"], ))["title"] elif r == 1: courseTitle = db.getOne( """ select title from Course where course_id=%s; """, (each["course_id"], ))["title"] topicTitle = db.getOne( """ select title from Topic where topic_id=%s; """, (each["topic_id"], ))["title"] relation = "{} / {}".format(courseTitle, topicTitle) elif r == 2: courseTitle = db.getOne( """ select title from Course where course_id=%s; """, (each["course_id"], ))["title"] topicTitle = db.getOne( """ select title from Topic where topic_id=%s; """, (each["topic_id"], ))["title"] sectionTitle = db.getOne( """ select title from Section where section_id=%s; """, (each["section_id"], ))["title"] relation = "{} / {} / {}".format(courseTitle, topicTitle, sectionTitle) else: relation = "" d = { "title": each["title"], "relation": relation, "id": str(each["practice_id"]) } pl.append(d) return {"success": True, "practiceList": pl}
def get(self): courseId = int(request.args.get("courseId")) db = Mysql() # Get Course Info d = db.getOne( """ select `title`,`description` from Course where course_id=%s; """, (courseId,) ) t = db.getAll( """ select topic_id as id, title as topicTitle from Topic where course_id=%s; """, (courseId,) ) if t: for topic in t: sl = db.getAll( """ select section_id as id, title as sectionTitle, entity_type as entityType from Section where topic_id=%s; """, (topic["id"],) ) if sl: for section in sl: section["id"] = str(section["id"]) else: sl = [] topic["sections"] = sl topic["id"] = str(topic["id"]) else : t = [] respData = { "id": str(courseId), "title": d["title"], "desc": d["description"], "courseDetail": t } return respData
def get(self): practiceId = request.args.get('practiceId') uid = request.args.get("uid") db = Mysql() draft = db.getOne( """ select * from StudentPractice where student_id=%s and practice_id=%s; """, (uid, practiceId) ) problemSet = db.getAll( """ select problem_id as id, position, content, choices from Problem where practice_id=%s; """, (practiceId,) ) status = draft["status"] # problemSet["status"] = status if status == 0: for problem in problemSet: problem["status"] = 0 problem["choices"] = json.loads(problem["choices"]) problem["id"] = str(problem["id"]) elif status == 1: d = eval(draft["draft"]) for problem in problemSet: id = problem["id"] problem["status"] = 1 problem["choices"] = json.loads(problem["choices"]) problem["draft"] = d[id] problem["id"] = str(problem["id"]) elif status == 2: d = eval(draft["draft"]) for problem in problemSet: id = problem["id"] problem["status"] = 2 problem["choices"] = json.loads(problem["choices"]) problem["draft"] = d[id] answer = db.getOne( """ select answer from Problem where problem_id=%s; """, (problem["id"],) )["answer"] problem["correctAnswer"] = problem["choices"].index(answer) problem["id"] = str(problem["id"]) respData = { "problemSet":problemSet, "status": status } return respData
def get(self): userId = int(request.args.get("userId")) courseId = int(request.args.get("courseId")) db = Mysql() res = db.getOne( """ select process_detail from StudentCourse where course_id=%s and student_id=%s; """, (courseId, userId) )["process_detail"] sl = db.getAll( """ select * from Section where topic_id in (select topic_id from Topic where course_id=%s); """, (courseId,) ) d = [] if sl: res = eval(res) for section in sl: sectionId = section["section_id"] status = res[str(sectionId)] d.append({ "id": str(sectionId), "title": section["title"], "entity": { "type": section["entity_type"] }, "status": status }) # for key, values in res.items(): # sectionId = int(key) # status = values # # section = db.getOne( # """ # select * from Section where section_id=%s; # """, (sectionId) # ) # d.append({ # "id": key, # "title": section["title"], # "entity": { # "type": section["entity_type"], # }, # "status": status # }) return d
def get(self): # data = request.args.get("userId") userId = request.args.get("userId") # character = request.args.get("character") db = Mysql() # Query Statements res = db.getOne( """ select course_count from User where user_id=%s; """, (userId,) )['course_count'] if res: if res <= 5: page = 0 courseRes = db.getAll( """ select * from Course where teacher_id=%s; """, (userId,) ) courseCount = res else: page = res/5+1 if res%5 else res/5 courseRes = db.getSome( """ select * from Course where teacher_id=%s; """, 5, (userId,) ) courseCount = 5 courseDetail = [] for i in range(0, courseCount): courseDetail.append({ "id": str(courseRes[i]["course_id"]), "title": courseRes[i]['title'], "count": [courseRes[i]['topic_count'], courseRes[i]['section_count'], courseRes[i]['practice_count'], courseRes[i]['student_count']] }) respData = { "state": "success", "count": courseCount, "page": page, "courses": courseDetail } db.close() return respData
def get(self): practiceId = int(request.args.get("practiceId")) db = Mysql() p = db.getOne( """ select * from Practice where practice_id=%s; """, (practiceId, )) problems = db.getAll( """ select * from Problem where practice_id=%s; """, (practiceId, )) pl = [] if problems: # Has Problems for each in problems: choices = json.loads(each["choices"]) d = { "id": str(each["problem_id"]), "problem": each["content"], "choices": [{ "cid": i, "value": choices[i] } for i in range(0, len(choices))], "correctAnswer": each["answer"] } pl.append(d) else: # No Problems pl = [] return {"success": True, "title": p["title"], "problemList": pl}
def put(self): data = request.get_json() or request.form practiceId = int(data["practiceId"]) title = data["title"] relatedCourse = data["relatedCourse"] relatedTopic = data["relatedTopic"] relatedSection = data["relatedSection"] db = Mysql() rl = db.getOne( """ select relation,course_id from Practice where practice_id=%s; """, (practiceId,) ) db.modify( """ update Practice set title=%s where practice_id=%s; """, (title,practiceId) ) # Relation modify here if relatedCourse == "": # no change pass else: if relatedSection == "" or relatedSection == "0": if relatedTopic == "" or relatedTopic == "0": # relation 0 courseId = int(relatedCourse) db.modify( """ update Practice set relation=0,course_id=%s where practice_id=%s; """, (courseId, practiceId) ) else: # relation 1 courseId = int(relatedCourse) topicId = int(relatedTopic) db.modify( """ update Practice set relation=1,course_id=%s,topic_id=%s where practice_id=%s; """, (courseId, topicId, practiceId) ) else: # relation 2 courseId = int(relatedCourse) topicId = int(relatedTopic) sectionId = int(relatedSection) db.modify( """ update Practice set relation=2,course_id=%s,topic_id=%s,section_id=%s where practice_id=%s; """, (courseId, topicId, sectionId, practiceId) ) # Handle students # 删除以前的所有记录 db.delete( """ delete from StudentPractice where practice_id=%s; """, (practiceId,) ) if rl["relation"] != 3: courseBefore = rl["course_id"] # 以前的课程 -1 db.modify( """ update Course set practice_count=practice_count-1 where course_id=%s; """, (courseBefore,) ) # 新课程 +1 db.modify( """ update Course set practice_count=practice_count+1 where course_id=%s; """, (courseId,) ) # 新增新的课程学生的记录 sl = db.getAll( """ select student_id from StudentCourse where course_id=%s; """, (courseId,) ) if sl: for student in sl: db.insertOne( """ insert into StudentPractice (`student_id`,`practice_id`,`status`) values (%s,%s,0); """, (student["student_id"],practiceId) ) return { "success": True }
def get(self): uid = request.args.get("uid") # practiceCount = db.getOne( # """ # select practice_count from User where user_id=%s; # """, (userId,) # )['practice_count'] # # if practiceCount < 5: # page = 0 # res = db.getAll( # """ # select * from StudentPractice where student_id=%s; # """, (userId,) # ) # else: # page = practiceCount/5+1 if practiceCount%5 else practiceCount/5 # res = db.getSome( # """ # select * from StudentPractice where student_id=%s; # """, 5, (userId,) # ) db = Mysql() # Get practice list practices = db.getAll( """ select * from StudentPractice where student_id=%s; """, (uid, )) for practice in practices: practiceId = practice["practice_id"] practiceInfo = db.getOne( """ select * from Practice where practice_id=%s; """, (practiceId, )) relation = practiceInfo["relation"] if relation == 0: # related with course courseTitle = db.getOne( """ select title from Course where course_id=%s; """, (practiceInfo["course_id"], ))["title"] practiceInfo["relation"] = courseTitle practiceInfo["course_id"] = str(practiceInfo["course_id"]) elif relation == 1: # related with topic courseTitle = db.getOne( """ select title from Course where course_id=%s; """, (practiceInfo["course_id"], ))["title"] topicTitle = db.getOne( """ select title from Topic where topic_id=%s; """, (practiceInfo["topic_id"], ))["title"] practiceInfo["relation"] = "{} / {}".format( courseTitle, topicTitle) practiceInfo["course_id"] = str(practiceInfo["course_id"]) practiceInfo["topic_id"] = str(practiceInfo["topic_id"]) elif relation == 2: # related with section courseTitle = db.getOne( """ select title from Course where course_id=%s; """, (practiceInfo["course_id"], ))["title"] topicTitle = db.getOne( """ select title from Topic where topic_id=%s; """, (practiceInfo["topic_id"], ))["title"] sectionTitle = db.getOne( """ select title from Section where section_id=%s; """, (practiceInfo["section_id"], ))["title"] practiceInfo["relation"] = "{} / {} / {}".format( courseTitle, topicTitle, sectionTitle) practiceInfo["course_id"] = str(practiceInfo["course_id"]) practiceInfo["topic_id"] = str(practiceInfo["topic_id"]) practiceInfo["section_id"] = str(practiceInfo["section_id"]) practiceInfo["practice_id"] = str(practiceInfo["practice_id"]) practiceInfo["teacher_id"] = str(practiceInfo["teacher_id"]) practice["detail"] = practiceInfo practice["practice_id"] = str(practice["practice_id"]) practice["student_id"] = str(practice["student_id"]) respData = practices return respData
def delete(self): topicId = int(request.args.get("topicId")) courseId = int(request.args.get("courseId")) db = Mysql() # Delete Topic itself db.delete( """ delete from Topic where topic_id=%s; """,(topicId,) ) db.modify( """ update Practice set relation=3 where topic_id=%s and relation=1; """, (topicId,) ) # Get Section list and delete sectionIds = db.getAll( """ select section_id as id from Section where topic_id=%s; """,(topicId,) ) if sectionIds: cnt = len(sectionIds) # Delete all sections related with the topic db.delete( """ delete from Section where topic_id=%s; """,(topicId,) ) # Delete the entities related with sections for section in sectionIds: id = section["id"] db.delete( """ delete from Entity where section_id=%s; """,(id,) ) # Untie the relationship of practice for section in sectionIds: id = section["id"] db.modify( """ update Practice set relation=3 where section_id=%s and relation=2; """, (id,) ) # Delete the learning process related with deleted sections pd = db.getAll( """ select `student_id`,`process_detail` where course_id=%s; """, (courseId,) ) if pd: for each in pd: process = eval(each["process_detail"]) for section in sectionIds: process.pop(str(section["id"])) db.modify( """ update StudentCourse set process=detail=%s where student_id=%s and course_id=%s; """, (str(process),each["student_id"],courseId) ) else : cnt = 0 db.modify( """ update Course set topic_count=topic_count-1, section_count=section_count-%s where course_id=%s; """, (cnt, courseId) ) db.close() return { "state": "success" }
def post(self): data = request.get_json() or request.form courseId = int(data['courseId']) topicId = int(data['topicId']) title = data['title'] type = data['entity']['entityType'] db = Mysql() sectionId = get_id() if type == 'doc': db.insertOne( """ insert into Section (`section_id`, `title`, `topic_id`, `entity_type`) values(%s,%s,%s,%s); """,(sectionId,title,topicId,1) ) entityId = get_id() db.modify( """ update Section set entity_id=%s where section_id=%s; """,(entityId, sectionId) ) db.insertOne( """ insert into Entity (`entity_id`, `entity_type`, `content`, `section_id`, `url`) values (%s,%s,%s,%s,%s); """, (entityId, 1, data['entity']['content'], sectionId, data['entity']["url"]) ) elif type == 'video': db.insertOne( """ insert into Section (`section_id`,`title`,`topic_id`,`entity_type`) values(%s,%s,%s,%s); """, (sectionId,title,topicId,0) ) entityId = get_id() db.modify( """ update Section set entity_id=%s where section_id=%s; """, (entityId, sectionId) ) db.insertOne( """ insert into Entity (`entity_id`, `entity_type`, `url`, `section_id`) values (%s,%s,%s,%s); """, (entityId, 0, data['entity']['url'], sectionId) ) db.modify( """ update Topic set section_count=section_count+1 where topic_id=%s; """, (topicId,) ) db.modify( """ update Course set section_count=section_count+1 where course_id=%s; """, (courseId,) ) # Modify process_detail pd = db.getAll( """ select `student_id`,`process_detail` from StudentCourse where course_id=%s; """, (courseId,) ) if pd: for each in pd: process = eval(each["process_detail"]) process[str(sectionId)] = 0 db.modify( """ update StudentCourse set process_detail=%s where course_id=%s and student_id=%s; """,(str(process),courseId,each["student_id"]) ) return { "state": "success" }
def get(self): courseId = int(request.args.get("courseId")) studentId = int(request.args.get("studentId")) db = Mysql() res = db.getOne( """ select * from Course where course_id=%s; """, (courseId,) ) d = db.getOne( """ select process_detail from StudentCourse where course_id=%s and student_id=%s; """, (courseId, studentId) ) t = db.getOne( """ select name from User where user_id=%s; """, (res["teacher_id"]) ) processDetail = eval(d["process_detail"]) resp = {} if res: resp = { "title": res["title"], "desc": res["description"], "teacher": t["name"] } if res["topic_count"] != 0: topics = db.getAll( """ select * from Topic where course_id=%s; """,(courseId,) ) if topics: for topic in topics: sections = [] s = db.getAll( """ select * from Section where topic_id=%s; """,(topic["topic_id"],) ) print (s) if s: for section in s: section["section_id"] = str(section["section_id"]) section["topic_id"] = str(section["topic_id"]) section["process"] = processDetail[section["section_id"]] sections = s else: sections = [] topic["sections"] = sections topic["course_id"] = str(topic["course_id"]) topic["topic_id"] = str(topic["topic_id"]) else: topics = [] resp["topics"] = topics resp["id"] = str(courseId) db.close() return resp
def post(self): data = request.get_json() or request.form userId = int(data["userId"]) title = data["title"] relatedCourse = data["relatedCourse"] relatedTopic = data["relatedTopic"] relatedSection = data["relatedSection"] db = Mysql() practiceId = get_id() db.insertOne( """ insert into Practice (`practice_id`,`title`,`teacher_id`) values (%s,%s,%s); """,(practiceId,title,userId) ) db.modify( """ update User set practice_count=practice_count+1 where user_id=%s; """,(userId,) ) if relatedCourse == "" or relatedCourse == "0": # relation = 3 db.modify( """ update Practice set relation=3 where practice_id=%s; """, (practiceId,) ) else: if relatedSection == "" or relatedSection == "0": if relatedTopic == "" or relatedTopic == "0": # relation = 0 courseId = int(relatedCourse) db.modify( """ update Practice set relation=0,course_id=%s where practice_id=%s; """, (courseId,practiceId) ) else: # relation = 1 courseId = int(relatedCourse) topicId = int(relatedTopic) db.modify( """ update Practice set relation=1,course_id=%s,topic_id=%s where practice_id=%s; """, (courseId,topicId,practiceId) ) else: # relation = 2 courseId = int(relatedCourse) topicId = int(relatedTopic) sectionId = int(relatedSection) db.modify( """ update Practice set relation=2,course_id=%s,topic_id=%s,section_id=%s where practice_id=%s; """, (courseId,topicId,sectionId,practiceId) ) # 给课程增加上这个练习 +1 db.modify( """ update Course set practice_count=practice_count+1 where course_id=%s; """, (courseId,) ) # 让所有课程的学生添加上这个练习 sl = db.getAll( """ select student_id from StudentCourse where course_id=%s; """, (courseId,) ) if sl: for student in sl: db.insertOne( """ insert into StudentPractice (`student_id`,`practice_id`,`status`) values (%s,%s,0); """, (student["student_id"],practiceId) ) return { "state": "success", "id": str(practiceId) }
def get(self): practiceId = int(request.args.get("practiceId")) db = Mysql() p = db.getOne( """ select * from Practice where practice_id=%s; """, (practiceId,) ) relation = p["relation"] if relation == 0: # course coursetitle = db.getOne( """ select title from Course where course_id=%s; """, (p["course_id"],) )["title"] relation = "{}".format(coursetitle) elif relation == 1: # course, topic coursetitle = db.getOne( """ select title from Course where course_id=%s; """, (p["course_id"],) )["title"] topictitle = db.getOne( """ select title from Topic where topic_id=%s; """, (p["topic_id"],) )["title"] relation = "{} / {}".format(coursetitle, topictitle) elif relation == 2: # course, topic, section coursetitle = db.getOne( """ select title from Course where course_id=%s; """, (p["course_id"],) )["title"] topictitle = db.getOne( """ select title from Topic where topic_id=%s; """, (p["topic_id"],) )["title"] sectiontitle = db.getOne( """ select title from Section where section_id=%s; """, (p["section_id"],) )["title"] relation = "{} / {} / {}".format(coursetitle, topictitle, sectiontitle) elif relation == 3: # no relation relation = "暂未关联任何课程" problems = db.getAll( """ select * from Problem where practice_id=%s; """, (practiceId,) ) pl = [] if problems: # Has Problems for each in problems: choices = json.loads(each["choices"]) d = { "id": str(each["problem_id"]), "problem": each["content"], "choices": [{"cid": i, "value": choices[i]} for i in range(0, len(choices))], "correctAnswer": each["answer"] } pl.append(d) else: # No Problems pl = [] return { "success": True, "title": p["title"], "problemList": pl, "relation": relation }
def get(self): print ("Get the Request!") userId = request.args.get("userId") # character = request.args.get("character") db = Mysql() # Query Statements res = db.getOne( """ select course_count from User where user_id=%s; """, (userId,) )['course_count'] if res: if res <= 5: page = 0 # courseRes = db.getAll( # """ # select * from Course where course_id in ( # select course_id from StudentCourse where student_id=%s # ); # """, # (userId,) # ) # User JOIN instead of Sub-query --> faster courseRes = db.getAll( """ select a.*, b.status, b.last_section_id from Course a inner join StudentCourse b on a.course_id=b.course_id where b.student_id=%s; """, (userId,) ) courseCount = res else: page = res/5+1 if res%5 else res/5 courseRes = db.getSome( """ select a.*, b.status, b.last_section_id from Course a inner join StudentCourse b on a.course_id=b.course_id where b.student_id=%s; """, 5, (userId,) ) courseCount = 5 courseDetail = [] for i in range(0, courseCount): print (courseRes[i]) courseId = courseRes[i]['course_id'] teacherId = courseRes[i]['teacher_id'] teacherRes = db.getOne( """ select name, icon from User where user_id=%s; """, (teacherId,) ) if courseRes[i]['last_section_id'] is not None: sectionTitle = db.getOne( """ select title from Section where section_id=%s; """, (courseRes[i]['last_section_id'],) )["title"] else: sectionTitle = '' courseDetail.append({ "title": courseRes[i]['title'], "desc": courseRes[i]['description'], "id": str(courseRes[i]['course_id']), "teacher": teacherRes["name"], "lastProgress": { "title": sectionTitle, "id": str(courseRes[i]['last_section_id']) } }) respData = { "state": "success", "count": courseCount, "page": page, "courses": courseDetail } db.close() return respData