<commit_before>from random import SystemRandom

from flask import flash, g, redirect, render_template, request, session, url_for
from sqlalchemy import func

from pydrill import app, redis_store
from pydrill import utils
from pydrill.models import Answer, Question

urandom = SystemRandom()


@app.route('/ask/<question_id>/')
def ask_with_random_seed(question_id):
    return redirect(url_for('ask', question_id=question_id, seed=make_seed()))


@app.route('/ask/<question_id>/<seed>/')
def ask(question_id, seed):
    return render_template('ask.html', question=get_cur_question())


@app.route('/answer/<question_id>/<answer_id>/<seed>/', methods=['POST'])
def accept_answer(question_id, answer_id, seed):
    question, answer = get_cur_question_and_answer()
    if answer.is_correct and question.id not in g.user.answered:
        utils.add_score(g.user, question.difficulty)
    g.user.answered.add(question.id)
    remember_answer(answer)
    return redirect(url_for('ask', question_id=get_next_question().id, seed=make_seed()))


def remember_answer(answer):
    explain_url = url_for('explain', question_id=answer.question.id, answer_id=answer.id,
                          seed=g.seed)
    flash({'text': 'right!' if answer.is_correct else 'wrong!',
           'explain_url': explain_url,
           'is_correct': answer.is_correct}, 'answer')


@app.route('/score/')
def show_score():
    return render_template('score.html', team_scores=utils.get_team_scores())


@app.route('/explain/<question_id>/<answer_id>/<seed>/')
def explain(question_id, answer_id, seed):
    question, given_answer = get_cur_question_and_answer()
    return render_template('explain.html', question=question, given_answer=given_answer)


@app.before_request
def set_globals():
    # setting g.redis_pipeline first because it's used by utils.create_user
    g.redis_pipeline = redis_store.pipeline(transaction=False)
    g.seed = request.view_args.get('seed')
    if 'user' not in session:
        g.user = utils.create_user(request)
    else:
        g.user = utils.User(**session['user'])


@app.after_request
def execute_redis_pipeline(response):
    session['user'] = utils.user_as_dict(g.user)
    g.redis_pipeline.execute()
    return response

<commit_msg>Rename execute_redis_pipeline<commit_after>from random import SystemRandom

from flask import flash, g, redirect, render_template, request, session, url_for
from sqlalchemy import func

from pydrill import app, redis_store
from pydrill import utils
from pydrill.models import Answer, Question

urandom = SystemRandom()


@app.route('/ask/<question_id>/')
def ask_with_random_seed(question_id):
    return redirect(url_for('ask', question_id=question_id, seed=make_seed()))


@app.route('/ask/<question_id>/<seed>/')
def ask(question_id, seed):
    return render_template('ask.html', question=get_cur_question())


@app.route('/answer/<question_id>/<answer_id>/<seed>/', methods=['POST'])
def accept_answer(question_id, answer_id, seed):
    question, answer = get_cur_question_and_answer()
    if answer.is_correct and question.id not in g.user.answered:
        utils.add_score(g.user, question.difficulty)
    g.user.answered.add(question.id)
    remember_answer(answer)
    return redirect(url_for('ask', question_id=get_next_question().id, seed=make_seed()))


def remember_answer(answer):
    explain_url = url_for('explain', question_id=answer.question.id, answer_id=answer.id,
                          seed=g.seed)
    flash({'text': 'right!' if answer.is_correct else 'wrong!',
           'explain_url': explain_url,
           'is_correct': answer.is_correct}, 'answer')


@app.route('/score/')
def show_score():
    return render_template('score.html', team_scores=utils.get_team_scores())


@app.route('/explain/<question_id>/<answer_id>/<seed>/')
def explain(question_id, answer_id, seed):
    question, given_answer = get_cur_question_and_answer()
    return render_template('explain.html', question=question, given_answer=given_answer)


@app.before_request
def set_globals():
    # setting g.redis_pipeline first because it's used by utils.create_user
    g.redis_pipeline = redis_store.pipeline(transaction=False)
    g.seed = request.view_args.get('seed')
    if 'user' not in session:
        g.user = utils.create_user(request)
    else:
        g.user = utils.User(**session['user'])


@app.after_request
def save_user_and_execute_redis_pipeline(response):
    session['user'] = utils.user_as_dict(g.user)
    g.redis_pipeline.execute()
    return response


{'fraction_non_alphanumeric': 0.13030035335689047, 'fraction_numerical': 0.0, 'mean_word_length': 4.836340206185567, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1498144', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1467, 'n_words': 283}