Пример #1
0
def post_view(request, uid):
    "Return a detailed view for specific post"

    # Get the post.
    post = Post.objects.filter(uid=uid).select_related('root').first()

    if not post:
        messages.error(request, "Post does not exist.")
        return redirect("post_list")

    auth.update_post_views(post=post, request=request)
    if not post.is_toplevel:
        return redirect(post.get_absolute_url())

    # Form used for answers
    form = forms.PostShortForm(user=request.user, post=post)

    if request.method == "POST":

        form = forms.PostShortForm(data=request.POST, user=request.user, post=post)
        if form.is_valid():
            author = request.user
            content = form.cleaned_data.get("content")
            answer = auth.create_post(title=post.title, parent=post, author=author,
                                      content=content, ptype=Post.ANSWER, root=post.root)
            return redirect(answer.get_absolute_url())
        messages.error(request, form.errors)

    # Build the comment tree .
    root, comment_tree, answers, thread = auth.post_tree(user=request.user, root=post.root)
    # user string

    context = dict(post=root, tree=comment_tree, form=form, answers=answers)

    return render(request, "post_view.html", context=context)
Пример #2
0
def post_view(request, uid):
    "Return a detailed view for specific post"

    # Form used for answers
    form = forms.PostShortForm()

    # Get the parents info
    obj = Post.objects.filter(uid=uid).first()
    # Return root view if not at top level.
    obj = obj if obj.is_toplevel else obj.root

    auth.update_post_views(post=obj, request=request)

    if request.method == "POST":
        form = forms.PostShortForm(data=request.POST)
        if form.is_valid():
            post = form.save(author=request.user)
            location = reverse("post_view",
                               request=request,
                               kwargs=dict(uid=obj.root.uid)) + "#" + post.uid
            if tasks.HAS_UWSGI:
                tasks.created_post(pid=post.id)

            return redirect(location)

    # Populate the object to build a tree that contains all posts in the thread.
    # Answers are added here as well.
    comment_tree, answers, thread = auth.build_obj_tree(request=request,
                                                        obj=obj)
    context = dict(post=obj, tree=comment_tree, form=form, answers=answers)

    return render(request, "post_view.html", context=context)
Пример #3
0
def post_view(request, uid):
    "Return a detailed view for specific post"

    # Get the post.
    post = Post.objects.filter(uid=uid).select_related('root').first()
    user = request.user
    if not post:
        messages.error(request, "Post does not exist.")
        return redirect("post_list")

    # Redirect to post view
    if not post.is_toplevel:
        return redirect(post.get_absolute_url())

    # Return 404 when post is spam and user is anonymous.
    if post.is_spam and user.is_anonymous:
        raise Http404("Post does not exist.")

    # Form used for answers
    form = forms.PostShortForm(user=request.user, post=post)

    if request.method == "POST":

        form = forms.PostShortForm(data=request.POST,
                                   user=request.user,
                                   post=post)
        if form.is_valid():
            author = request.user
            content = form.cleaned_data.get("content")
            answer = auth.create_post(title=post.title,
                                      parent=post,
                                      author=author,
                                      content=content,
                                      ptype=Post.ANSWER,
                                      root=post.root,
                                      request=request)
            return redirect(answer.get_absolute_url())
        messages.error(request, form.errors)

    # Build the comment tree .
    root, comment_tree, answers, thread = auth.post_tree(user=request.user,
                                                         root=post.root)

    # Bump post views.
    models.update_post_views(post=post,
                             request=request,
                             timeout=settings.POST_VIEW_TIMEOUT)

    context = dict(post=root, tree=comment_tree, form=form, answers=answers)

    return render(request, "post_view.html", context=context)
Пример #4
0
def comment(request):
    location = reverse("post_list")
    if request.method == "POST":
        form = forms.PostShortForm(data=request.POST)
        if form.is_valid():
            post = form.save(author=request.user, post_type=Post.COMMENT)
            messages.success(request, "Added comment")
            location = reverse("post_view",
                               kwargs=dict(uid=post.uid)) + "#" + post.uid
            if tasks.HAS_UWSGI:
                tasks.created_post(pid=post.id)
        else:
            messages.error(request, f"Error adding comment:{form.errors}")
            parent = Post.objects.filter(
                uid=request.POST.get("parent_uid")).first()
            location = location if parent is None else reverse(
                "post_view", kwargs=dict(uid=parent.root.uid))

    return redirect(location)