示例#1
0
def gettrfromandr(request):
    data = []
    if request.POST:
        if request.POST.get('login') and request.POST.get('password'):
            print 'login and pass found'
            user = auth.authenticate(username=request.POST.get('login'), password=request.POST.get('password'))
            auth.login(request, user)
            for t in Teacher.objects.all():
                if t.login == request.POST.get('login'):
                    data = ['1', 't', t.id]
            for s in Student.objects.all():
                if s.login == request.POST.get('login'):
                    data = ['1', 's', s.id]
        elif request.POST.get('testresults'):
            print 'te'
            questions = []
            choices = []
            rightq = []
            falseq = []
            anddata = request.POST.get('testresults')
            data = anddata.split('/')
            print data
            studname = data[0].encode('utf8')
            student = Student.objects.get(login=studname)
            testid = data[1].encode('utf8')
            test = Test.objects.get(pk=int(testid))
            print test.grade, test.id, test.name
            for i, x in enumerate(data[2:len(data) - 1]):
                if i % 2 == 0:
                    questions.append(x)
                else:
                    choices.append(x)
            for i, x in enumerate(questions):
                print x.encode('utf-8'), questions
                print questions[i]
                question = Question.objects.get(pk=int(x))
                print question
                if question.test == test:
                    choices_ = Choice.objects.filter(question=question)
                    for j, y in enumerate(choices_):
                        for k in choices:
                            if y.right_choice and k[j] == '1':
                                rightq.append(y)
                            elif (not y.right_choice) and k[j] == '1':
                                falseq.append(y)
            try:
                tr = TestResult(student=student, test=test, balls=0,
                                quest_count=len(Question.objects.filter(test=test)),
                                unright_count=len(falseq),
                                right_count=len(rightq))
                tr.save()
                choicecount = 0
                for x in tr.test.question_set.all():
                    choicecount += len(x.choice_set.all())
                for x in rightq:
                    tr.right_choices.add(x)
                for x in falseq:
                    tr.unright_choices.add(x)
                tr.answers_count = choicecount
                tr.save()
            except Exception:
                print Exception
            print 'right: ' + str(rightq)
            print 'false' + str(falseq)
            print request.POST.get('testresults')
        elif request.POST.get('delete'):
            test = Test.objects.get(pk=int(request.POST.get('delete')))
            test.clean()
        elif request.POST.get('getav'):
            print request.POST.get('getav')
            student = Student.objects.get(login=request.POST.get('getav'))
            tested = False
            strtests = ""
            tests = Test.objects.all()
            for x in tests:
                for tr in TestResult.objects.all():
                    if tr.test == x and tr.student == student:
                        tested = True
                if not tested:
                    strtests += str(x.id) + "/"
                tested = False
            print "finished"
            return HttpResponse(strtests)
        elif request.POST.get('gettresults'):
            trs = []
            teacher = Teacher.objects.get(pk=int(request.POST.get('gettresults')))
            for x in TestResult.objects.all():
                if x.test.teacher == teacher:
                    trs.append(x.id)
            strtrs = ""
            for x in trs:
                strtrs += x + "/"
            return HttpResponse(strtrs)
        elif request.POST.get('gettestinfo'):
            test = Test.objects.get(pk=int(request.POST.get('getquestions')))
            return test.theme + "/" + test.school + "/" + test.grade
        elif request.POST.get('getquestions'):
            test = Test.objects.get(pk=int(request.POST.get('getquestions')))
            strqu = ""
            for x in Question.objects.filter(test=test):
                strqu += x.id + "/"
            return HttpResponse(strqu)
        elif request.POST.get('getchoices'):
            question = Question.objects.get(pk=int(request.POST.get('getchoices')))
            strch = ""
            for x in Choice.objects.filter(question=question):
                strch += x.id + "/"
            return HttpResponse(strch)
        elif request.POST.get('ttests'):
            teacher = Teacher.objects.get(pk=int(request.POST.get('ttests')))
            strtests = ""
            for x in Test.objects.filter(teacher=teacher):
                strtests += x.id + "/"
            return HttpResponse(strtests)
    return HttpResponse(data)
示例#2
0
def vote(request, test_id):
    p = get_object_or_404(Test, pk=test_id)
    student = Student.objects.get(login=request.session['username'])
    finished = False
    for t in TestResult.objects.all():
        if t.test == p and t.student == student:
            finished = True
    if not finished:
        checkbox_list = [x for x in request.POST if x.startswith('choice')]
        enter_list = [x for x in request.POST if x.startswith('enter')]
        list = []
        a = 0
        right_choices = []
        unright_choices = []
        selected_choices = []
        """get all choices with correct question"""
        for c in checkbox_list:
            id = c.replace('choice_', '')
            selected_choice = Choice.objects.get(id=int(id))
            if selected_choice.right_choice:
                list += 'True'
                a += 1
                right_choices.append(selected_choice)
            else:
                list += 'False'
                unright_choices.append(selected_choice)
        for c in enter_list:
            id = c.replace("enter_", "")
            value = request.POST.get(c)
            quest = Question.objects.get(pk=int(id))
            if value == quest.choice_set.all()[0].choice_text:
                a += 1
                right_choices.append(quest.choice_set.all()[0])
            else:
                unright_choices.append(quest.choice_set.all()[0])
        s = Student.objects.get(login=request.session['username'])
        tr = TestResult(student=s, test=p, balls=0, quest_count=len(p.question_set.all()))
        if (p in RndTest.objects.all()):
            rtest = RndTest.objects.get(name=p.name)
            tr = TestResult(student=s, test=p, balls=0, quest_count=rtest.qcount)
        tr.save()
        right_count = 0
        unright_count = 0
        choicecount = 0
        for x in tr.test.question_set.all():
            choicecount += len(x.choice_set.all())
        for x in right_choices:
            tr.right_choices.add(x)
            right_count += 1
        for x in unright_choices:
            tr.unright_choices.add(x)
            unright_count += 1
        tr.right_count = right_count
        tr.unright_count = unright_count
        tr.answers_count = choicecount
        tr.save()
        if a == len(checkbox_list):
            s.question_list.add(p)
        request.session['starts'] = None

        return redirect('/')
    else:
        raise Http404("You've already finished this test")