def query(self): id = request.args.get("id") or request.form.get("id") #判断参数是否存在 if not id: return error.error_1001() row = Question.QueryJoinCategory().filter(Question.id == id).first() question = {} if row: opts = Option.Query().filter_by(question_id=row.Question.id).all() options = [] for op in opts: option = { "id": op.id, "label": op.label, "content": op.content, "created_time": datetime.timestamp(op.created_time) } options.append(option) question = { "id": row.Question.id, "content": row.Question.content, "type": row.Question.type, "category_id": row.Question.category_id, "category_name": row.Category.name, "subject": row.Category.subject, "right_answer": row.Question.right_answer, "answer_analyze": row.Question.answer_analyze, "options": options, "created_time": datetime.timestamp(row.Question.created_time) } return error.success(question)
def query(self): subject = request.args.get("subject") or request.form.get("subject") # 判断是否选择学科 if not subject: return error.error_1001() # 查询用户已答题 user_id = g.uid user_answered = Answer.Query().filter_by(user_id=user_id).all() user_answered_ids = [] for r in user_answered: user_answered_ids.append(r.question_id) print(user_answered_ids) user_answered_ids_t = tuple(user_answered_ids) # 查出没有答过的题目 res = Question.QueryJoinCategory().filter( Category.subject == subject).filter( not_(Question.id.in_(user_answered_ids_t))).order_by( func.rand()).first() count = Question.QueryJoinCategory().filter( Category.subject == subject).filter( not_(Question.id.in_(user_answered_ids_t))).count() print(count) question = {} if res: # 获取题目选项 rows = Option.Query().filter_by(question_id=res.Question.id).all() options = [] for r in rows: option = {"id": r.id, "label": r.label, "content": r.content} options.append(option) question = { "id": res.Question.id, "type": res.Question.type, "content": res.Question.content, "category_name": res.Category.name, "category_id": res.Category.id, "options": options } return error.success(question)
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") print(subject) # 获取答案和分类 query = Question.QueryJoinCategory() if subject: query = query.filter(Category.subject == subject) rows = query.offset((pageNo - 1) * pageSize).limit(pageSize).all() count = query.count() pageCount = math.ceil(count / pageSize) questions = [] for r in rows: # 查询选项 ops = Option.Query().filter_by(question_id=r.Question.id).all() print(ops) options = [] for op in ops: option = { "id": op.id, "label": op.label, "content": op.content, "created_time": datetime.timestamp(op.created_time) } options.append(option) # 整理question数据 question = { "id": r.Question.id, "content": r.Question.content, "type": r.Question.type, "category_id": r.Question.category_id, "category_name": r.Category.name, "subject": r.Category.subject, "options": options, "created_time": datetime.timestamp(r.Question.created_time) } questions.append(question) return error.success({ "list": questions, "page": { "total": count, "pageCount": pageCount } })