def answer(id): # Figure out if answer already exists question = Question.query.get(int(id)) answer = Answer.query.filter_by(question=question).first() form = AnswerForm() # If answer doesn't yet exist if answer is None: if form.validate_on_submit(): answer = Answer(body=form.body.data, author=current_user, question=question) db.session.add(answer) db.session.commit() flash(f'Your response has been recorded') return redirect(url_for('profile', username=current_user.username)) elif request.method == 'GET': form.body.data = answer.body # Validate an existing answer elif form.validate_on_submit(): answer.body = form.body.data db.session.commit() flash(f'Your response has been edited') return redirect(url_for('profile', username=current_user.username)) return render_template('answer.html', form=form, question=question, title='Answer')
def game(): # protect route - if user is not logged in # they will be redirect back to the index page if not 'user' in session: return redirect(url_for('index')) # if a new game has begun start the timer if session['new_game'] == 1: session['new_game'] = 0 session['start_time'] = time.time() # Answer Checking answer_form = AnswerForm() if answer_form.validate_on_submit(): # if the answer is correct - increase the correct score if session['game'][session['index'] - 1]['answer'] == answer_form.answer.data.lower(): session['current_score'] += 1 # otherwise - it must be empty or a wrong answer else: if not answer_form.answer.data: # if answer is empty flash(f'answer field cannot be empty') else: # if answer is wrong flash(f'Incorrect answer! You guessed {answer_form.answer.data}, try again..') # return the same question # prevent index increasing session['index'] -= 1 return redirect(url_for('game')) # If the game is over # Write users scores to json file to save them # then redirect to leaderboard to show results if session['index'] >= 30: set_session_scores() return redirect(url_for('leaderboard')) # increase index pagination to show ther next question logo # continue to track the players time - pass time to jinja session['index'] += 1 session['current_time'] = round(time.time() - session['start_time']) # default - render game.html # if a question is passed - a new question will render return render_template('game.html', endpoint="game", answer_form=answer_form)
def index(): form = AnswerForm() form.generate_form() if form.validate_on_submit(): ans = str(dict(request.form)) print(ans) print(type(ans)) print('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊') current_user.ans = str(dict(request.form)) db.session.commit() return redirect(url_for('success')) return render_template('index.html', title='Answer', form=form)
def check_answer(): form = AnswerForm() if form.validate_on_submit(): print("original " + form.original.data) print("answer " + form.answer.data) if form.original.data == form.answer.data: flash('Correct!') print("Correct input!") else: flash('Wrong!') print("Incorrect input!") return render_template('index.html', form=form)
def create(question_id): form = AnswerForm() question = Question.query.get_or_404(question_id) if form.validate_on_submit(): content = request.form['content'] answer = Answer(content=content, create_date=datetime.now(), user=g.user) question.answer_set.append(answer) db.session.commit() return redirect('{}#answer_{}'.format( url_for('question.detail', question_id=question_id), answer.id)) return render_template('question/question_detail.html', question=question, form=form)
def modify(answer_id): answer = Answer.query.get_or_404(answer_id) if g.user != answer.user: flash('수정권한이 없습니다') return redirect( url_for('question.detail', question_id=answer.question.id)) if request.method == "POST": form = AnswerForm() if form.validate_on_submit(): form.populate_obj(answer) answer.modify_date = datetime.now() # 수정일시 저장 db.session.commit() return redirect('{}#answer_{}'.format( url_for('question.detail', question_id=answer.question.id), answer.id)) else: form = AnswerForm(obj=answer) return render_template('answer/answer_form.html', answer=answer, form=form)
def question(question_id): q = Question.query.filter_by(id=question_id).first() if q.answered == True or q.receiver_id != current_user.id: return redirect(url_for('index')) form = AnswerForm() if form.validate_on_submit(): q.answered = True q.answer = form.answer.data q.answer_timestamp = datetime.utcnow() db.session.add(q) db.session.commit() return (redirect('/user/' + current_user.username) ) # Need to be changed on 'Profile' page return render_template('question.html', question=q, form=form, page='Answer')
def post(post_id): post = Post.query.filter_by(id=post_id).first() if post is None: flash('Post with id: {} not found.'.format(post_id)) return redirect(url_for('index')) form = AnswerForm() if form.validate_on_submit(): answer = Answer(body=form.post.data, participant=current_user, parent=post) db.session.add(answer) db.session.commit() flash('Your answer saved!') return redirect(url_for('post', post_id=post_id)) answers = Answer.query.filter_by(post_id=post_id) return render_template('post.html', title='Post', form=form, post=post, answers=answers)
def question(): if CLOSE_WEBSITE: return redirect(url_for('index')) else: n_question = current_user.number_answers() form = AnswerForm() question_id = session['question_id'] current_question = Question.query.get(question_id) if form.validate_on_submit(): # flash('Your answered:'+str(form.choice.data)+' to question :'+str(current_question.id)+' known '+str(form.known.data)) if Question.query.get(question_id).ongoing_user == current_user.id and datetime.utcnow()-current_question.ongoing_since > LOCK_TIME: flash('You took too long to answer (more than '+str(int(LOCK_TIME.total_seconds()//60))+' minutes)! Here is a new example.') current_question.ongoing_since = MIN_DATE current_question.ongoing_user = -1 db.session.commit() else: aborted = current_question.answer(form.choice.data,current_user,recognised=form.known.data,difficulty=form.difficulty.data) if aborted: flash('Something went wrong! Here is a new example.') current_question.ongoing_since = MIN_DATE current_question.ongoing_user = -1 db.session.commit() session['question_id'] = current_user.next_question() Question.query.get(session['question_id']).ongoing_since = datetime.utcnow() Question.query.get(session['question_id']).ongoing_user = current_user.id db.session.commit() return redirect(url_for('question')) # flash(str([current_question.id, question_id])) # import datetime as dt # all_questions = Question.query.filter(Question.ongoing_since>datetime.utcnow()-LOCK_TIME).all() # print "current_user",current_user.id # for question in all_questions: # print question, question.ongoing_since, question.ongoing_user return render_template('question.html', number=n_question+1,filepaths = current_question.get_filepaths(), form=form, debug=app.debug)
def assessment(): assessment: Assessment = Assessment.get_new_assessment(current_user.id) if assessment is None: flash("No more assessments!") return redirect(url_for('index' )) userAssessment: UserAssessment = UserAssessment.query.filter_by(user_id=current_user.id, assessment_id=assessment.id).first() answer_form = AnswerForm() if answer_form.validate_on_submit(): if answer_form.answer.data == assessment.answer: flash("Correct!") userAssessment.completed = True userAssessment.correct = True else: flash("Incorrect!") userAssessment.completed = True userAssessment.correct = False db.session.commit() return redirect(url_for('assessment')) return render_template('AssessmentPage.html', assessment=assessment, answer_form=answer_form)
def question(id): if current_user.is_anony(): return redirect(url_for('question_for_anonymous', id=id)) ques = Items.query.filter(Items.id == id, Items.deleted == 0).first() ansform = AnswerForm() if ansform.validate_on_submit(): allans = Answer.query.filter_by(belongtoques=id).all() for a in allans: p = Items.query.filter_by(ans_id=a.id).first() if p.author_id == current_user.id: return render_template('not.html', text=u'您不能回答同一个问题两次') ans = Answer(belongtoques=id) db.session.add(ans) db.session.commit() a = Answer.query.order_by( Answer.id.desc()).filter_by(belongtoques=id).first() e = Items(title_or_ans=ansform.answer.data, author_id=current_user.id, types=2, ans_id=a.id) db.session.add(e) db.session.add(a) db.session.commit() ansnum = Answer.query.filter(Answer.belongtoques == id).count() allans = Answer.query.filter(Answer.belongtoques == id).all() for a in allans: if Items.query.filter_by(ans_id=a.id).first().deleted == 1: ansnum = ansnum - 1 ans_ = Answer.query.filter(Answer.belongtoques == id).all() ans_id_ = [] ansdo = [] for a in ans_: ans_id_.append(a.id) ans = Items.query.filter(Items.types == 2, Items.deleted == 0).filter( Items.ans_id.in_(ans_id_)).all() ans_id = [] for a in ans: ans_id.append(a.id) i_do_to_the_ans = Upvote.query.filter( Upvote.upvoter == current_user.id).filter( Upvote.answerid.in_(ans_id)).all() for i in i_do_to_the_ans: if Items.query.get(i.answerid).deleted == 0: ansdo.append((i.answerid, i.descr)) pt = PostTag.query.filter_by(postid=id).all() the_ques_tags_id = [] related = [] for p in pt: the_ques_tags_id.append(p.tagid) the_ques_tags = Items.query.filter(Items.id.in_(the_ques_tags_id)).all() rposts_rcds = PostTag.query.filter( PostTag.tagid.in_(the_ques_tags_id)).all() for r in rposts_rcds: related.append(r.postid) related = list(set(related)) if len(related) != 0: related.remove(id) rposts = Items.query.filter(Items.id.in_(related), Items.deleted == 0).all() else: rposts = [] thank = [] thank_ = Thank.query.filter(Thank.personid == current_user.id).filter( Thank.postid.in_(ans_id)).all() for t in thank_: thank.append(t.postid) return render_template('question.html', ques = ques, form = ansform, Permission = Permission, \ ans = ans, ansdo = ansdo, tags= the_ques_tags, ansnum = ansnum, rposts = rposts, thank = thank)