def ask(request): if request.method == 'POST': form = QuestionForm(request.user.profile, data=request.POST) if form.is_valid(): q = form.save() return redirect(reverse('question', kwargs={'qid': q.id})) return render(request, 'ask.html', tags_and_users)
def ask(request): if request.method == "GET": form = QuestionForm() else: form = QuestionForm(data=request.POST) if form.is_valid(): tags = form.cleaned_data["tag"].split(" ") text = form.cleaned_data["text"] title = form.cleaned_data["title"] question = Question.objects.create(author=request.user, text=text, title=title) tags_list = list() for tag_name in tags: try: tag = Tag.objects.get(name=tag_name) except Tag.DoesNotExist: tag = Tag.objects.create(name=tag_name) tags_list.append(tag) question.tags.set(tags_list) question.save() return redirect(reverse("question", kwargs={"qid": question.pk})) return render(request, "ask.html", { "form": form, })
def question(): global points form_quest = QuestionForm() form_score = ScoreForm() data = read_json_file(json_questions) length = len(data) - 1 while len(questions_made) < number_questions: random_index = generate(length) asked_questions.append(data[random_index]) good_answer = data[random_index]['answer'] if form_quest.validate_on_submit(): my_answer = form_quest.answer.data if answering(my_answer): if my_answer == good_answer: points += 1 redirect('/question') return render_template('question.html', question=data[random_index], form=form_quest) score = (points * 100) / number_questions if form_score.validate_on_submit(): correction = form_score.corrections.data if correction == 'Yes': return render_template('score.html', points=points, num_questions=number_questions, corrections=correction, questions=asked_questions, form=form_score, score=score) return render_template('score.html', points=points, num_questions=number_questions, form=form_score, score=score)
def edit_question(course_id, test_id, question_id): course = Course.query.filter_by(id=course_id).first() test = Test.query.filter_by(id=test_id).first() q = Question.query.filter_by(id=question_id).first() form = QuestionForm() if form.delete.data: db.session.delete(q) db.session.commit() return redirect( url_for('edit_test_view', course_id=course_id, test_id=test_id)) if form.validate_on_submit(): if form.save.data: q.test_id = test_id q.question_type = int(form.question_type.data) q.question_string = repr(form.description.data.encode())[2:-1] q.code_string = repr(form.code_string.data.encode())[2:-1] q.mcq_1 = form.mcq_1.data q.mcq_2 = form.mcq_2.data q.mcq_3 = form.mcq_3.data q.mcq_4 = form.mcq_4.data q.mcq_answer = form.mcq_solution.data q.answer = form.solution.data q.mark_alloc = form.mark_alloc.data db.session.commit() return redirect( url_for('edit_test_view', course_id=course_id, test_id=test_id))
def index(): (a, b) = equation.getAB() question = "{} x {} = ?".format(a, b) form = QuestionForm() if form.validate_on_submit(): flash('Data received: {}'.format(form.answer.data)) equation.tryAnswer(int(form.answer.data)) return redirect('/grade') return render_template('index.html', question=question, form=form)
def create(): form = QuestionForm() if request.method == 'POST' and form.validate_on_submit(): question = Question(subject=form.subject.data, content=form.content.data, create_date=datetime.now(), user=g.user) db.session.add(question) db.session.commit() return redirect(url_for('main.index')) return render_template('question/question_form.html', form=form)
def submitquestion(): questionform = QuestionForm() if questionform.validate_on_submit(): text = questionform.question.data raw = RawQuestion(question_text=text, created_by=current_user.id, user_email=current_user.email) db.session.add(raw) db.session.commit() return redirect(url_for("index"))
def create_question(request): if request.method == "GET": form = QuestionForm() return render_to_response( "app/questions/create.html", {'form': form}, RequestContext(request, {'title': 'Create new question'})) elif request.method == 'POST': user = request.user.userprofile form = QuestionForm(request.POST) question_services = QuestionServices() question = question_services.create_question(form.data['title'], form.data['body'], user) return HttpResponseRedirect('/question/' + str(question.id))
def start_quiz(quiz_id, current_question): form = QuestionForm() current_question = int(current_question) question_list = ( db.session.execute('Select * from Question where quiz_id ==' + str(quiz_id) + " ORDER BY question_id")).fetchall() quiz_name = (db.session.execute('Select * from Quiz where quiz_id ==' + str(quiz_id))).first() temp_list = question_list[current_question][4:8] user_id = str(current_user.get_id()) saved_answers = ( db.session.execute('Select * from results where question_id == ' + str(question_list[current_question][0]) + ' and user_id == ' + user_id)).first() option_list = [] random_int = random.randrange(0, 4) #Load the previous answer if it exists and randomise the other answers order if saved_answers == None: option_list.append((None, "Select Answer")) for counter in range(len(temp_list)): option = temp_list[((random_int + counter) % 4)] option_list.append((option, option)) #Otherwise create a list of answers and randomise order else: option_list.append((saved_answers[4], saved_answers[4])) for counter in range(len(temp_list)): option = temp_list[((random_int + counter) % 4)] if option == saved_answers[4]: continue option_list.append((option, option)) form.set_options(option_list) #If user submits a valid answer load next question if form.validate_on_submit(): submit_answer(quiz_id, question_list, form.answer.data, current_question) if form.submit_answer.data: if current_question + 1 < len(question_list): return redirect( url_for('start_quiz', quiz_id=quiz_id, current_question=current_question + 1)) else: return redirect(url_for('quiz_results', quiz_id=quiz_id)) return render_template('./question_page.html', title=quiz_name[1], question_list=question_list, current_question=current_question, quiz_name=quiz_name[1], form=form, quiz_id=quiz_id)
def admin(): if current_user.admin: form = QuestionForm() if form.validate_on_submit(): question = Questions(question=form.question.data, answers=form.answers.data, correct_answer=form.correct.data, speed=form.speed.data) db.session.add(question) db.session.commit() return redirect(url_for('admin')) return render_template('admin.html', form=form) else: return redirect(url_for('index'))
def index(show): if not current_user.is_authenticated: return redirect(url_for('login')) else: questionform = QuestionForm() questions = db.session.query( ModifiedQuestion, func.count(likes.c.user_id).label('total')).join(likes).group_by( ModifiedQuestion).all() if len(questions) < show: rem_questions = db.session.query(ModifiedQuestion).filter( ~ModifiedQuestion.likes.any()).limit(show - len(questions)).all() else: rem_questions = [] tags = db.session.query(Tag).all() l1 = tags[:len(tags) // 2] if len(tags) % 2 == 1: l1.append(" ") l2 = tags[len(tags) // 2:] zipped = zip(l1, l2) return render_template('index.html', questions=questions, rem_questions=rem_questions, questionform=questionform, user=current_user, zipped=zipped)
def questions(): form = QuestionForm() if form.validate_on_submit(): new_question = Question(question=form.question.data, module=form.modules.data, option_one=form.option_one.data, option_two=form.option_two.data, option_three=form.option_three.data, option_four=form.option_four.data, right_choice=form.right_choice.data) db.session.add(new_question) db.session.commit() flash('Die Frage wurde eingereicht.') flash('Sie muss noch von einem Tutor freigegeben werden.') return redirect(url_for('questions')) return render_template('questions.html', form=form)
def main(): return render_template('main.html', login=LoginForm(), signup=SignupForm(), movie=SelectMovieForm(), question=QuestionForm(), answer=AnswerForm())
def home(): """ This is the home directory """ question_form = QuestionForm() questions = get_questions() context = { 'questions': questions, 'question_form': question_form, } if question_form.validate_on_submit(): set_question(question=question_form.question.data) flash('Se agrego la pregunta') return redirect(url_for('home')) return render_template('home.html', **context)
def question(quest_id): form = QuestionForm() page = request.args.get('page', 1, type=int) post = Post.query.filter_by(id=quest_id).first() if form.validate_on_submit(): answer = Answer(body=form.answer.data, author=current_user, post=post) db.session.add(answer) db.session.commit() flash('Your answer is very good, bro!') return redirect(url_for('question', quest_id=quest_id)) answers = Answer.query.filter_by(quest_id=quest_id).order_by( Answer.timestamp.desc()) return render_template('question.html', title='Question', answers=answers, form=form, post=post)
def ask(request): tags = Tag.objects.get_popular() if request.method == "GET": form = QuestionForm() else: form = QuestionForm(data=request.POST) if form.is_valid(): question = Question.objects.create( author=request.user.profile, title=form.cleaned_data['title'], text=form.cleaned_data["text"]) return redirect(reverse('question', kwargs={'qid': question.pk})) return render(request, 'ask.html', { 'form': form, 'user': request.user, 'tags': tags, })
def ask_question(): form = QuestionForm() if form.validate_on_submit(): data = form.data user = User.query.filter_by(username=session['user']).first() user_id = user.id question = Question( title=data['title'], label=data['cate'], detail=data['detail'], author_id=user_id, addtime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') ) db.session.add(question) db.session.commit() return redirect('/') return render_template('ask.html', title='提问', form=form)
def find_questiontag(): form = QuestionForm() if form.validate_on_submit(): question = form.question.data # print(question) tags = model_predictions.run(question) tags = tags[0].strip('[]') tags_list = tags.split(',') final_tags = [] for tag in tags_list: final_tags.append(tag.strip().strip('\'')) tags_str = ', '.join(final_tags) return render_template('print_questiontag.html', tags=tags_str, question=question) return render_template('find_questiontag.html', form=form)
def question_view(request): """ Shows a next question to the user and manages the answers. Checks for an ongoing exam and picks a question from the list. Answers are checked and user's score is adjusted accordingly. name: exam:question URL: /exam/question/ """ exam_id = request.session.get('exam_id') if exam_id is None: return redirect('exam:list') exam = Exam.objects.get(id=exam_id) question_id = ( request.session['questions'][request.session['current_question']]) question = Question.objects.get(id=question_id) answers = question.answers.all() form = QuestionForm(request.POST or None, question_type=question.type, answer_choices=[(ans.id, ans.text) for ans in answers]) if form.is_valid(): score_change = AnswerScore.get_rating_change( question=question, answer_choices=answers, answer=form.cleaned_data['answer'], user_rating=request.user.userx.rating) request.user.userx.rating += score_change question.rating -= score_change request.user.userx.save() question.save() request.session['current_question'] += 1 if request.session['current_question'] >= exam.num_questions: return redirect('exam:finished') else: return redirect('exam:question') return render( request, 'exam/question.html', { 'exam_name': exam.name, 'question': question, 'form': form, 'question_no': request.session['current_question'], 'num_questions': exam.num_questions })
def index(): form = QuestionForm(meta={'csrf': True}) if request.method == 'POST' and form.validate_on_submit(): email = form.data['username'] for fieldname, value in form.data.items(): if fieldname == 'question_fields': array[fieldname] = value[0] #'question_fields': [{'csrf_token': u'', 'question11': u'Bus', #'question10': u'Edmonton', 'question12': u' Toronto', #'question5': u'sf', 'question4': u'dsafd', 'question7': u'fdsaf', # 'question6': u'dsa', 'question1': u'fdsafs', 'question3': u'dsaf', #'question2': u'fdsaf', 'question9': u'fdsfds', 'question8': u'dsaf'}] save_answer(value[0], email) flash('Questions are submmited by user {}'.format( form.username.data)) return redirect('/') else: form.questions = Question.query.all() return render_template('questionForm.html', title='Task1', form=form)
def apply(request): if request.method == "POST": form = QuestionForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('app:index') else: form = QuestionForm() return render(request, 'app/apply.html', {'form': form})
def addQuestion(): if current_user.tipo_usuario != 1: return redirect(url_for('index')) form = QuestionForm() if form.validate_on_submit(): pregunta = Pregunta(pregunta=form.pregunta.data, year=form.year.data) pregunta.addPregunta() QuestionOption2.newOption(pregunta.id, form.respuesta1.data) QuestionOption2.newOption(pregunta.id, form.respuesta8.data) QuestionOption2.newOption(pregunta.id, form.respuesta7.data) QuestionOption2.newOption(pregunta.id, form.respuesta6.data) QuestionOption2.newOption(pregunta.id, form.respuesta5.data) QuestionOption2.newOption(pregunta.id, form.respuesta4.data) QuestionOption2.newOption(pregunta.id, form.respuesta3.data) QuestionOption2.newOption(pregunta.id, form.respuesta2.data) flash('Pregunta añadida correctamente') return redirect(url_for('adminProfile')) return render_template('addQuestion.html', title='Nueva Pregunta', form=form)
def question_new(request): message = "" if request.method == "POST": form = QuestionForm(request.POST) minNumChoices = 2 maxNumChoices = 4 validNumChoices = int( request.POST["num_choices"]) >= minNumChoices and int( request.POST["num_choices"]) <= maxNumChoices validCorrectChoice = int(request.POST["correct_choice"]) >= 1 and int( request.POST["correct_choice"]) <= int(request.POST["num_choices"]) if not validNumChoices: message = "El número de respuestas debe ser entre " + str( minNumChoices) + " y " + str(maxNumChoices) elif not validCorrectChoice: message = "El número de la respuesta correcta debe estar entre 1 y " + request.POST[ "num_choices"] elif form.is_valid(): question = form.save(commit=False) question.pub_date = datetime.now() question.save() message = "Pregunta añadida!" #return redirect('detail', pk=question_id) #return render(request, 'polls/index.html', {'title':'Respuestas posibles','question': question}) else: form = QuestionForm() return render(request, 'polls/question_new.html', { 'form': form, 'message': message })
def new_question(course_id, test_id): form = QuestionForm() if form.validate_on_submit(): q = Question() q.test_id = test_id q.question_type = int(form.question_type.data) q.question_string = repr(form.description.data.encode())[2:-1] q.code_string = repr(form.code_string.data.encode())[2:-1] q.mcq_1 = form.mcq_1.data q.mcq_2 = form.mcq_2.data q.mcq_3 = form.mcq_3.data q.mcq_4 = form.mcq_4.data q.mcq_answer = form.mcq_solution.data q.answer = form.solution.data q.mark_alloc = form.mark_alloc.data db.session.add(q) db.session.commit() return redirect( url_for('edit_test_view', course_id=course_id, test_id=test_id))
def main(): movie = request.args.get('movie') if movie: url = 'https://www.omdbapi.com/?apikey=227f7057&' + 't=' + movie data = requests.get(url).json() return render_template('main.html', data=data, login=LoginForm(), signup=SignupForm(), question=QuestionForm(), answer=AnswerForm()) else: return redirect(url_for('home'))
def user(username): if not current_user.is_authenticated: return redirect(url_for('index')) question_form = QuestionForm() if request.method == "POST": if question_form.validate_on_submit(): question = Question(asker_id=current_user.id, asker_name=current_user.first_name + " " + current_user.last_name, topic=question_form.topic.data, content=question_form.question.data, category=question_form.category.data) db.session.add(question) db.session.commit() return redirect(url_for('q_a')) else: flash("Dikkatli ol biraz :)") return render_template('user/main.html', form=question_form) if request.method == "GET": return render_template('user/main.html', form=question_form)
def home(request): if request.method == 'POST': form = QuestionForm(request.POST) if form.is_valid(): form.save() return redirect(home) else: form = QuestionForm() questions = Question.objects.all() return render(request, 'home.html', { 'questions': questions, 'form': form, })
def newQuestion(): """Route to generate new questions.""" form = QuestionForm() if form.validate_on_submit(): question = Question( category=form.category.data, question=form.question.data, frontend=form.frontend.data, ans01=form.ans01.data, ans02=form.ans02.data, ans03=form.ans03.data, ans04=form.ans04.data, ans05=form.ans05.data, ans06=form.ans06.data, ans07=form.ans07.data, ans08=form.ans08.data, ans09=form.ans09.data, ans10=form.ans10.data, ans11=form.ans11.data, ans12=form.ans12.data, ans13=form.ans13.data, ans14=form.ans14.data, ans15=form.ans15.data, ans16=form.ans16.data, ans17=form.ans17.data, ans18=form.ans18.data, ans19=form.ans19.data, ans20=form.ans20.data, sort=form.sort.data, ) db.session.add(question) db.session.commit() return redirect('/frage/neu') return render_template('newQuestion.html', title='Neue Frage erfassen', form=form)
def add_question(request): best_members = UserProfile.objects.best_members() popular_tags = Tag.objects.popular_tags() if request.method == 'GET': form = QuestionForm() else: form = QuestionForm(data=request.POST) if form.is_valid(): question = Question.objects.create( author=request.user.userprofile, title=form.cleaned_data['title'], text=form.cleaned_data['text']) tags = form.cleaned_data['tags'] question.tags.set(tags) question.save() return redirect( reverse('one_question', kwargs={'num_quest': question.pk})) return render(request, 'add_question.html', { 'form': form, 'popular_tags': popular_tags, 'best_members': best_members })
def edit_test_view(course_id, test_id): course = Course.query.filter_by(id=course_id).first() test = Test.query.filter_by(id=test_id).first() questions = test.questions form = QuestionForm() course_form = NewCourseForm() return render_template('admin-test-edit.html', course=course, test=test, questions=questions, form=form, course_form=course_form)