Beispiel #1
0
def post_detail(request, year, month, day, post):
    '''
    post detail view
    '''
    post = get_object_or_404(Post,
                            slug = post,
                            status = 'published',
                            publish__year = year,
                            publish__month = month,
                            publish__day = day
                            )
    #list of active comments for this post
    comments = post.comments.filter(active = True)

    if request.method == "POST":
        #A comments was posted
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet
            new_comment = comment_form.save(commit = False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        #get empty form
        comment_form = CommentForm()

    # List of similar posts
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]

    return render(request, "myblog/post/detail.html",
        {'post':post, 'comments': comments, 'comment_form':comment_form, 'similar_posts':similar_posts})
Beispiel #2
0
def post(post_id):
    """View function for post page"""

    #Form object:'Comment'
    form = CommentForm()
    #form.validator_on_submit() will be true and return the
    #data object to form instance form user enter.
    #when the HTTP request is POST
    if form.validate_on_submit():
        new_comment = Comment(id=str(uuid4()), name=form.name.data)
        new_comment.text = form.text.data
        new_comment.date = datetime.datetime.now()
        new_comment.post_id = post_id
        db.session.add(new_comment)
        db.session.commit()

    post = db.session.query(Post).get_or_404(post_id)
    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    recent, top_tags = sidebar_data()

    return render_template('post.html',
                           post=post,
                           tags=tags,
                           comments=comments,
                           form=form,
                           recent=recent,
                           top_tags=top_tags)
Beispiel #3
0
 def post(self, request):
     comment_form = CommentForm(request.POST)
     if comment_form.is_valid():
         comment_form.save()
         return HttpResponse('{"status": "success"}', content_type='application/json')
     else:
         return HttpResponse('{"status": "fail"}', content_type='application/json')
Beispiel #4
0
 def post(self, slug, id):
   form = CommentForm(request.form)
   post = Post.objects.get_or_404(id=id)
   if form.validate():
     comment = Comment(body=form.body.data, author=current_user.id)
     post.comments.append(comment)
     post.save()
   return redirect(post.get_absolute_url())
Beispiel #5
0
def view_post(request, slug):
    post = get_object_or_404(Post, slug=slug)
    form = CommentForm(request.POST or None)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.post = post
        comment.save()
        return redirect(request.path)
    return render_to_response('blog_post.html', { 'post': post, 'form': form, }, context_instance=RequestContext(request))
Beispiel #6
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid:
            comment = form.save(commit=False)
            comment.Post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
        return render(request, 'blog/comment_form.html', {'form': form})
Beispiel #7
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['POST_PER_PAGE']
    pagination = Comment.query.with_parent(post).order_by(Comment.timestamp.asc()).paginate(
        page, per_page=per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        from_admin = True
    else:
        form = CommentForm()
        from_admin = False

    if form.validate_on_submit():
        author = form.author.data
        body = form.body.data
        comment = Comment(
            author=author, body=body,
            from_admin=from_admin, post=post
        )
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash('Comment publish.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.')
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
Beispiel #8
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)

    form = CommentForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(
                author=form.cleaned_data["author"],
                body=form.cleaned_data["body"],
                post=post,
            )
            comment.save()

    context = {"post": post, "comments": comments, "form": form}
    return render(request, "blog_detail.html", context)
Beispiel #9
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.private and not current_user.is_authenticated:
        flash("你没有权限访问该文章!", "warning")
        return redirect(url_for(".index"))
    page = request.args.get("page", 1, type=int)
    per_page = current_app.config.get("BLOG_COMMENT_PER_PAGE", 15)
    pagination = (
        Comment.query.with_parent(post)
        .filter_by(reviewed=True)
        .order_by(Comment.timestamp.asc())
        .paginate(page, per_page=per_page)
    )
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config["BLOG_EMAIL"]
        form.site.data = url_for(".index")
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(
            author=author,
            email=email,
            site=site,
            body=body,
            from_admin=from_admin,
            post=post,
            reviewed=reviewed,
        )
        replied_id = request.args.get("reply")
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)

        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash("Comment published.", "success")
        else:
            flash("Thanks, your comment will be published after reviewed.", "info")
            send_new_comment_email(post)
        return redirect(url_for(".show_post", post_id=post_id))
    return render_template(
        "blog/post.html", post=post, pagination=pagination, comments=comments, form=form
    )
Beispiel #10
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['BLUELOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          site=site,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()

        if current_user.is_authenticated:
            flash('Comment pulished', 'success')
        else:
            flash('thanks,you comment will be published after reviewed',
                  'info')
            send_new_comment_email(post)
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
def add_comment_to_post(request, pk):
    """
    To add a comment when a 'request' with primary key('pk') of post given,
    :param request:
    :param pk:
    :return: the html tag 'form' and the form is created based on logic
    """
    # Grab the object or return a 404(not found) error
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        # If the form filled and request submitted
        # get the content to form
        form = CommentForm(request.POST)
        if form.is_valid():
            # checking the form valid or not,
            # if valid save to DB
            comment = form.save(commit=False)
            # post in Comment model foreignkey relation, attach comment to
            # this post
            comment.post = post
            comment.save()
            # after saving redirect to current post_detail page
            return redirect('myblog:post_detail', pk=post.pk)
    else:
        # the form is not posted stay on comment form
        form = CommentForm()
    # render out the html based on 'form' template tagging in
    # 'comment_form.html'
    return render(request, 'myblog/comment_form.html', {'form': form})
Beispiel #12
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.username
        form.email.data = current_app.config['MYBLOG_EMAIL']
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash('评论发布成功', 'success')
        else:
            flash('您的评论将在管理员审核后发布', 'info')
        return redirect(url_for('main.show_post', post_id=post_id))
    if request.args.get('from_index') == 'True':
        post.read_count += 1
        db.session.add(post)
        db.session.commit()
    return render_template('posts/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Beispiel #13
0
def FullPost(post_id):
    post = Post.query.get(post_id)
    form = CommentForm()

    if form.validate_on_submit():
        cmnt = Comment(content=form.content.data,
                       mainpost=post,
                       comment_author=current_user)
        db.session.add(cmnt)
        db.session.commit()
        flash(f'Comment Added Successfully', 'success')
        return redirect(url_for('FullPost', post_id=post.id))
    elif (form.is_submitted() and (not form.validate())):
        flash(f'An Error Has Occured Please Review Your Comment', 'danger')

    comments = Comment.query.filter_by(mainpost=post)
    return render_template("Post.html",
                           title=post.title,
                           post=post,
                           render_html=render_html,
                           user=post.author,
                           form=form,
                           form2=None,
                           comments=comments)
Beispiel #14
0
def EditComment(comment_id):

    cmnt2 = Comment.query.get(comment_id)
    post = cmnt2.mainpost

    form2 = EditCommentForm()
    if request.method == "GET":
        form2.content.data = cmnt2.content
    elif (form2.submit2.data and form2.validate()):
        cmnt2.content = form2.content.data
        db.session.commit()
        flash(f'Comment Edited Successfully', 'success')
        return redirect(url_for('FullPost', post_id=post.id))

    form = CommentForm()

    if (form.submit.data and form.validate()):
        cmnt = Comment(content=form.content.data,
                       mainpost=post,
                       comment_author=current_user)
        db.session.add(cmnt)
        db.session.commit()
        flash(f'Comment Added Successfully', 'success')
        return redirect(url_for('FullPost', post_id=post.id))
    elif (form.is_submitted() and (not form.validate())):
        flash(f'An Error Has Occured Please Review Your Comment', 'danger')

    comments = Comment.query.filter_by(mainpost=post)
    return render_template("Post.html",
                           title=post.title,
                           post=post,
                           render_html=render_html,
                           user=post.author,
                           form2=form2,
                           form=form,
                           comments=comments)
Beispiel #15
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_MANAGE_POST_PER_PAGE']
    # with_parent(post)表示当前文章下的所有评论,filter_by(reviewed=True)表示通过审核的评论
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate(
        page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:  # 如果当前用户已登陆,使用管理员表单
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['MYBLOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True
    else:  # 否则使用普通表单
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(
            author=author, email=email, site=site, body=body,
            from_admin=from_admin, post=post, reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:  # 若果存在参数reply,表示是回复
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:  # send message based on authentication status
            flash('回复成功', 'success')
        else:
            flash('谢谢,您的评论将在审核后发表', 'info')
            send_new_comment_email(post)  # send notification email to admin
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
Beispiel #16
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate(
        page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['MAIL_DEFAULT_SENDER']
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        # site = form.site.data
        body = form.body.data
        comment = Comment(author=author,body=body,post=post,email=email, reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:  # send message based on authentication status
            flash('Comment published.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.', 'info')
            send_new_comment_email(post)  # send notification email to admin
        return redirect(url_for('.show_post', post_id=post_id) + '#comment-form')
    return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
Beispiel #17
0
def post_detail(request, year, month, day, post):
    '''
    post detail view
    '''
    post = get_object_or_404(Post,
                             slug=post,
                             status='published',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)
    #list of active comments for this post
    comments = post.comments.filter(active=True)

    if request.method == "POST":
        #A comments was posted
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet
            new_comment = comment_form.save(commit=False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        #get empty form
        comment_form = CommentForm()

    # List of similar posts
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude(
        id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by(
        '-same_tags', '-publish')[:4]

    return render(
        request, "myblog/post/detail.html", {
            'post': post,
            'comments': comments,
            'comment_form': comment_form,
            'similar_posts': similar_posts
        })
Beispiel #18
0
def show_post(post_id):
    """
    文章详情页:文章内容+评论+评论表单
    :param post_id:
    :return:
    """
    # 获取文章详情
    post = Post.query.get_or_404(post_id)
    # 评论信息
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.desc()).paginate(
            page, per_page)
    comments = pagination.items

    # 验证用户是否登录,
    # 如果登录则将相关数据填入表单
    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['BLOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True  # 直接通过审核
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    # 表单验证通过后写入数据库
    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          site=site,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)

        # 对于评论的回复
        # 试图获取是否为回复:如果是回复,则添加对应的字段
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment  # 为回复建立与评论的关系
            # todo 邮件发送功能
            # send_new_reply_email(replied_comment)

        db.session.add(comment)
        db.session.commit()

        # 如果管理员登录则显示发布成功,如果不是则发送邮件通知管理员
        if current_user.is_authenticated:
            flash('Comment published.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.',
                  'info')
            # todo 邮件发送功能
            # send_new_comment_email(post)

        return redirect(url_for('.show_post', post_id=post_id))

    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)