Esempio n. 1
0
def test(page):
    """Checks that id has been set in the session and renders the test template if so, if
    id has not been set it redirects back to start."""
    if session.has_key('id'):
        num_questions = Question.questionCount()
        if not num_questions:
            abort(404)

        #start a test session if not already started
        if not session.has_key('test'):
            temp_test = {}
            session['test'] = temp_test
        #check for answers, if any, to save
        if request.method == 'POST':
            temp_test = session['test']
            for k, v in request.form.iteritems():
                temp_test[k] = v
            session['test'] = temp_test

        #how many questions per page? 
        PER_PAGE = 10                 
        #randomize the questions per page, seeded by uid
        questions = Question.getPage(page, PER_PAGE)
        random.seed(session['id'])
        random.shuffle(questions)
        #paginate questions
        paginate = Paginator(page, PER_PAGE, num_questions)
        return render_template('test.html', questions=questions, pagination=paginate)

    else:
        return redirect(url_for('start'))
def get_questions():
    count = int(raw_input('Number of questions to enter: '))
    answers = {}
    for i in range(0, count):
        question = Question()
        question.name = 'q' + str(i)
        question.text = raw_input('Enter question #' + str(i+1) + ': ')
        question.answers = []
        
        items = 'abcde'
        for c in items:
            answer = {}
            answer['text'] = raw_input('Enter answer ' + c + ': ')
            answer['value'] = c
            question.answers.append(answer)
            cont = raw_input('Enter another answer(n to exit)?: ')
            if 'n' in cont:
                break
        #get the correct answer
        cor = raw_input('What letter was the correct answer for this question?: ')
        answers[question.name] = cor
        #save the new question
        try:
            question.save()
        except KeyError:
            print 'Duplicate questions name - you must clear the database first.'
            
    
    #write the answers to answers.json.temp
    answers_file = open('answers.json.temp', 'w')
    answers_file.write(json.dumps(answers))
    answers_file.close()

    return