<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.db import transaction

from apps.home.training.utils import get_quiz_or_404
from apps.users.models import TrainingResult


def training_list_page(request):
    from apps.home.training import training_summary
    return {'training_steps': training_summary.get_context(request.user)}


def intro_quiz(request):
    quiz_slug = 'intro_quiz'
    quiz = get_quiz_or_404(quiz_slug)
    return {
        'quiz': quiz,
        'quiz_slug': quiz_slug
    }


@transaction.atomic
def complete_quiz(request):
    from apps.home.training.types import Quiz
    quiz_slug = 'intro_quiz'
    quiz = get_quiz_or_404(quiz_slug)
    user = request.user

    answers = Quiz.extract_answers(request.POST)
    score = quiz.score(answers)
    correct_answers = list(quiz.correct_answers(answers))
    quiz_summary = list(_quiz_summary(quiz, answers))

    result, created = TrainingResult.objects.get_or_create(
        user_id=user.id,
        module_name=quiz_slug)

    best_score = max(result.score, score)
    result.score = best_score
    result.save()

    passed_quiz = (score >= quiz.passing_score)

    passed_quiz_bool = 'training_finished_%s' % quiz_slug
    if passed_quiz and getattr(user, passed_quiz_bool) is False:
        setattr(user, passed_quiz_bool, True)
        user.save()

    return {
        'quiz': quiz,
        'quiz_slug': quiz_slug,
        'quiz_summary': quiz_summary,
        'score': score,
        'best_score': best_score,
        'passed_quiz': passed_quiz,
        'correct_answers': correct_answers
    }
<commit_msg>Stop setting quiz finished boolean twice<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from django.db import transaction

from apps.home.training.utils import get_quiz_or_404
from apps.users.models import TrainingResult


def training_list_page(request):
    from apps.home.training import training_summary
    return {'training_steps': training_summary.get_context(request.user)}


def intro_quiz(request):
    quiz_slug = 'intro_quiz'
    quiz = get_quiz_or_404(quiz_slug)
    return {
        'quiz': quiz,
        'quiz_slug': quiz_slug
    }


@transaction.atomic
def complete_quiz(request):
    from apps.home.training.types import Quiz
    quiz_slug = 'intro_quiz'
    quiz = get_quiz_or_404(quiz_slug)
    user = request.user

    answers = Quiz.extract_answers(request.POST)
    score = quiz.score(answers)
    correct_answers = list(quiz.correct_answers(answers))
    quiz_summary = list(_quiz_summary(quiz, answers))

    result, created = TrainingResult.objects.get_or_create(
        user_id=user.id,
        module_name=quiz_slug)

    best_score = max(result.score, score)
    result.score = best_score
    result.save()

    passed_quiz = (score >= quiz.passing_score)

    return {
        'quiz': quiz,
        'quiz_slug': quiz_slug,
        'quiz_summary': quiz_summary,
        'score': score,
        'best_score': best_score,
        'passed_quiz': passed_quiz,
        'correct_answers': correct_answers
    }

{'fraction_non_alphanumeric': 0.09273570324574962, 'fraction_numerical': 0.0061823802163833074, 'mean_word_length': 3.5195530726256985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4614651', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1072, 'n_words': 215}