コード例 #1
0
	def query(self):
		id = g.uid
		row = User.Query().filter_by(id=id).first()

		has_answered = Answer.Query().filter_by(user_id=g.uid).count()
		has_incorrect = Answer.Query().filter_by(user_id=g.uid, is_right=False).count()
		user = {
			"id": row.id,
			"name": row.name,
			"phone": row.phone,
			"has_answered": has_answered,
			"has_incorrect": has_incorrect
		}
		return error.success(user)
コード例 #2
0
    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})
コード例 #3
0
    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)
コード例 #4
0
    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)