def show(id): if not g.db.doc_exist(id): abort(404) errors = [] task = Task.get(id) form = CommentForm(request.form) comments = list(Comment.view('comments/by_task_id', key=id)) comments = sorted(comments, key=lambda x: x.date) if request.method == 'POST' and form.validate(): new_comment = Comment() new_comment.author = session['username'] new_comment.text = form.text.data new_comment.date = datetime.datetime.utcnow() new_comment.task_id = id new_comment.save() flash('Comment was successfully added') return redirect(url_for('tasks.show', id=id)) fpath = os.path.join(UPLOADED_FILES, id) files = None if os.path.exists(fpath): files = os.listdir(fpath) errors.extend(format_form_errors(form.errors.items())) return render_template('task_show.html', \ task = task, comments = comments, form = form, errors = errors, \ files = files)
def post_detail(slug): try: post = Post.objects(slug=slug).first() tags = post.tags if post.tags else [] if post.picture and post.pic_name: filename = post.pic_name else: filename = None try: user = post.user.fetch() user_id = str(user.id) except Exception: user_id = None form = CommentForm() if current_user.is_authenticated: comment_author = User.objects(id=current_user.get_id()).first() else: comment_author = None if request.method == 'POST': comment = request.form.get('comment') if form.validate_on_submit: comment = Comment(body=comment, author=comment_author) post.comments.append(comment) post.save() return render_template('posts/post_detail.html', post=post, tags=tags, picture=filename, post_author=user_id, \ form=form, comment_author=comment_author, comments=post.comments[::-1]) except Exception: return render_template('404.html'), 404