Exemple #1
0
def edit_question(question_id=-1):
    form = NewQuestionForm(request.form)
    #answerzip = zip(form.answers, form.validities)
    answers = []
    if request.method == 'POST' and form.validate_on_submit():
        for answer in form.answers:
            answers.append(
                Answer(text=answer.answer.data,
                       is_correct=answer.is_correct.data))

        question = Question.query.get(question_id)
        question.text = form.question.data
        Answer.query.filter(Answer.question_id == question.id).delete()
        question.answers = answers
        db.session.add(question)
        db.session.commit()

        return redirect(url_for('topic_questions', topic_id=question.topic.id))

    question = Question.query.get(question_id)
    for i in range(2):
        form.answers.pop_entry()

    form.question.data = question.text
    for answer in question.answers:
        answer_form = AnswerForm()
        answer_form.answer = answer.text
        answer_form.is_correct = answer.is_correct
        form.answers.append_entry(answer_form)

    return render_template('question.html', title='New Question', form=form)
Exemple #2
0
def new_question(topic_id=-1):
    form = NewQuestionForm(request.form)
    #answerzip = zip(form.answers, form.validities)
    answers = []
    if request.method == 'POST' and form.validate_on_submit():
        for answer in form.answers:
            answers.append(
                Answer(text=answer.answer.data,
                       is_correct=answer.is_correct.data))

        question = Question(text=form.question.data,
                            author=g.user,
                            topic=Topic.query.get(topic_id),
                            answers=answers)
        db.session.add(question)
        db.session.commit()

        return redirect(url_for('topic_questions', topic_id=topic_id))
        #question = Question(texty, author, topic, answers)

    return render_template('question.html', title='New Question', form=form)