Exemplo n.º 1
0
def user(username):
    page = request.args.get('page', 1, type=int)
    user = User.query.filter_by(username=username).first_or_404()
    if current_user.username == username:
        return redirect(
            url_for('index'))  # Need to be changed on 'Profile' page
    form = AskForm()
    if form.validate_on_submit():
        q = Question(
            body=form.body.data,
            anonymous=form.is_anonymous.data,
            sender_id=current_user.id,
            receiver_id=User.query.filter_by(username=username).first().id,
            answered=False)
        db.session.add(q)
        db.session.commit()
        flash('Your question was sent!')
        return (redirect('/user/' + username))
    questions = user.received.filter_by(answered=True).order_by(-Question.answer_timestamp)\
                .paginate(page, app.config['QUESTIONS_PER_PAGE'], False)
    next_url = url_for('user', username=username, page=questions.next_num) \
        if questions.has_next else None
    prev_url = url_for('user', username=username, page=questions.prev_num) \
        if questions.has_prev else None
    return render_template('user.html',
                           user=user,
                           questions=questions.items,
                           page=user.username,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url)
Exemplo n.º 2
0
def ask():
    getUser()
    form = AskForm()
    if session['qCount'] >= 5:
        return redirect('/')
    if form.validate_on_submit():
        q = Question(id=str(uuid.uuid4()),body=request.form['question'],optionOne=request.form['answerOne'],optionTwo=request.form['answerTwo'])
        q.author=db.session.query(User).filter_by(ip=session['user']).first()
        session['qCount']+=1
        db.session.add(q)
        db.session.commit()
        return redirect('/')
    return render_template('ask.html',form=form)