def exam_(): if r.hexists("user:"******"user"], "start_time"): all_question_id = r.lrange("user_question_id:" + session["user"], 0, current_app.config["once_exam_nums"]) else: r.hset("user:"******"user"], "start_time", int(time.time())) all_question_id = r.srandmember( "all_question_id", current_app.config["once_exam_nums"]) # 抽题 for i in all_question_id: r.rpush("user_question_id:" + session["user"], i) questions = [] for q_id in all_question_id: description = r.hget("question[%s]" % q_id, "description") answerA = r.hget("question[%s]" % q_id, "answerA") answerB = r.hget("question[%s]" % q_id, "answerB") answerC = r.hget("question[%s]" % q_id, "answerC") answerD = r.hget("question[%s]" % q_id, "answerD") question = { "id": q_id, "description": description, "answerA": answerA, "answerB": answerB, "answerC": answerC, "answerD": answerD } questions.append(question) return render_template("exam.html", match_name=app.config['match_name'], questions=questions)
def load_user_data(): res = {} all_question_id = r.lrange("user_question_id:" + session["user"], 0, r.llen("user_question_id:" + session["user"])) for i in all_question_id: res[i] = {"checked": 0, "mark": 0} if r.hexists("user:"******"user"], "question[%s]" % i): user_choise = r.hget("user:"******"user"], "question[%s]" % i) res[i]["checked"] = user_choise if r.hexists("user:"******"user"], "mark%s" % i): if r.hget("user:"******"user"], "mark%s" % i) == "1": res[i]["mark"] = 1 return jsonify(res)
def match_time(): left_time = int(time.time()) - int( r.hget("user:"******"user"], "start_time")) return jsonify({ "match_duration": app.config['match_duration'], "left_time": left_time })
def check(): has_started = r.hexists("user:"******"user"], "start_time") has_finished = r.hexists("user:"******"user"], "finish_time") if has_started and not has_finished: start_time = int(r.hget("user:"******"user"], "start_time")) now_time = int(time.time()) spend_time = now_time - start_time if spend_time > app.config["match_duration"] * 60: # 答题时长超出了设置的答题时长 return redirect(url_for("exam.check_answers")) else: return func() return func()
def sign_mark(): try: if r.hexists("user:"******"user"], "mark" + request.args.get('questionid')): if r.hget("user:"******"user"], "mark" + request.args.get('questionid')) == "1": r.hset("user:"******"user"], "mark" + request.args.get('questionid'), 0) return "0" # 取消标记 r.hset("user:"******"user"], "mark" + request.args.get('questionid'), 1) return "1" # 标记 except: return "error"
def check_answers(): score = 0 user_question_id = r.lrange("user_question_id:" + session["user"], 0, app.config["once_exam_nums"]) for q_id in user_question_id: q = "question[%s]" % q_id if r.hget("user:"******"user"], q) == r.hget(q, "answer"): score += app.config['one_question_score'] finish_time = int(time.time()) start_time = int(r.hget("user:"******"user"], "start_time")) spend_time = finish_time - start_time try: score_data = { "username": session["user"], "score": score, "finish_time": finish_time, "start_time": start_time, "spend_time": spend_time } db.session.add(Score(**score_data)) db.session.commit() r.hset("user:"******"user"], "score", score) r.hset("user:"******"user"], "finish_time", finish_time) r.hset("user:"******"user"], "spend_time", spend_time) status_code = 200 message = "交卷成功" except: status_code = 300 message = "交卷失败" callback_type = "forward" url = url_for("display.result") if request.method == "POST": res = '{"statusCode": %s,"message": "%s","callbackType": "%s","forwardUrl": "%s"}' % ( status_code, message, callback_type, url) return res return redirect(url_for("display.result"))