Beispiel #1
0
def show_post(request, post_type, post_slug):
    post = get_object_or_404(Post, slug=post_slug)

    # post_type can be changed by moderator
    if post.type != post_type:
        return redirect("show_post", post.type, post.slug)

    # don't show private posts into public
    if not post.is_public:
        access_denied = check_user_permissions(request, post=post)
        if access_denied:
            return access_denied

    # drafts are visible only to authors and moderators
    if not post.is_visible:
        if not request.me or (request.me != post.author
                              and not request.me.is_moderator):
            raise Http404()

    # record a new view
    last_view_at = None
    if request.me:
        request.me.update_last_activity()
        post_view, last_view_at = PostView.register_view(
            request=request,
            user=request.me,
            post=post,
        )
    else:
        PostView.register_anonymous_view(
            request=request,
            post=post,
        )

    return render_post(request, post, {"post_last_view_at": last_view_at})
Beispiel #2
0
def show_post(request, post_type, post_slug):
    post = get_object_or_404(Post, type=post_type, slug=post_slug)

    # don't show private posts into public
    if not post.is_public:
        access_denied = check_user_permissions(request)
        if access_denied:
            return access_denied

    # drafts are visible only to authors and moderators
    if not post.is_visible:
        if not request.me or (request.me != post.author
                              and not request.me.is_moderator):
            raise Http404()

    # record a new view
    if request.me:
        request.me.update_last_activity()
        PostView.create_or_update(
            request=request,
            user=request.me,
            post=post,
        )

    return render_post(request, post)
Beispiel #3
0
def show_post(request, post_type, post_slug):
    post = get_object_or_404(Post, slug=post_slug)

    # post_type can be changed by moderator
    if post.type != post_type:
        return redirect("show_post", post.type, post.slug)

    # don't show private posts into public
    if not post.is_public:
        access_denied = check_user_permissions(request, post=post)
        if access_denied:
            return access_denied

    # drafts are visible only to authors and moderators
    if not post.is_visible:
        if not request.me or (request.me != post.author
                              and not request.me.is_moderator):
            raise Http404()

    # record a new view
    last_view_at = None
    if request.me:
        request.me.update_last_activity()
        post_view, last_view_at = PostView.register_view(
            request=request,
            user=request.me,
            post=post,
        )
    else:
        PostView.register_anonymous_view(
            request=request,
            post=post,
        )

    # find linked posts and sort them by upvotes
    linked_posts = sorted(
        {
            link.post_to if link.post_to != post else link.post_from
            for link in LinkedPost.links_for_post(post)[:50]
        },
        key=lambda p: p.upvotes,
        reverse=True)

    # force cleanup deleted/hidden posts from linked
    linked_posts = [p for p in linked_posts if p.is_visible]

    return render_post(request, post, {
        "post_last_view_at": last_view_at,
        "linked_posts": linked_posts,
    })