<commit_before>from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from contest import models
from contest.forms import AttemptForm, RegistrationForm, ProfileForm

def home(request):
    context = {}
    template = 'contest/home.html'
    context['contests'] = models.Contest.objects.filter(published=True)
    return render(request, template, context)


def contest(request, cpk):
    context = {}
    template = 'contest/contest.html'
    contest = get_object_or_404(models.Contest, pk=cpk)
    context['contest'] = contest
    context['questions'] = models.Question.objects.filter(contest=contest)
    return render(request, template, context)


def details(request):
    data = {}
    questions = models.Question.objects.all()
    data['question'] = {}
    for q in questions:
        tcase_list = models.TextCase.objects.filter(question=q)
        data['question'][str(q.pk)] = [(i.inp.url,
                                        i.out.url,
                                        i.exact_check) for i in tcase_list]
    data['language'] = {}
    languages = models.Language.objects.all()
    for l in languages:
        data['language'][str(l.pk)] = {'wrap': l.wrapper.url,
                                       'misc': l.name,
                                       'overwrite': l.strict_filename}
    return JsonResponse(data)




@login_required
def question(request, cpk, qpk):
    context = {}
    template = 'contest/question.html'

    context['contest'] = get_object_or_404(models.Contest, pk=cpk)
    context['question'] = get_object_or_404(models.Question, pk=qpk)
    profile_user = models.Profile.objects.filter(user=request.user)
    profile = profile_user.filter(contest=context['contest'])[0]

    if request.method == 'GET':
        
        context['answer_form'] = AttemptForm()
    elif request.method == 'POST':
        form = AttemptForm(request.POST, request.FILES)
        if form.is_valid():
            attempt = form.save(commit=False)
            attempt.profile = profile
            attempt.question = context['question']
            attempt.save()
        else:
            data['answer_form'] = form
    return render(request, template, context)

def signup(request):
    context = {}
    template = 'contest/signup.html'
    context['form'] = RegistrationForm()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            uname = form['username'].value()
            pwd = form['password'].value()
            p = models.User()
            p.username = uname
            p.set_password(<PASSWORD>)
            p.save()
            context['successful_registration'] = uname
        else:
            context['form'] = form
    return render(request, template, context)

@login_required
def register(request):
    context = {}
    template = 'contest/register.html'<commit_msg>Persistent code accross submissions<commit_after>from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from contest import models
from contest.forms import AttemptForm, RegistrationForm, ProfileForm

def home(request):
    context = {}
    template = 'contest/home.html'
    context['contests'] = models.Contest.objects.filter(published=True)
    return render(request, template, context)


def contest(request, cpk):
    context = {}
    template = 'contest/contest.html'
    contest = get_object_or_404(models.Contest, pk=cpk)
    context['contest'] = contest
    context['questions'] = models.Question.objects.filter(contest=contest)
    return render(request, template, context)


# TODO: This seems deprecated
def details(request):
    data = {}
    questions = models.Question.objects.all()
    data['question'] = {}
    for q in questions:
        tcase_list = models.TextCase.objects.filter(question=q)
        data['question'][str(q.pk)] = [(i.inp.url,
                                        i.out.url,
                                        i.exact_check) for i in tcase_list]
    data['language'] = {}
    languages = models.Language.objects.all()
    for l in languages:
        data['language'][str(l.pk)] = {'wrap': l.wrapper.url,
                                       'misc': l.name,
                                       'overwrite': l.strict_filename}
    return JsonResponse(data)




@login_required
def question(request, cpk, qpk):
    context = {}
    template = 'contest/question.html'

    context['contest'] = get_object_or_404(models.Contest, pk=cpk)
    context['question'] = get_object_or_404(models.Question, pk=qpk)
    profile_user = models.Profile.objects.filter(user=request.user)
    profile = profile_user.filter(contest=context['contest'])[0]
    last_attempt = profile.profile_attempt.order_by('-stamp').first()

    if request.method == 'GET':
        context['answer_form'] = AttemptForm(instance=last_attempt) if last_attempt else AttemptForm()
    elif request.method == 'POST':
        form = AttemptForm(request.POST, request.FILES)
        if form.is_valid():
            attempt = form.save(commit=False)
            attempt.profile = profile
            attempt.question = context['question']
            attempt.save()
            if attempt.correct:
                context['correct'] = True
            else:
                context['incorrect'] = True
            context['answer_form'] = AttemptForm(instance=last_attempt) if last_attempt else AttemptForm()
        else:
            context['answer_form'] = form
    return render(request, template, context)

def signup(request):
    context = {}
    template = 'contest/signup.html'
    context['form'] = RegistrationForm()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            uname = form['username'].value()
            pwd = form['password'].value()
            p = models.User()
            p.username = uname
            p.set_password(<PASSWORD>)
            p.save()
            context['successful_registration'] = uname
        else:
            context['form'] = form
    return render(request, template, context)

@login_required
def register(request):
    context = {}
    template = 'contest/register.html'
{'fraction_non_alphanumeric': 0.12057852538908977, 'fraction_numerical': 0.004087407640308128, 'mean_word_length': 2.570145903479237, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6685721', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1759, 'n_words': 367}