Beispiel #1
0
def question_detail_page(qid, subject):
    """ Show detail on question and all answers / if Post, handle answer """
    form = AnswerForm()

    question = Question.query.get_or_404(qid)
    answers = db.session.query(Answer).filter_by(questionID=question.id).all()

    if form.validate_on_submit():
        # handle answer form
        answer = Answer(
            content=form.answer.data,
            authorID=g.user.id,
            questionID=question.id,
            # upvotes=1
        )
        db.session.add(answer)
        db.session.commit()

        question.answered = True
        db.session.add(question)
        db.session.commit()

        return redirect(f'/q/{subject}/{qid}')

    return render_template('board/question-detail.html',
                           question=question,
                           answers=answers,
                           form=form)
Beispiel #2
0
def answer(questionid):
    form = AnswerForm()
    question = Question.objects.get_or_404(id=questionid)
    if form.validate_on_submit():
        question.answer = form.answer.data
        question.save()
        flash("Your answer has been saved.")
        return redirect(url_for("index"))
    return render_template("answer.html", form=form, question=question)
Beispiel #3
0
def answer(questionid):
    form = AnswerForm()
    question = Question.objects.get_or_404(id=questionid)
    if form.validate_on_submit():
        question.answer = form.answer.data
        question.save()
        flash("Your answer has been saved.")
        return redirect(url_for("index"))
    return render_template("answer.html", form=form, question=question)
Beispiel #4
0
def answers():
    form = AnswerForm()
    if form.validate_on_submit():
        answer = Answer(body=form.answer.data, question_id=form.question_id.data)
        db.session.add(answer)
        db.session.commit()
        flash('Answer was added')
        return redirect(url_for('index'))
    answers_all = Answer.query.all()
    return render_template('answers.html', title='Answers', form=form, answers=answers_all)
Beispiel #5
0
def answer_question():
    form = AnswerForm()

    if form.validate_on_submit():
        id = form.question_id.data
        print('This is the question_id: ', id)
        answer = Answer(answer=form.answer.data, question_id=id)
        db.session.add(answer)
        db.session.commit()

        return redirect(url_for('index'))

    return render_template('answer.html')
Beispiel #6
0
def post(id):
    form = AnswerForm()
    post_=Post.query.filter_by(id=id).first()
    answers=post_.answers.all()
    if request.method == 'POST':
        if form.validate_on_submit():
            answer = Answer(body = form.answer.data, timestamp = datetime.utcnow(), author = g.user, question=post_)
            db.session.add(answer)
            db.session.commit()
            flash('Your post is now live!')
            return redirect(url_for('post', id=post_.id))
        #return redirect(url_for('post', post=post, answers=answers, form=form))
    
    return render_template('post.html', post=post_, answers=answers, form=form)
Beispiel #7
0
def game(id, attempt):
    form = AnswerForm()
    week = session['week']
    fix_list = init_game(week)
    name = session['name']
    score = session['score']
    if form.validate_on_submit():
        plr_answer = form.answer.data
        currId = id
        correct_result = get_correct_result(currId, fix_list)
        if id <= 9:
            if attempt == 1:
                if plr_answer != correct_result:
                    flash(f'Wrong answer {name}, you have one more attempt',
                          'dark')
                    return redirect(url_for('game', id=id, attempt=2))
                else:
                    flash(f'You are correct {name}', 'success')
                    session['score'] = session['score'] + plr_answer
                    return redirect(url_for('game', id=id + 1, attempt=1))
            elif attempt == 2:
                if plr_answer != correct_result:
                    flash(f'Wrong answer {name}', 'dark')
                    return redirect(url_for('game', id=id + 1, attempt=1))
                else:
                    flash(f'You are correct {name}', 'success')
                    session['score'] = session['score'] + 1
                    return redirect(url_for('game', id=id + 1, attempt=1))
        elif id == 10:
            if attempt == 1:
                if plr_answer != correct_result:
                    flash(f'Wrong answer {name}, you have one more attempt',
                          'dark')
                    return redirect(url_for('game', id=10, attempt=2))
                else:
                    score = score + plr_answer
                    return redirect(url_for('winner'))
            if attempt == 2:
                if plr_answer != correct_result:
                    return redirect(url_for('winner'))
                else:
                    session['score'] = session['score'] + 1
                    return redirect(url_for('winner'))
    return render_template('game.html',
                           form=form,
                           id=id,
                           fix_list=fix_list,
                           name=name,
                           week=week)
Beispiel #8
0
def detail(id):
    question = Question.query.get(id)
    form = AnswerForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=session['email']).first()
        answer = Answer(form.answer.data, question, user)
        db.session.add(answer)
        db.session.commit()
        flash('Successfully added answer ')
        return redirect(url_for('detail', id=question.id))

    if question is None:
        flash('Q not found')
        return redirect(url_for('index'))
    return render_template('question.html', question=question, form=form)
Beispiel #9
0
def checkanswer():
    """Check user's answer for reseting password."""

    form = AnswerForm()
    username = session['username']

    if form.validate_on_submit():
        if Users.checkanswer(username, form.answer.data):
            return redirect('/resetpw')

        flash("Wrong answer, please try again.")

    question = session['question']
    return render_template('users/checkanswer.html',
                           form=form,
                           question=question)
Beispiel #10
0
def interpreted():
    global col_name, theme_name, data

    if request.method == 'GET':
        col_name, theme_name, data = getData1()

    form = AnswerForm()

    if form.validate_on_submit():
        if form.submit.data:
            answer = 1
        else:
            answer = 0
        putData(col_name, theme_name, 1, answer)
        message = 'Спасибо за помощь в исследовании :)'
        return redirect(url_for('.extra_word', message=message))

    message = request.args['message']
    return render_template('interpreted.html', title='Research', form=form, data=data, message=message)
Beispiel #11
0
def question_detail(question_id, page):
    question = Question.query.filter_by \
        (id=question_id).first()
    answers = Answer.query.filter_by \
        (question_id=question_id).paginate(page, 5, False)
    form = AnswerForm()
    if form.validate_on_submit():
        answer = Answer(text=form.answer.data, user_id=g.user.id,
                        timestamp=datetime.datetime.utcnow(),
                        votes=0,
                        question_id=question_id)
        db.session.add(answer)
        db.session.commit()
        flash('New answer to question has been posted', 'success')
        return redirect(request.referrer)
    return render_template('question_detail.html',
                           title = 'question',
                           question = question,
                           answers=answers,
                           form = form)
def question_review(id):
    # Get the question
    question = Question.query.get_or_404(id)

    # All answers in this question
    answers = Answer.query.filter_by(question_id=id)

    author_question = User.query.get(question.user_id)

    # author_answer = User.query.get(answers.user_id)

    # was form posted?
    if request.method == 'POST':
        form = AnswerForm(request.form)

        if form.validate_on_submit():
            answer = Answer(
                text=request.form['text'],
                date=datetime.now(),
                question_id=id,
                user_id=current_user.id)

            # Answer save in db
            db.session.add(answer)
            db.session.commit()

            return redirect(url_for('question_review', id=id))

        else:
            # Show errors in form
            flash_errors(form)

    else:
        form = AnswerForm()

    return render_template('question_review.html',
        form = form,
        question = question,
        answers = answers,
        author_question=author_question,
        users = User)
Beispiel #13
0
def details(id):
    question = Question.query.filter_by(id=id).first()
    if question == None:
        flash(gettext('Question with ID %(id)s not found.', id = id), 'warning')
        return redirect(url_for('questions'))
    answers = Answer.query.filter_by(question_id=id).all()
    vote = request.args.get('vote')
    if vote != None:
        answer = Answer.query.filter_by(id=vote).first()
        answer.votes += 1
        db.session.commit()
    form = AnswerForm(request.form)
    if form.validate_on_submit():
        answer = Answer(text=form.text.data,
                        date=datetime.utcnow(),
                        owner=question)
        db.session.add(answer)
        db.session.commit()
        flash(gettext('Your answer succesfully posted'), 'success')
        return redirect(url_for('questions'))
    return render_template("question.html", question=question, form=form, answers=answers)
Beispiel #14
0
def question(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    answers = Answer.query.filter_by(questionId=question_id)

    if form.validate_on_submit():
        new_answer = Answer(questionId=question_id,
                            datetime=datetime.now(),
                            authorId=question.author.id,
                            vote=0,
                            answer=form.answer.data,
                            authorName=current_user.username)
        db.session.add(new_answer)
        db.session.commit()
        flash('Your answer has been submitted!', 'success')
        return redirect(url_for('question', question_id=question_id))
    return render_template('question.html',
                           form=form,
                           title=question.question,
                           question=question,
                           answers=answers)
Beispiel #15
0
def answer(id):
    post = Post.query.get(id)
    if post == None:
        flash("Post not found")
        return redirect(url_for('index'))
    if post.author.id != g.user.id:
        flash("You cannot edit this post")
        return redirect(url_for('index'))
    form = AnswerForm()
    if form.validate_on_submit():
        post.answered = True
        post.answer = form.answer.data
        post.answer_time = datetime.utcnow()
        db.session.add(post)
        db.session.commit()
        flash(gettext('Your answer was added!'))
        return redirect(url_for('post', id = post.id))
    form.answer.data = post.answer
    return render_template('answer_post.html',
        post = post,
        form = form)
def question_review(id):
    # Get the question
    question = Question.query.get_or_404(id)

    # All answers in this question
    answers = Answer.query.filter_by(question_id=id)

    author_question = User.query.get(question.user_id)

    # author_answer = User.query.get(answers.user_id)

    # was form posted?
    if request.method == 'POST':
        form = AnswerForm(request.form)

        if form.validate_on_submit():
            answer = Answer(text=request.form['text'],
                            date=datetime.now(),
                            question_id=id,
                            user_id=current_user.id)

            # Answer save in db
            db.session.add(answer)
            db.session.commit()

            return redirect(url_for('question_review', id=id))

        else:
            # Show errors in form
            flash_errors(form)

    else:
        form = AnswerForm()

    return render_template('question_review.html',
                           form=form,
                           question=question,
                           answers=answers,
                           author_question=author_question,
                           users=User)
Beispiel #17
0
def interpreted():
    global col_name, theme_name, data

    if request.method == 'GET':
        col_name, theme_name, data = getData1()

    form = AnswerForm()

    if form.validate_on_submit():
        if form.submit.data:
            answer = 1
        else:
            answer = 0
        putData(col_name, theme_name, 1, answer)
        message = 'Спасибо за помощь в исследовании :)'
        return redirect(url_for('.extra_word', message=message))

    message = request.args['message']
    return render_template('interpreted.html',
                           title='Research',
                           form=form,
                           data=data,
                           message=message)
Beispiel #18
0
def question_take():
    if session.get('user_id') is None:
        flash("未登录")
        return redirect(url_for('login'))
    form = AnswerForm()
    if form.validate_on_submit():
        que = Questions.query.filter_by(id=1).first()
        result = Results.query.filter_by(question=que.question).first()
        num = form.select.data
        if num == '1':
            result.count_select1 = result.count_select1 + 1
        if num == '2':
            result.count_select2 = result.count_select2 + 1
        if num == '3':
            result.count_select3 = result.count_select3 + 1
        if num == '4':
            result.count_select4 = result.count_select4 + 1
        db.session.add(result)
        cou = Coupon.query.filter_by(active_state=True).first()
        cou.user_name = session.get('user_id')
        cou.active_state = False
        db.session.add(cou)
        return redirect(url_for('coupon.coupon_detail'))
    return render_template('questions.html', form=form, user=True)
Beispiel #19
0
def new_answer(question_id):
    """Create a new answer corresponding to a question."""
    question = Question.get_by_id(question_id)
    answerform = AnswerForm()
    if request.method == "POST" and answerform.validate_on_submit():
        answer = Answer(
            content=answerform.content.data,
            added_by=users.get_current_user(),
            location=get_location(answerform.location.data),
            for_question=question,
            parent=question.key
        )
        try:
            answer.put()
            notify_new_answer(answer)
            answer_id = answer.key.id()

            flash(u'Answer %s successfully saved.' % answer_id, 'success')
            return redirect(url_for('answers_for_question', question_id=question_id))
        except CapabilityDisabledError:
            flash(u'App Engine Datastore is currently in read-only mode.', 'info')
            return redirect(url_for('answers_for_question', question_id=question_id))

    return render_template('new_answer.html', question=question, form=answerform)
Beispiel #20
0
def answer_question(question_id):
    answer_form = AnswerForm()
    if answer_form.validate_on_submit():
        if not is_answered(current_user.username, question_id) or not get_question(question_id).one_attempt:
            write_answer(current_user.username, question_id, answer_form.answer_text.data, answer_form.user_score.data)
    return redirect(url_for('questions'))
Beispiel #21
0
def multiplayer(id, p_num, attempt):
    name_1 = session['name_1']
    name_2 = session['name_2']
    names = [name_1, name_2]
    form = AnswerForm()
    week = session['week']
    fix_list = init_game(week)
    if form.validate_on_submit():
        plr_answer = form.answer.data
        count = 2
        currId = id
        correct_result = get_correct_result(currId, fix_list)
        fixtures = 10
        """ play game only for count number of players """
        if p_num <= count:
            """ last fixture, last player """
            if id == fixtures and p_num == count:
                if attempt == 1:
                    if plr_answer != correct_result:
                        flash(
                            f'Wrong answer {names[p_num-1]}, you have one more attempt',
                            'dark')
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num,
                                    attempt=2))
                    else:
                        session['score_b'] = session['score_b'] + plr_answer
                        return redirect(url_for('winnermult'))
                elif attempt == 2:
                    if plr_answer != correct_result:
                        return redirect(url_for('winnermult'))
                    else:
                        session['score_b'] = session['score_b'] + 1
                        return redirect(url_for('winnermult'))
            # if last player in a Question
            elif p_num == count:
                if attempt == 1:
                    if plr_answer != correct_result:
                        flash(
                            f'Wrong answer {names[p_num-1]}, you have one more attempt',
                            'dark')
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num,
                                    attempt=2))
                    else:
                        flash(f'You are correct {names[p_num-1]}', 'success')
                        session['score_b'] = session['score_b'] + plr_answer
                        return redirect(
                            url_for('multiplayer',
                                    id=id + 1,
                                    p_num=1,
                                    attempt=1))
                elif attempt == 2:
                    if plr_answer != correct_result:
                        flash(f'Wrong answer {names[p_num-1]}', 'dark')
                        return redirect(
                            url_for('multiplayer',
                                    id=id + 1,
                                    p_num=1,
                                    attempt=1))
                    else:
                        flash(f'You are correct {names[p_num-1]}', 'success')
                        session['score_b'] = session['score_b'] + 1
                        return redirect(
                            url_for('multiplayer',
                                    id=id + 1,
                                    p_num=1,
                                    attempt=1))
            # fixture 1 to 9 all players excluding last player
            elif p_num < count:
                if attempt == 1:
                    if plr_answer != correct_result:
                        flash(
                            f'Wrong answer {names[p_num-1]}, you have one more attempt',
                            'dark')
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num,
                                    attempt=2))
                    else:
                        flash(f'You are correct {names[p_num-1]}', 'success')
                        session['score_a'] = session['score_a'] + plr_answer
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num + 1,
                                    attempt=1))
                elif attempt == 2:
                    if plr_answer != correct_result:
                        flash(f'Wrong answer {names[p_num-1]}', 'dark')
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num + 1,
                                    attempt=1))
                    else:
                        flash(f'You are correct {names[p_num-1]}', 'success')
                        session['score_a'] = session['score_a'] + 1
                        return redirect(
                            url_for('multiplayer',
                                    id=id,
                                    p_num=p_num + 1,
                                    attempt=1))
    return render_template('multiplayer.html',
                           form=form,
                           id=id,
                           fix_list=fix_list,
                           p_num=p_num,
                           names=names,
                           week=week)
Beispiel #22
0
def answers():
    if not g.user_id:
        flash(message="You must Login or Signup")
        return redirect(url_for('login'))
    elif request.method == 'GET':
        flash(message="DO the question to get the answer")
        return redirect(url_for('subject_list'))
    else:
        form = AnswerForm()
        verify_user_login = Questions.query.filter_by(
            question_id=form.question_id.data).first()
        current_user = verify_user_login.user_id
        if form.validate_on_submit() and current_user == session.get(
                'user_id', None):
            user_answer = form.answer.data
            user_answer = user_answer.lower()
            subject = form.subject_name.data
            question_id = form.question_id.data
            user_id = session.get('user_id', None)
            get_user_name = User.query.filter_by(id=user_id).first()
            user_name = get_user_name.user_second_name
            #print(question_id)
            update_questions = Marks.query.filter_by(user_id=user_id).first()
            if user_answer:
                update_questions.questions_done += 1
                db.session.commit()
                question_done = update_questions.questions_done
                add_user_answer = Questions.query.filter_by(
                    question_id=question_id).first()
                add_user_answer.user_answers = user_answer
                db.session.commit()
                language = add_user_answer.question
                language = ast.literal_eval(language)
                user_answer = add_user_answer.user_answers
                for item in language:
                    answer = item[u'answer']
                    if answer == user_answer:
                        update_marks = Marks.query.filter_by(
                            user_id=user_id).first()
                        update_marks.points += 1
                        db.session.commit()
                        score = update_marks.points
                    else:
                        update_marks = Marks.query.filter_by(
                            user_id=user_id).first()
                        score = update_marks.points
            flash(message="Answer for {} question ".format(subject))
            return render_template('answers.html',
                                   language=language,
                                   user_answer=user_answer,
                                   question_id=question_id,
                                   question_done=question_done,
                                   score=score,
                                   user_name=user_name)
        elif not form.validate_on_submit() and current_user == session.get(
                'user_id', None):
            form = AnswerForm()
            question_id = form.question_id.data
            get_question = Questions.query.filter_by(
                question_id=question_id).first()
            language = get_question.question
            language = ast.literal_eval(language)
            error = "Your answer must be one of this a,b,c or d"
            flash(message="{}".format(error))
            return render_template('questions.html',
                                   language=language,
                                   question_id=question_id,
                                   form=form)
        else:
            flash(message=
                  "you must login again becouse we have dropped your session")
            return redirect(url_for('login'))