INNER CODE UNIT · Python

post_answer

kingname/SourceCodeofMongoRedis · chapter_11/QASystem/main.py:75

def post_answer():
    session_data = redis.fetch_session(request.cookies.get('session'))
    if not session_data:
        return json.dumps({'success': False, 'reason': '未登录!'})
    answer = request.json
    is_valid = check_answer_valid(answer)
    if not is_valid['success']:
        return json.dumps(is_valid, ensure_ascii=False)
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    question_id = answer['question_id']
    if redis.check_user_answer_question(session_data['user'], question_id):
        return json.dumps({'success': False, 'reason': '已经回答过这个问题,不能回答两次。'})
    author = session_data['user']
    answer = answer['answer']
    mongo.insert_answer(question_id, answer, author, now)
    redis.set_answer_flag(question_id, author)
    return json.dumps({'success': True})

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…