<commit_before># -*- coding: utf-8 -*-

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.template import RequestContext
from django.http import Http404,HttpResponseRedirect
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse,reverse_lazy
from django.db.models import Sum

from operator import itemgetter
from sandbox import Sandbox, SandboxConfig
from common.responses import JSONResponse
from cStringIO import StringIO
import sys

from puukissa.main.forms import LoginForm, ProfileForm
from puukissa.main.models import Lesson,Hint,HintUsage,Answer

def index(request):
    # Just print out index template. There is nothign else on this page.
    return render_to_response('main/index.html', {}, context_instance=RequestContext(request))

@login_required(login_url=reverse_lazy('main:login'))
def lessons(request):
    # Get lessons, prefoxmat some stuff
    lessons = []
    for lesson in Lesson.objects.all().order_by('number'):
        lesson.formatted_score = lesson.format_answer_score(request.user)
        lesson.formatted_status = lesson.format_status(request.user)
        lesson.status = lesson.get_status(request.user)
        lessons.append(lesson)
    
    # Render the template
    return render_to_response('main/lessons.html', {
        'lessons': lessons,
    }, context_instance=RequestContext(request))

@login_required(login_url=reverse_lazy('main:login'))
def top(request):
    # Get toplist to dict
    tmp = {}
    for a in Answer.objects.filter(status__in=[1,3]):
        if a.user.id in tmp:
            tmp[a.user.id]['score'] += a.get_score()
        else:
            tmp[a.user.id] = {
                'score': a.get_score(),
                'first_name': a.user.first_name,
                'last_name': a.user.last_name,
            }
    
    # Dump stuff to list
    top = []
    for k,v in tmp.items():
        top.append(v)
        
    # Sort by score
    top = sorted(top,key=itemgetter('score'))
    top.reverse()
    
    # Render the template
    return render_to_response('main/top.html', {
        'top': top,
    }, context_instance=RequestContext(request))

@login_required
def json_execute(request):
    # Make sure the request is in POST
    if request.method != "POST":
        raise Http404
    
    # Get vars
    answer_id = request.POST['answer_id']
    result = request.POST['content']
    
    # Find some objects
    answer = get_object_or_404(Answer, id=answer_id)
    
    # Make sure answer belongs to the user
    if answer.user != request.user:
        raise Http404

    # Run answer through sandbox, and capture stdout
    stdout_backup = sys.stdout
    sys.stdout = StringIO()
    sandbox = Sandbox(SandboxConfig('stdout'))
    sandbox.execute(result)
    output = sys.stdout.getvalue()
    sys.stdout = stdout_backup
    <commit_msg>Enable math module in sandbox<commit_after># -*- coding: utf-8 -*-

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.template import RequestContext
from django.http import Http404,HttpResponseRedirect
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse,reverse_lazy
from django.db.models import Sum

from operator import itemgetter
from sandbox import Sandbox, SandboxConfig
from common.responses import JSONResponse
from cStringIO import StringIO
import sys

from puukissa.main.forms import LoginForm, ProfileForm
from puukissa.main.models import Lesson,Hint,HintUsage,Answer

def index(request):
    # Just print out index template. There is nothign else on this page.
    return render_to_response('main/index.html', {}, context_instance=RequestContext(request))

@login_required(login_url=reverse_lazy('main:login'))
def lessons(request):
    # Get lessons, prefoxmat some stuff
    lessons = []
    for lesson in Lesson.objects.all().order_by('number'):
        lesson.formatted_score = lesson.format_answer_score(request.user)
        lesson.formatted_status = lesson.format_status(request.user)
        lesson.status = lesson.get_status(request.user)
        lessons.append(lesson)
    
    # Render the template
    return render_to_response('main/lessons.html', {
        'lessons': lessons,
    }, context_instance=RequestContext(request))

@login_required(login_url=reverse_lazy('main:login'))
def top(request):
    # Get toplist to dict
    tmp = {}
    for a in Answer.objects.filter(status__in=[1,3]):
        if a.user.id in tmp:
            tmp[a.user.id]['score'] += a.get_score()
        else:
            tmp[a.user.id] = {
                'score': a.get_score(),
                'first_name': a.user.first_name,
                'last_name': a.user.last_name,
            }
    
    # Dump stuff to list
    top = []
    for k,v in tmp.items():
        top.append(v)
        
    # Sort by score
    top = sorted(top,key=itemgetter('score'))
    top.reverse()
    
    # Render the template
    return render_to_response('main/top.html', {
        'top': top,
    }, context_instance=RequestContext(request))

@login_required
def json_execute(request):
    # Make sure the request is in POST
    if request.method != "POST":
        raise Http404
    
    # Get vars
    answer_id = request.POST['answer_id']
    result = request.POST['content']
    
    # Find some objects
    answer = get_object_or_404(Answer, id=answer_id)
    
    # Make sure answer belongs to the user
    if answer.user != request.user:
        raise Http404

    # Run answer through sandbox, and capture stdout
    stdout_backup = sys.stdout
    sys.stdout = StringIO()
    sandbox = Sandbox(SandboxConfig('stdout','math'))
    sandbox.execute(result)
    output = sys.stdout.getvalue()
    sys.stdout = stdout_backup
    
{'fraction_non_alphanumeric': 0.1004512786227645, 'fraction_numerical': 0.006017048303526659, 'mean_word_length': 3.246983676366217, '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': '4083300', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1773, 'n_words': 440}