def query(self): pageNo = int( request.args.get("pageNo") or request.form.get("pageNo") or 1) pageSize = int( request.args.get("pageSize") or request.form.get("pageSize") or 10) # 查询数据库 rows = Paper.Query().offset( (pageNo - 1) * pageSize).limit(pageSize).all() count = Paper.Query().count() pageCount = math.ceil(count / pageSize) papers = [] for r in rows: question_count = Question.Query().filter_by(paper_id=r.id).count() paper = { "id": r.id, "name": r.name, "question_count": question_count, "subject": r.subject, "created_time": datetime.timestamp(r.created_time) } papers.append(paper) return error.success({ "list": papers, "page": { "total": count, "pageCount": pageCount } })
def query(self): pageNo = int( request.args.get("pageNo") or request.form.get("pageNo") or 1) pageSize = int( request.args.get("pageSize") or request.form.get("pageSize") or 10) query = Answer.Query() rows = query.offset((pageNo - 1) * pageSize).limit(pageSize).all() answers = [] for r in rows: user = User.Query().filter_by(id=r.user_id).first() question = Question.Query().filter_by(id=r.question_id).first() print(question.category_id) category = Category.Query().filter_by( id=question.category_id).first() answer = { "id": r.id, "username": user.name, "subject": question.subject, "category": category.name, "question_id": question.id, "question_content": question.content, "is_right": r.is_right, "user_answer": r.user_answer } answers.append(answer) return error.success({"list": answers})
def post(self): id = request.form.get("id") if not id: return error.error_1001() # 查询数据库 question = Question.Query().filter_by(id=id).first() if question: question.is_valid = False db.session.add(question) db.session.commit() # 返回结果 return error.success()
def post(self): form = request.form print(form) content = form.get("content") category_id = form.get("category_id") right_answer = form.get("right_answer") options = form.get("options") # 判断参数是否为空 if not content or not right_answer or not category_id: return error.error_1001() # 判断right_answer是否合法 answers_lst = right_answer.split(",") for answer in answers_lst: if answer not in ["A", "B", "C", "D", "E"]: return error.error_1007() # 判断题目是否重复 content_count = Question.Query().filter_by(content=content).count() if content_count: return error.error_1002("题目不能重重") # 将题目添加到到数据库 question = Question(form) db.session.add(question) db.session.flush() # 获取题目的临时id question_id = question.id # 解析选项 options = json.loads(options) for r in options: r["question_id"] = question_id # 判断label是否合法 if not r.get("label") or r.get("label") not in [ "A", "B", "C", "D", "E" ]: return error.error_1007() # 同一题目A,B,C,D,E出现两次 label_count = Option.Query().filter_by(question_id=question_id, label=r["label"]).count() print("label_count ", label_count) if label_count: return error.error_1002(r["label"] + "重复") option = Option(r) db.session.add(option) db.session.commit() return error.success()
def post(self): id = request.form.get("id") if not id: return error.error_1001() # 查询该考试中是否有题目 question_count = Question.Query().filter_by(paper_id=id).count() if question_count: return error.error_1008("此考试中有题目,不能被删除!") # 查询数据库 paper = Paper.Query().filter_by(id=id).first() if paper: paper.is_valid = False db.session.add(paper) db.session.commit() return error.success()
def post(self): form = request.form # 获取参数 id = form.get("id") content = form.get("content") category_id = form.get("category_id") right_answer = form.get("right_answer") options = form.get("options") answer_analyze = form.get("answer_analyze") print(answer_analyze) #判断参数是否为空 if not id: return error.error_1001() # 查询数据库 question = Question.Query().filter_by(id=id).first() # 判断需要修改的数据 # 修改题目标题 if content: question.content = content # 修改题目分类 if category_id: question.category_id = category_id # 修改正确答案 if right_answer: question.right_answer = right_answer answers_lst = right_answer.split(",") length = len(answers_lst) type = "single" if length > 1: type = "double" question.type = type # 修改题目选项 if options: options = json.loads(options) for op in options: option = Option.Query().filter_by(id=op.get("id")).first() option.content = op.get("content") option.label = op.get("label") db.session.add(option) # 修改题目答案解析 if answer_analyze: question.answer_analyze = answer_analyze # 提交到数据库 db.session.add(question) db.session.commit() return error.success()
def post(self): id = request.form.get("id") # 判断id是否为空 if not id: return error.error_1001() # 查看该分类下是否有题目 question_count = Question.Query().filter_by(category_id=id).count() if question_count: return error.error_1008("该分类中有题目,不能被删除"); # 查询数据库 category = Category.Query().filter_by(id=id).first() # 判断category是否存在 if category: # 修改并提交到数据库 category.is_valid = False db.session.add(category) db.session.commit() return error.success()
def query(self): pageNo = int(request.args.get("pageNo") or request.form.get("pageNo") or 1) pageSize = int(request.args.get("pageSize") or request.form.get("pageSize") or 10) subject = request.args.get("subject") or request.form.get("subject") or "" # 查询数据库 query = Category.Query().filter(subject and (Category.subject == subject)) rows = query.offset((pageNo - 1)*pageSize).limit(pageSize).all() count = query.count() pageCount = math.ceil(count / pageSize) categories = [] for r in rows: question_count = Question.Query().filter_by(category_id = r.id).count() category = { "id": r.id, "name": r.name, "subject": r.subject, "chapter": r.chapter, "question_count": question_count, "created_time": datetime.timestamp(r.created_time) } categories.append(category) return error.success({"list": categories, "page": {"total": count, "pageCount": pageCount}})
def post(self): form = request.form # 获取参数 question_id = form.get("question_id") user_answer = form.get("user_answer") # 判断参数是否存在 if not question_id or not user_answer: return error.error_1001() # 判断用户是否重复回答 answer_count = Answer.Query().filter_by( user_id=g.uid, question_id=question_id).count() if answer_count: return error.error_1002("此题已经回答") # 查询数据库 question = Question.Query().filter_by(id=question_id).first() if not question: return error.error_1009("题目不存在") is_right = user_answer == question.right_answer postData = { "question_id": question_id, "user_answer": user_answer, "user_id": g.uid, "is_right": is_right } answer = Answer(postData) db.session.add(answer) db.session.commit() result = { "id": answer.id, "is_right": is_right, "right_answer": question.right_answer, "user_answer": user_answer } return error.success(result)
def query(self): subject = request.args.get("subject") or request.form.get("subject") # 判断subject是否存在 if not subject: return error.error_1001() # 判断subject值是否正确 if subject not in config.subjects: return error.error_1006() # 查询数据库 rows = Category.Query().filter_by(subject=subject).all() print(rows) categories = [] for r in rows: question_count = Question.Query().filter_by(category_id=r.id).count() category = { "id": r.id, "name": r.name, "subject": r.subject, "chapter": r.chapter, "question_count": question_count, "created_time": datetime.timestamp(r.created_time) } categories.append(category) return error.success(categories)