<commit_before>from django.contrib.auth.decorators import login_required, user_passes_test
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
from django.template import RequestContext
from django.shortcuts import render_to_response
from random import randint
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from oppgavegen import generation
#sympy
from sympy import sympify
from sympy.printing.mathml import mathml
from sympy import Integral, latex
from django.forms import ModelForm
# models and tables
from oppgavegen.models import Template
from oppgavegen.models import Topic
from oppgavegen.models import User
from oppgavegen.tables import *
from django_tables2 import RequestConfig
from oppgavegen.templatetags.app_filters import is_teacher
from oppgavegen import view_logic

def is_member(user): #Checks if a user is a member of a group
    if user.is_superuser:
        return True
    return user.groups.filter(name='Teacher').exists()

@login_required
def task(request):
    context = RequestContext(request)
    question_type = request.GET.get('q', '')
    if question_type != "":
        context_dict = generation.generate_task(question_type)
    else:
        context_dict = generation.generate_task("")
    context_dict['title'] = generation.printer()
    return render_to_response('taskview.html', context_dict, context)

@login_required
def task_by_id_and_type(request, template_id, desired_type='normal'):
    context = RequestContext(request)
    context_dict = generation.generate_task(template_id, desired_type)
    context_dict['title'] = generation.printer()
    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)

@login_required
def task_by_id(request, template_id):
    context = RequestContext(request)
    context_dict = generation.generate_task(template_id)
    context_dict['title'] = generation.printer()
    return render_to_response('taskview.html', context_dict, context)

class QuestionForm(forms.Form):
    user_answer = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400)
    primary_key = forms.IntegerField()
    variable_dictionary = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)
    template_specific = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)
    template_type = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=20)
    replacing_words = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)

    def process(self):
        cd = {'variable_dictionary' : self.cleaned_data['variable_dictionary'],'primary_key' : self.cleaned_data['primary_key'],
              'user_answer' : self.cleaned_data['user_answer'], 'template_type' : self.cleaned_data['template_type'],
              'template_specific' : self.cleaned_data['template_specific'],
              'replacing_words' : self.cleaned_data['replacing_words']}
        return cd

class TemplateForm(ModelForm):
    class Meta:
        model = Template
        fields = '__all__' #['question_text', 'solution', 'answer', 'variables','number_of_decimals','answer_can_be_zero','random_domain']

        def process(self):
            cd = [self.cleaned_data['question'], self.cleaned_data['answer']]
            return cd

@login_required
@user_passes_test(is_teacher, '/')
def gen(request):
    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):
    message = 'don\'t come here'
    if request.method == 'POST':
        message = 'failure!'
        #todo check input for errors
        form = TemplateForm(request.POST)
        if form.is_valid():
            template = form.save(commit=False)
            if request.REQUEST['pk'] != '': #can this be writter as v = req != ''?
                template.pk = request.REQUEST['pk'] #workaround, for some reason 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):
    context = RequestContext(request)
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        counter = 0
        if form.is_valid():
            form_values = form.process()
            context_dict = view_logic.make_answer_context_dict(form_values)
            return render_to_response('answers.html', context_dict, context)
        else:
            print(form.errors)
    return  render_to_response('answers.html')

@login_required
@user_passes_test(is_teacher, '/')
def templates(request):
    panel_title = "Alle Maler"
    table = BootstrapTemplateTable(Template.objects.all())
    RequestConfig(request, paginate={"per_page": 20}).configure(table)
    return render(request, "templates.html", {"table": table, "panel_title": panel_title})

@login_required
@user_passes_test(is_teacher, '/')
def template_table_by_user(request):
    user = request.user
    panel_title = "Dine Maler"
    table = UserTemplates(Template.objects.filter(creator=user))
    RequestConfig(request, paginate={"per_page": 20}).configure(table)
    return render(request, "templates.html", {"table": table, "panel_title": panel_title})

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


def edit_template(request, template_id):
    context = RequestContext(request)
    context_dict = view_logic.make_edit_context_dict(template_id)<commit_msg>Main table only shows validated templates.<commit_after>from django.contrib.auth.decorators import login_required, user_passes_test
from django.shortcuts import render
from django.http import HttpResponse
from django.template import Context, loader
from django.template import RequestContext
from django.shortcuts import render_to_response
from random import randint
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from oppgavegen import generation
#sympy
from sympy import sympify
from sympy.printing.mathml import mathml
from sympy import Integral, latex
from django.forms import ModelForm
# models and tables
from oppgavegen.models import Template
from oppgavegen.models import Topic
from oppgavegen.models import User
from oppgavegen.tables import *
from django_tables2 import RequestConfig
from oppgavegen.templatetags.app_filters import is_teacher
from oppgavegen import view_logic

def is_member(user): #Checks if a user is a member of a group
    if user.is_superuser:
        return True
    return user.groups.filter(name='Teacher').exists()

@login_required
def task(request):
    context = RequestContext(request)
    question_type = request.GET.get('q', '')
    if question_type != "":
        context_dict = generation.generate_task(question_type)
    else:
        context_dict = generation.generate_task("")
    context_dict['title'] = generation.printer()
    return render_to_response('taskview.html', context_dict, context)

@login_required
def task_by_id_and_type(request, template_id, desired_type='normal'):
    context = RequestContext(request)
    context_dict = generation.generate_task(template_id, desired_type)
    context_dict['title'] = generation.printer()
    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)

@login_required
def task_by_id(request, template_id):
    context = RequestContext(request)
    context_dict = generation.generate_task(template_id)
    context_dict['title'] = generation.printer()
    return render_to_response('taskview.html', context_dict, context)

class QuestionForm(forms.Form):
    user_answer = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400)
    primary_key = forms.IntegerField()
    variable_dictionary = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)
    template_specific = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)
    template_type = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=20)
    replacing_words = forms.CharField(widget=forms.widgets.HiddenInput(), max_length=400, required=False)

    def process(self):
        cd = {'variable_dictionary' : self.cleaned_data['variable_dictionary'],'primary_key' : self.cleaned_data['primary_key'],
              'user_answer' : self.cleaned_data['user_answer'], 'template_type' : self.cleaned_data['template_type'],
              'template_specific' : self.cleaned_data['template_specific'],
              'replacing_words' : self.cleaned_data['replacing_words']}
        return cd

class TemplateForm(ModelForm):
    class Meta:
        model = Template
        fields = '__all__' #['question_text', 'solution', 'answer', 'variables','number_of_decimals','answer_can_be_zero','random_domain']

        def process(self):
            cd = [self.cleaned_data['question'], self.cleaned_data['answer']]
            return cd

@login_required
@user_passes_test(is_teacher, '/')
def gen(request):
    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):
    message = 'don\'t come here'
    if request.method == 'POST':
        message = 'failure!'
        #todo check input for errors
        form = TemplateForm(request.POST)
        if form.is_valid():
            template = form.save(commit=False)
            if request.REQUEST['pk'] != '': #can this be writter as v = req != ''?
                template.pk = request.REQUEST['pk'] #workaround, for some reason 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):
    context = RequestContext(request)
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        counter = 0
        if form.is_valid():
            form_values = form.process()
            context_dict = view_logic.make_answer_context_dict(form_values)
            return render_to_response('answers.html', context_dict, context)
        else:
            print(form.errors)
    return  render_to_response('answers.html')

@login_required
@user_passes_test(is_teacher, '/')
def templates(request):
    panel_title = "Alle Maler"
    table = BootstrapTemplateTable(Template.objects.filter(valid_flag=True))
    RequestConfig(request, paginate={"per_page": 20}).configure(table)
    return render(request, "templates.html", {"table": table, "panel_title": panel_title})

@login_required
@user_passes_test(is_teacher, '/')
def template_table_by_user(request):
    user = request.user
    panel_title = "Dine Maler"
    table = UserTemplates(Template.objects.filter(creator=user))
    RequestConfig(request, paginate={"per_page": 20}).configure(table)
    return render(request, "templates.html", {"table": table, "panel_title": panel_title})

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


def edit_template(request, template_id):
    context = RequestContext(request)
    context_dict = view_logic.make_edit_context_dict(template_id)
{'fraction_non_alphanumeric': 0.10847814639283834, 'fraction_numerical': 0.0033100127886857744, 'mean_word_length': 4.15871168024835, 'pattern_counts': {'":': 14, '<': 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': '7418820', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3852, 'n_words': 890}