def manage_questions(): form = AddQuestionForm() import_form = ImportQuestionsForm() if form.validate_on_submit(): q = Question(title=form.question.data, text=form.caption.data, points=form.points.data, number=form.number.data) db.session.add(q) db.session.commit() for answer in form.answers.data.split('\n'): a = Answer(question_id=q.id) if answer.startswith('+'): answer = answer.replace('+', '', 1).lstrip() a.is_correct = True a.text = answer db.session.add(a) db.session.commit() return redirect(url_for('manage_questions')) q = Question.query.order_by(Question.number).all() if not q: form.number.data = 0 else: form.number.data = q[-1].number + 10 form.points.data = 1 return render_template('questions.html', form=form, questions=Question.query.all(), show_correct=True, editable=True, import_form=import_form, shuffle=(lambda x: x))
def add_question(): if request.method == 'POST': category = request.form['category'] form = AddQuestionForm(request.form) if form.validate_on_submit(): g.db.execute('''insert into questions (question_text, category_id, pub_date, last_modified, user_id) values (?, ?, ?, ?, ?)''', [form.question_text.data, category, datetime.utcnow(), datetime.utcnow(), session['user_id']]) g.db.commit() flash("your question was successfully added, awesome!") return redirect(url_for('homepage')) return render_template( 'add-question.html', form=form, categories=query_db( 'select category_id, category_name from categories') )
def edit_question(question_id): q = Question.query.get(question_id) if q is None: flash(f'Вопроса с id={question_id} не существует') return redirect(back('manage_questions')) form = AddQuestionForm() if form.validate_on_submit(): q.title = form.question.data q.text = form.caption.data q.points = form.points.data q.number = form.number.data db.session.commit() flash('Вопрос отредактирован', 'success') return redirect(url_for('manage_questions')) form.question.data = q.title form.caption.data = q.text form.points.data = q.points form.answers.data = '+Unsupported...' form.number.data = q.number return render_template('edit_question.html', form=form)
def add_question(): if not (session.get('logged_in')): abort(401) if not session.get('role_id') > 4: abort(405) form = AddQuestionForm() if form.validate_on_submit(): b_i = int(form.block_id.data) q_t = str(form.question_text.data) c_a_t = str(form.correct_answer_text.data) flash(f"получены данные: {b_i}, {q_t}, {c_a_t}") conn = get_db() cur = conn.cursor() try: print('try to insert') cur.execute( '''INSERT INTO QUESTIONS (block_id, question_text) values (?, ?)''', (b_i, q_t)) print('try to select') cur.execute( ''' SELECT question_id from questions where question_text = ?''', (q_t, )) print('try to fetch') q_i = int(cur.fetchone()[0]) flash(f'Вопрос с ид {q_i} добавлен') cur.execute( '''INSERT INTO CorrectAnswers (question_id, c_answer_text) values (?, ?)''', (q_i, c_a_t)) conn.commit() flash('New question was successfully added') return redirect(url_for('manage_questions')) except BaseException as be: print('add block error: ', type(be), be) return be return redirect(url_for('show_entries')) return render_template('add_question_form.html', title='add question', form=form)
def add_question(): print "REq args ----------->", request.args.get('exam_id') form = AddQuestionForm() choices = [(int(each.id), each.exam) for each in Exams.query.all()] form.exam_id.choices = [(0, "-- Select Subject --")] form.exam_id.choices += choices print "form.validate_on_submit()", request.method, form.validate( ), form.validate_on_submit() print "Request method ----------->", request.form if request.method == 'POST': if form.validate() == False: flash("All fields are required") return render_template('add_questions.html', form=form) else: ques = Questions.query.filter_by( question=request.form['name']).first() if not ques: print "request.form['right_choice']>>>>>>>", request.form[ 'right_choice'], request.form['exam_id'] question = Questions(question=request.form['name'], is_active=request.form['is_active'], right_choice=request.form['right_choice'], exam_id=request.form['exam_id']) db.session.add(question) db.session.commit() option1 = False option2 = False option3 = False option4 = False if str(request.form['right_choice']) == 'option1': option1 = True elif str(request.form['right_choice']) == 'option2': option2 = True elif str(request.form['right_choice']) == 'option3': option3 = True elif str(request.form['right_choice']) == 'option4': option4 = True opt1 = QuestionsChoices(choice=request.form['option1'], is_right=option1, main_question=question) opt2 = QuestionsChoices(choice=request.form['option2'], is_right=option2, main_question=question) opt3 = QuestionsChoices(choice=request.form['option3'], is_right=option3, main_question=question) opt4 = QuestionsChoices(choice=request.form['option4'], is_right=option4, main_question=question) db.session.add(opt1) db.session.add(opt2) db.session.add(opt3) db.session.add(opt4) db.session.commit() flash("Questions added sucessfully!") return render_template('questions_added.html', form=form, questions=Questions.query.all()) return render_template('questions_added.html')