def comment(post_id): if request.method == "POST": form = CommentForm(request.form) if form.validate_on_submit(): comment = Comment(post_id, session['username'], form.body.data) db.session.add(comment) db.session.flush() db.session.commit() if comment.id: return " Success! Comment added" else: return "Insert failed." else: print("not valid") print(form.body) return "failed validation" else: post = Post.query.filter_by(id=post_id).first() form = CommentForm() if post: #return render_template('/blog/comment.html', form=form, post=post, username=username, action="new") return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Post not found" return "exit comment function"
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id) if form.validate_on_submit(): body = form.comment.data post_id = post.id comment = Comment(body, post_id) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', post=post, form=form, comments=comments, slug=slug)
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() form = CommentForm() if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() body = form.body.data comment = Comment(author, post, body) db.session.add(comment) db.session.commit() flash("Comment posted") return redirect(url_for('article', slug=post.slug)) return render_template('blog/article.html', post=post, form=form)
def comment(post_id): form = CommentForm() post = Post.query.filter_by(id=post_id).first_or_404() if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() body = form.body.data comment = Comment(post, author, body) db.session.add(comment) db.session.flush() db.session.commit() flash("Blog post commented") return redirect(url_for('article', slug=post.slug)) return render_template('blog/comment.html', form=form, post=post)
def article(slug): form = CommentForm() if form.validate_on_submit(): post = Post.query.filter_by(slug=slug).first_or_404() author = Author.query.filter_by(username=session['username']).first() body = form.body.data timestamp = datetime.utcnow() comment = Comment(post, author, body, timestamp) db.session.add(comment) db.session.commit() flash("Comment posted.") return redirect(url_for('article', slug=post.slug)) post = Post.query.filter_by(slug=slug).first_or_404() return render_template('blog/article.html', form=form, post=post)
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() form = CommentForm() if form.validate_on_submit(): comment_author = form.comment_author.data comment_body = form.comment_body.data comment = Comment(comment_author, comment_body) form.populate_obj(comment) db.session.add(comment) db.session.commit() post.comment_id = comment.id post.comments = Comment.query.filter_by(id=post.comment_id).first() db.session.add(post) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', form=form, post=post)
def article(post_id): post = Post.query.filter_by(id=post_id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment_author = form.comment_author.data comment_body = form.comment_body.data post_id = post.id comment = Comment(post_id, comment_author, comment_body) db.session.add(comment) # db.session.commit() post.comments.append(comment) db.session.add(post) db.session.commit() return redirect(url_for('article', post_id=post.id)) comments=Comment.query.all() return render_template('blog/article.html', form=form, post=post, comments=comments)
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() if form.validate_on_submit(): author_name = session.get('username') if not author_name: author_name = 'Anonymous' comment = Comment(form.text.data, author_name, post) db.session.add(comment) db.session.flush() db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', post=post, form=form)
def article(slug): form = CommentForm() blog = Blog.query.first() post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id).all() if form.validate_on_submit(): if session['username']: user = User.query.filter_by(username=session['username']).first() comment = Comment( post.id, user, form.content.data ) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', slug=slug, blog=blog, post=post, form=form, comments=comments)
def comment(post_id): if request.method == "POST": form = CommentForm(request.form) if form.validate_on_submit(): comment = Comment(post_id, session['username'], form.body.data) #, None) db.session.add(comment) db.session.flush() db.session.commit() if comment.id: return redirect(url_for('getslug', id=post_id)) else: flash("Insert failed.") post = Post.query.filter_by(id=post_id).first() if post: return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Insert failed." else: print("not valid") flash("failed validation") post = Post.query.filter_by(id=post_id).first() if post: return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "failed validation" else: post = Post.query.filter_by(id=post_id).first() form = CommentForm() if post: #return render_template('/blog/comment.html', form=form, post=post, username=username, action="new") return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Post not found"
def article(slug): user_logged = False if session.get('username'): print('Yes, there is a user logged') user_logged = True form = CommentForm() if form.validate_on_submit(): blog = Blog.query.first() post = Post.query.filter_by(slug=slug).first_or_404() author = Author.query.filter_by(username=session['username']).first() username = session['username'] body = form.body.data comment = Comment(blog, post, author, username, body) db.session.add(comment) db.session.commit() flash("Comment posted.") return redirect(url_for('article', slug = post.slug)) post = Post.query.filter_by(slug = slug).first_or_404() return render_template('blog/article.html', form=form, post=post)
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() # either find the post or return 404 comments = Comment.query.filter_by(post_id=post.id, live=True).order_by(Comment.comment_date.desc()) # check if comments exist comment_check = Comment.query.first() if not comment_check: is_comment = 0 else: is_comment = 1 # posting a comment error = None if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() comment_body = form.comment_body.data comment = Comment(comment_body, post.id, author.id) db.session.add(comment) db.session.commit() flash("Comment posted!") return render_template('blog/article.html', post=post, comments=comments, form=form, is_comment=is_comment)
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id) form = CommentForm() if request.method == 'POST': if session.get('username'): if form.validate_on_submit(): blog = Blog.query.first() author = Author.query.filter_by( username=session['username']).first() post = Post.query.filter_by(slug=slug).first_or_404() body = form.body.data comment = Comment(author, blog, post, body) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) else: flash('Please fill out required fields') else: flash('You must be logged in to comment.') return render_template('blog/article.html', post=post, comments=comments, form=form)