Пример #1
0
def dashboard(request):
    context = RequestContext(request)
    quiz = Page.objects.get(slug="online-quiz")
    site = Site.objects.all()[0]
    if not QuizProfile.objects.all().filter(user=request.user):
        quiz_profile = QuizProfile(user=request.user, total_score = 0.0, fb_share_key = ''.join(random.choice(string.letters) for i in xrange(300)))
        quiz_profile.save()
    else:
        quiz_profile = get_object_or_404(QuizProfile, user=request.user)
    qp_set = QuizProfile.objects.all().order_by('-total_score')
    players_count = len(qp_set)
    
    if request.user.is_superuser:
        top_hundread = qp_set
    else:
        if len(qp_set)>100:
            top_hundread = qp_set[:100]
        else:
            top_hundread = qp_set
    
    rank = 1
    for qp in qp_set:
        if qp == quiz_profile:
            break
        rank += 1
    
    return render_to_response( "quiz/dashboard.html", {"quiz_profile":quiz_profile,"rank":rank, "top_hundread":top_hundread,"site":site, "players_count":players_count, "quiz":quiz,}, context_instance = context)
Пример #2
0
def show_new_question(request):
    context = RequestContext(request)
    if not QuizProfile.objects.all().filter(user=request.user):
        quiz_profile = QuizProfile(user=request.user, total_score = 0.0, fb_share_key = ''.join(random.choice(string.letters) for i in xrange(300)))
        quiz_profile.save()
    else:
        quiz_profile = get_object_or_404(QuizProfile, user=request.user)
    if request.method == 'POST': # If the form has been submitted...
        form = QuestionForm(request.POST)
        if form.is_valid():
            submitted_option = form.cleaned_data['correct_option']
            submitted_question = get_object_or_404(Question, pk= int(request.POST['question']))
            attempted_question = get_object_or_404(AttemptedQuestion, player = quiz_profile, question = submitted_question,)

            if attempted_question.answered:
                return HttpResponseRedirect("/quiz/")

            time_taken = float(request.POST['time_taken'])

            marks = 0.0
            if submitted_question.correct_option == int(submitted_option):
                success = True
                if time_taken <= submitted_question.max_time and time_taken >= submitted_question.min_time:
                    marks = submitted_question.max_marks*submitted_question.min_time/time_taken
                elif time_taken < submitted_question.min_time:
                    marks = submitted_question.max_marks
            else:
                success = False
            marks = round(marks,2)

            alert = False
            attempted_question.time_taken = time_taken
            attempted_question.server_time_taken = calc_time(attempted_question.time, datetime.datetime.now())

            if attempted_question.server_time_taken - attempted_question.time_taken > 10:
                marks = 0.0
                alert = True


            attempted_question.marks_obtained = marks
            attempted_question.answered = True
            attempted_question.save()
            
            quiz_profile.update()
            return render_to_response( "quiz/submit_done.html", {"opt":submitted_option, "success":success,"question": submitted_question, "marks":marks, "time":time_taken, "alert":alert, }, context_instance = context)
    else:
        form = QuestionForm()
        questions = Question.objects.all()
        attempted_questions = AttemptedQuestion.objects.all().filter(player__user = request.user)

        limit_exceeded = False
        aqs_today = AttemptedQuestion.objects.filter(player__user= request.user, time__day=datetime.datetime.now().day)
        aqs = []
        daily_limit = 30
        
        if len(questions) == len(attempted_questions) or len(questions) == 0 or aqs_today.count() >= daily_limit :
            if aqs_today.count() >= daily_limit:
                limit_exceeded = True
            return render_to_response( "quiz/no_active_question.html",{"limit_exceeded":limit_exceeded, "daily_limit":daily_limit}, context_instance = context)
            
        for a in attempted_questions:
            try:
                aqs.append(a.question)
            except:
                continue
        
        while True:            
            q = questions[random.randint(0, len(questions)-1)]
            if q in aqs:
                continue
            else:
                break

        attempted_question = AttemptedQuestion(question = q, player = quiz_profile)
        attempted_question.save()

        quiz_profile.update()
        return render_to_response( "quiz/show_new_question.html", {"question":q, "form":form }, context_instance = context)