Example #1
0
def user(nickname, page=1):
    form = PostnCommentForm()
    if form.validate_on_submit():
        comment = Comment(name=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), post_id=form.p_id.data)
        db.session.add(comment)
        db.session.commit()
        flash('Your comment is now live!')
        return redirect(url_for('index'))
    user = User.query.filter_by(nickname=nickname).first()
    if user == None:
        flash('User %s not found.' % nickname)
        return redirect(url_for('index'))
    posts = user.posts.paginate(page, POSTS_PER_PAGE, False)
    
    return render_template('user.html',
                           user=user,
			   form=form,
                           posts=posts)
Example #2
0
def index(page=1):
    form = PostnCommentForm()
    if form.validate_on_submit():
      if request.form['btn'] == 'Post!':
        post = Post(tags=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), author=g.user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('index'))
      else:    
        comment = Comment(name=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), post_id=form.p_id.data)
        db.session.add(comment)
	#or
	#post = Post.query.get_or_404(form.p_id.data) (retrieving object 'post' which containg respective post.id)
        #comment = Comment(name=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), parent_post=post)
        db.session.commit()
        flash('Your comment is now live!')
        return redirect(url_for('index'))
    posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
    return render_template('index.html',
                           title='Home',
                           form=form,
                           posts=posts)