def contest_time(request):
    context = {}
    now = timezone.now()
    contest = models.ContestControl.objects.first()
    if now < contest.start:
        time = contest.start
    elif contest.start <= now <= contest.end:
        time = contest.end
    else:
        time = None
    context['contest_time'] = time
    context['contest_on'] = is_contest_on()
    context['contest_phase'] = contest_phase()
    return context
예제 #2
0
def question(request, qno):
    """Display a question and the form to accept the user's answer"""
    qno = int(qno)
    data = {}
    template = 'question/question.html'
    ques = get_object_or_404(models.Question, qno=qno)
    if (is_contest_on() or ques.practice):
        data['question'] = ques
        data['marks'] = data['question'].get_marks()
        data['attempts'] = models.Attempt.\
            objects.filter(question=data['question'],
                           player=request.user.profile).order_by('-stamp')
        if len(data['attempts']) > 0:
            last_correct = data['attempts'][0].correct
        else:
            last_correct = False
        data['last_correct'] = last_correct

        if not ques.has_been_answered(request.user):
            # Only if the question is yet to be answered, say yes
            if request.method == 'GET':
                data['answer_form'] = functions.get_attempt_form(
                    ques, request.user.profile)
            if request.method == 'POST':
                data['answer_form'] = models.AttemptForm(
                    request.POST, request.FILES)
                if data['answer_form'].is_valid():
                    form = data['answer_form']
                    attempt = form.save(commit=False)
                    attempt.player = request.user.profile
                    attempt.question = data['question']
                    attempt.marks = data['marks']
                    attempt.save()
                    if attempt.is_correct():  # force a check request
                        functions.update_marks(request.user.profile, attempt)
                    return redirect('question:question', qno=qno)

    return render(request, template, data)
예제 #3
0
def question(request, qno):
    """Display a question and the form to accept the user's answer"""
    qno = int(qno)
    data = {}
    template = 'question/question.html'
    ques = get_object_or_404(models.Question, qno=qno)
    if (is_contest_on() or ques.practice):
        data['question'] = ques
        data['marks'] = data['question'].get_marks()
        data['attempts'] = models.Attempt.\
            objects.filter(question=data['question'],
                           player=request.user.profile).order_by('-stamp')
        if len(data['attempts']) > 0:
            last_correct = data['attempts'][0].correct
        else:
            last_correct = False
        data['last_correct'] = last_correct

        if not ques.has_been_answered(request.user):
            # Only if the question is yet to be answered, say yes
            if request.method == 'GET':
                data['answer_form'] = functions.get_attempt_form(
                    ques, request.user.profile)
            if request.method == 'POST':
                data['answer_form'] = models.AttemptForm(
                    request.POST, request.FILES)
                if data['answer_form'].is_valid():
                    form = data['answer_form']
                    attempt = form.save(commit=False)
                    attempt.player = request.user.profile
                    attempt.question = data['question']
                    attempt.marks = data['marks']
                    attempt.save()
                    if attempt.is_correct():  # force a check request
                        functions.update_marks(request.user.profile, attempt)
                    return redirect('question:question', qno=qno)

    return render(request, template, data)