<commit_before>import json
from django import forms
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import View
from lazysignup.decorators import allow_lazy_user
from core import mdetect
from model.utils import process_answer, recalculate_model
from model.models import Skill
from model.recommendation import recommend_questions

from questions.models import Question, Answer, Simulator


@ensure_csrf_cookie
@allow_lazy_user
def play(request, skill, pk=None):
    if pk:
        skill = get_object_or_404(Skill, pk=pk, parent__name=skill)
    else:
        skill = get_object_or_404(Skill, name=skill)
    simulators = Simulator.objects.filter(questions__skill__in=skill.children_list.split(",")).distinct()

    return render(request, 'questions/play.html', {
        "simulators": simulators,
        "skill": skill,
        "device": get_device_info(request)
    })


@login_required
def get_questions(request):
    """
    load recommended questions from server as jason
    """
    skill = get_object_or_404(Skill, pk=request.GET["skill"])
    subskills = skill.active_children_list()
    in_queue = [] if request.GET["in_queue"] == "" else request.GET["in_queue"].split(",")
    if "" in in_queue:
        in_queue.remove("")

    simulators = [] if request.GET["simulators"] == "" else request.GET["simulators"].split(",")

    questions = recommend_questions(int(request.GET["count"]), request.user, subskills, in_queue, simulators)

    return HttpResponse(json.dumps([q.as_json() for q in questions]))


@login_required
def get_selected_question(request, question_pk):
    q = get_object_or_404(Question, pk=question_pk)
    return HttpResponse(json.dumps([q.as_json()]))


# @staff_member_required
def get_question_test(request):
    """
    test page for question recommendation
    """
    skill = get_object_or_404(Skill, pk=request.GET["skill"])
    subskills = skill.active_children_list()
    in_queue = [] if request.GET["in_queue"] == "" else request.GET["in_queue"].split(",")

    questions = recommend_questions(int(request.GET["count"]), request.user, subskills, in_queue)

    if "json" in request.GET:
        return HttpResponse(json.dumps([q.as_json() for q in questions]), content_type="application/json")

    return render(request, 'questions/get_question_test.html', {
        "questions": questions
    })


@login_required
def save_answer(request):
    """
    save answer to question and process it
    """
    device = get_device_info(request)

    if request.method == "POST":
        data = json.loads(request.body)
        answer = Answer.objects.create(
            question_id=data["pk"],
            user=request.user,
            log=data["log"],
            solving_time=data["time"],<commit_msg>fix for invalid solving time<commit_after>import json
from django import forms
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic import View
from lazysignup.decorators import allow_lazy_user
from core import mdetect
from model.utils import process_answer, recalculate_model
from model.models import Skill
from model.recommendation import recommend_questions

from questions.models import Question, Answer, Simulator


@ensure_csrf_cookie
@allow_lazy_user
def play(request, skill, pk=None):
    if pk:
        skill = get_object_or_404(Skill, pk=pk, parent__name=skill)
    else:
        skill = get_object_or_404(Skill, name=skill)
    simulators = Simulator.objects.filter(questions__skill__in=skill.children_list.split(",")).distinct()

    return render(request, 'questions/play.html', {
        "simulators": simulators,
        "skill": skill,
        "device": get_device_info(request)
    })


@login_required
def get_questions(request):
    """
    load recommended questions from server as jason
    """
    skill = get_object_or_404(Skill, pk=request.GET["skill"])
    subskills = skill.active_children_list()
    in_queue = [] if request.GET["in_queue"] == "" else request.GET["in_queue"].split(",")
    if "" in in_queue:
        in_queue.remove("")

    simulators = [] if request.GET["simulators"] == "" else request.GET["simulators"].split(",")

    questions = recommend_questions(int(request.GET["count"]), request.user, subskills, in_queue, simulators)

    return HttpResponse(json.dumps([q.as_json() for q in questions]))


@login_required
def get_selected_question(request, question_pk):
    q = get_object_or_404(Question, pk=question_pk)
    return HttpResponse(json.dumps([q.as_json()]))


# @staff_member_required
def get_question_test(request):
    """
    test page for question recommendation
    """
    skill = get_object_or_404(Skill, pk=request.GET["skill"])
    subskills = skill.active_children_list()
    in_queue = [] if request.GET["in_queue"] == "" else request.GET["in_queue"].split(",")

    questions = recommend_questions(int(request.GET["count"]), request.user, subskills, in_queue)

    if "json" in request.GET:
        return HttpResponse(json.dumps([q.as_json() for q in questions]), content_type="application/json")

    return render(request, 'questions/get_question_test.html', {
        "questions": questions
    })


@login_required
def save_answer(request):
    """
    save answer to question and process it
    """
    device = get_device_info(request)

    if request.method == "POST":
        data = json.loads(request.body)
        answer = Answer.objects.create(
            question_id=data["pk"],
            user=request.user,
            log=data["log"],
            solving_time=data["time"] if data["time"] else "0",
{'fraction_non_alphanumeric': 0.1178417742469381, 'fraction_numerical': 0.006123800066203244, 'mean_word_length': 4.259355961705831, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, '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': '1955896', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1833, 'n_words': 401}