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_questions(modeladmin, request, queryset): '''批量添加问题''' opts = modeladmin.model._meta app_label = opts.app_label if request.POST.get('post'): form = AddQuestionForm([], initial={ '_selected_action': request.POST.getlist( admin.ACTION_CHECKBOX_NAME) }, data=request.POST) n = queryset.count() if n: if form.is_valid(): question = Question.objects.get( id=form.cleaned_data['question_id']) for article in queryset: article.questions.add(question) modeladmin.message_user(request, '成功修改%d个产品' % n) return None else: modeladmin.message_user(request, '输入有误,请重新提交;%s' % str(form.errors)) pass else: modeladmin.messag_user(request, '未选择任何数据') return None title = '添加问题' action = 'add_questions' form = AddQuestionForm( [], initial={'_selected_action': [i.id for i in queryset]}) context = { 'title': title, 'queryset': queryset, 'count': queryset.count(), 'opts': opts, 'app_label': app_label, 'action': action, 'action_checkbox_name': admin.ACTION_CHECKBOX_NAME, 'form': form, } return TemplateResponse(request, 'zhufu/admin/add_questions.html', context=context, current_app=modeladmin.admin_site.name)
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_questions(modeladmin, request, queryset): '''批量添加问题''' opts = modeladmin.model._meta app_label = opts.app_label if request.POST.get('post'): form = AddQuestionForm([], initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)}, data = request.POST) n = queryset.count() if n: if form.is_valid(): question = Question.objects.get(id=form.cleaned_data['question_id']) for article in queryset: article.questions.add(question) modeladmin.message_user(request, '成功修改%d个产品'%n) return None else: modeladmin.message_user(request, '输入有误,请重新提交;%s'%str(form.errors)) pass else: modeladmin.messag_user(request, '未选择任何数据') return None title = '添加问题' action = 'add_questions' form = AddQuestionForm([], initial={'_selected_action': [i.id for i in queryset]}) context = { 'title': title, 'queryset': queryset, 'count': queryset.count(), 'opts': opts, 'app_label': app_label, 'action': action, 'action_checkbox_name': admin.ACTION_CHECKBOX_NAME, 'form': form, } return TemplateResponse(request, 'zhufu/admin/add_questions.html', context=context, current_app=modeladmin.admin_site.name)
def ask_question(request): form = AddQuestionForm(request.POST or None) if request.method == "POST" and form.is_valid(): new_question = Question(title=form.cleaned_data['title'], content=form.cleaned_data['content'], author=request.user) new_question.save() tags_list = form.cleaned_data['tags'].split(" ") tags = [] for tag_title in tags_list: t = Tag.objects.get_or_create(title=tag_title) tags.append(t) for (key, value) in tags: new_question.tags.add(key) new_question.save() return redirect(new_question) return render(request, 'addquestion.html', { 'form': form, })
def add_question(request): context = {} num = IntegerSetting.get('question_number_of_answers').get_value() if request.method == 'POST': form = AddQuestionForm(request.POST) if form.is_valid(): new_question = form.save() return redirect('qpool_home', cat=new_question.category.name) else: form = AddQuestionForm() answers_range = [str(i) for i in xrange(1, num + 1)] categories = [(c.name.capitalize(), c.name) for c in Category.objects.all()] context['categories'] = categories context['answers_range'] = answers_range context['form'] = form return render_to_response('cpanel/add_question.html', context, context_instance=RequestContext(request))
def edit_question(ques_id=False): engine = create_engine(connection_string) # print engine cnx = engine.connect() # x = cnx.execute("select name,count(id) from contact group by name") # cnx.close() # calling update request=== POST 3 {'csrf_token': u'ImY3NDY3MDJkMzQyNWEwM2I0MmNiOTJlYzE5MGRmNGUxMGU3ODc5NDEi.Db3UTA.GOe-w_VNPaEPSX72uXP2rR6bZyQ', # 'name': u'what is your favourate color', 'save_data': u'Save Question', 'option4': u'pink', # 'option2': u'greeen', 'option3': u'blue', 'option1': u'red'} form = AddQuestionForm() search_result = Questions.query.filter_by(id=int(ques_id)).first() all_option = {} for each in search_result.questions_choices: print "eachhhhhhhhh=", each if 'option1' not in all_option: all_option.update({'option1': each.choice}) elif 'option2' not in all_option: all_option.update({'option2': each.choice}) elif 'option3' not in all_option: all_option.update({'option3': each.choice}) elif 'option4' not in all_option: all_option.update({'option4': each.choice}) print("all_optionnnnn=====", all_option) if request.method == 'POST': print "calling update request===", request.method, ques_id, request.form.to_dict( ) form_data = request.form.to_dict() search_result.question = form_data.get('name') search_result.right_choice = form_data.get('right_choice') user_ans = UserQuestionAnswer.query.filter_by( user_id=current_user.id, question_id=int(ques_id)).all() print "user_ans=====", user_ans for each in user_ans: cnx.execute("delete from user_question_answer where id =%s", (each.id)) cnx.close() choices = QuestionsChoices.query.filter_by( question_id=int(ques_id)).all() print "choices=======", choices for each in choices: db.session.delete(each) db.session.commit() #for updating the options option1 = False option2 = False option3 = False option4 = False if str(form_data.get('right_choice')) == 'option1': option1 = True elif str(form_data.get('right_choice')) == 'option2': option2 = True elif str(form_data.get('right_choice')) == 'option3': option3 = True elif str(form_data.get('right_choice')) == 'option4': option4 = True opt1 = QuestionsChoices(choice=str(form_data.get('option1')), is_right=option1, main_question=search_result) opt2 = QuestionsChoices(choice=str(form_data.get('option2')), is_right=option2, main_question=search_result) opt3 = QuestionsChoices(choice=str(form_data.get('option3')), is_right=option3, main_question=search_result) opt4 = QuestionsChoices(choice=str(form_data.get('option4')), is_right=option4, main_question=search_result) db.session.add(opt1) db.session.add(opt2) db.session.add(opt3) db.session.add(opt4) db.session.commit() return redirect(url_for('view_all_question')) return render_template("edit_question.html", form=form, search_result=search_result, all_options=all_option)
def manage_questions(): 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 return render_template('add_questions.html', 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')