<commit_before>"""

Defines views, and renders data to html templates.

"""

from django.contrib.auth.decorators import login_required, user_passes_test
from django.template import RequestContext
from django.shortcuts import render_to_response, HttpResponse, get_object_or_404
from django.http import JsonResponse
from django.shortcuts import render
from oppgavegen.tables import *
from django_tables2 import RequestConfig
from oppgavegen.templatetags.app_filters import is_teacher
from oppgavegen import view_logic
from oppgavegen.view_logic import *
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from django.views.decorators.cache import cache_control
from oppgavegen.models import Set, Chapter, Level, Template

# Search Views and Forms
from .forms import QuestionForm, TemplateForm, SetForm, LevelCreateForm, ChapterNameForm
from django.forms.formsets import formset_factory
from django import http
from django.forms.models import modelformset_factory, inlineformset_factory
from django.core.urlresolvers import reverse


def is_member(user):
    """Returns true/false depending on if the user is a member of the teacher group (or is a superuser)"""
    if user.is_superuser:
        return True
    return user.groups.filter(name='Teacher').exists()


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@login_required
def task(request):
    """Returns a render of taskview.html with a rating apropriate math question"""
    context = RequestContext(request)
    question_type = request.GET.get('q', '')
    if question_type != "":
        context_dict = generation.generate_task(request.user, question_type)
    else:
        context_dict = generation.generate_task(request.user, "")
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    return render_to_response('taskview.html', context_dict, context)


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@login_required
def task_by_id_and_type(request, template_extra, desired_type='normal'):
    """Returns a render of taskview with a specific math template with specified type"""
    context = RequestContext(request)
    context_dict = generation.generate_task(request.user, template_extra, desired_type)
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    if context_dict['question'] == 'error':
        message = {'message': 'Denne oppgavetypen har ikke blitt laget for denne oppgaven'}
        return render_to_response('error.html', message, context)
    return render_to_response('taskview.html', context_dict, context)


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@login_required
def task_by_extra(request, template_extra):
    """Returns a render of taskview with a specific math template"""
    context = RequestContext(request)
    context_dict = generation.generate_task(request.user, template_extra)
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    return render_to_response('taskview.html', context_dict, context)


@login_required
@user_passes_test(is_teacher, '/')
def gen(request):
    """Returns a render of gen.html"""
    context = RequestContext(request)
    topics = ""
    for e in Topic.objects.all():  # Retrieves a list of topics and passes them to the view.
        topics += '§' + str(e.pk) + '§'
        topics += e.topic
    topics = topics[1:]
    context_dict = {'topics': topics}
    return render_to_response('gen.html', context_dict, context)


@login_required
@user_passes_test(is_teacher, '/')
def submit(request):
    """Returns a render of submit html. Different depending on if the submission goes through ot not"""
    message = 'don\'t come here'
    if request.method == 'POST':
        message = 'Det har skjedd noe feil ved innsending av form'
        form = TemplateForm(request.POST)
        if form.is_valid():
            template = form.save(commit=False)
            if request.REQUEST['pk'] != '':  # Can this be written as v = req != ''?
                template.pk = request.REQUEST['pk']  # Workaround, template doesn't automatically get template.pk
                update = True
            else:
                update = False
            message = view_logic.submit_template(template, request.user, update)

        else:
            print(form.errors)
    context = RequestContext(request)
    return render_to_response('submit.html', {'message': message}, context)


@login_required
def answers(request):
    """Returns a render of answers.html"""
    context = RequestContext(request)
    cheat_message = '\\text{Ulovlig tegn har blitt brukt i svar}'
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form_values = form.process()
            template = Template.objects.get(pk=form_values['primary_key'])
            if cheat_check(form_values['user_answer'], template.disallowed):
                return render_to_response('answers.html', {'answer': cheat_message}, context)
            context_dict = view_logic.make_answer_context_dict(form_values)
            view_logic.change_elo(template, request.user, context_dict['user_won'], form_values['template_type'])<commit_msg>Added ajax view for answers.<commit_after>"""

Defines views, and renders data to html templates.

"""

from django.contrib.auth.decorators import login_required, user_passes_test
from django.template import RequestContext
from django.shortcuts import render_to_response, HttpResponse, get_object_or_404
from django.http import JsonResponse
from django.shortcuts import render
from oppgavegen.tables import *
from django_tables2 import RequestConfig
from oppgavegen.templatetags.app_filters import is_teacher
from oppgavegen import view_logic
from oppgavegen.view_logic import *
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from django.views.decorators.cache import cache_control
from oppgavegen.models import Set, Chapter, Level, Template

# Search Views and Forms
from .forms import QuestionForm, TemplateForm, SetForm, LevelCreateForm, ChapterNameForm
from django.forms.formsets import formset_factory
from django import http
from django.forms.models import modelformset_factory, inlineformset_factory
from django.core.urlresolvers import reverse


def is_member(user):
    """Returns true/false depending on if the user is a member of the teacher group (or is a superuser)"""
    if user.is_superuser:
        return True
    return user.groups.filter(name='Teacher').exists()


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@login_required
def task(request):
    """Returns a render of taskview.html with a rating apropriate math question"""
    context = RequestContext(request)
    question_type = request.GET.get('q', '')
    if question_type != "":
        context_dict = generation.generate_task(request.user, question_type)
    else:
        context_dict = generation.generate_task(request.user, "")
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    return render_to_response('taskview.html', context_dict, context)


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True) # Doesn't cache the page
@login_required
def task_by_id_and_type(request, template_extra, desired_type='normal'):
    """Returns a render of taskview with a specific math template with specified type"""
    context = RequestContext(request)
    context_dict = generation.generate_task(request.user, template_extra, desired_type)
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    if context_dict['question'] == 'error':
        message = {'message': 'Denne oppgavetypen har ikke blitt laget for denne oppgaven'}
        return render_to_response('error.html', message, context)
    return render_to_response('taskview.html', context_dict, context)


@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
@login_required
def task_by_extra(request, template_extra):
    """Returns a render of taskview with a specific math template"""
    context = RequestContext(request)
    context_dict = generation.generate_task(request.user, template_extra)
    context_dict['rating'] = view_logic.get_user_rating(request.user)
    return render_to_response('taskview.html', context_dict, context)


@login_required
@user_passes_test(is_teacher, '/')
def gen(request):
    """Returns a render of gen.html"""
    context = RequestContext(request)
    topics = ""
    for e in Topic.objects.all():  # Retrieves a list of topics and passes them to the view.
        topics += '§' + str(e.pk) + '§'
        topics += e.topic
    topics = topics[1:]
    context_dict = {'topics': topics}
    return render_to_response('gen.html', context_dict, context)


@login_required
@user_passes_test(is_teacher, '/')
def submit(request):
    """Returns a render of submit html. Different depending on if the submission goes through ot not"""
    message = 'don\'t come here'
    if request.method == 'POST':
        message = 'Det har skjedd noe feil ved innsending av form'
        form = TemplateForm(request.POST)
        if form.is_valid():
            template = form.save(commit=False)
            if request.REQUEST['pk'] != '':  # Can this be written as v = req != ''?
                template.pk = request.REQUEST['pk']  # Workaround, template doesn't automatically get template.pk
                update = True
            else:
                update = False
            message = view_logic.submit_template(template, request.user, update)

        else:
            print(form.errors)
    context = RequestContext(request)
    return render_to_response('submit.html', {'message': message}, context)


@login_required
def answers(request, level=''):
    """Returns a render of answers.html"""
    context = RequestContext(request)
    cheat_message = '\\text{Ulovlig tegn har blitt brukt i svar}'
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form_values = form.process()
            template = Template.objects.get(pk=form_values['primary_key'])
            if cheat_check(form_values['user_answer'], template.disallowed):
                return render_to_response('answers.html', {'answer': cheat_message}, context)
            context_dict = view_logic.make_answer_context_dict(form_values)
            if request.is_ajax():
                view_logic.change_level_rating(template, request.user, context_dict['user_won'], form_values['template_type'], level)
                return render_to_response('game/answer.html', context_dict, context)
            else:
                view_logic.change_elo(template, request.user, context_dict['user_won'], form_values['template_type'])
{'fraction_non_alphanumeric': 0.09960416091319156, 'fraction_numerical': 0.0014728896253337015, 'mean_word_length': 4.151256519677572, 'pattern_counts': {'":': 2, '<': 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': '2805606', 'n_tokens_mistral': 3145, 'n_tokens_neox': 3073, 'n_words': 828}