Example #1
0
def topic_reply(request, topic_id):
	topic = get_object_or_404(Topic, id=topic_id)
	post = Post(topic=topic, user=request.user)

	if request.POST:
		form = ReplyForm(request.POST, instance=post)
		if form.is_valid():
			form.save()
			topic.last_post_at = post.created_at
			topic.last_post_by_user = request.user
			topic.reply_count = topic.posts.count() - 1
			topic.save()
			return redirect(topic.get_absolute_url() + ('#post-%d' % post.id))
		else:
			# the only possible error is leaving the box totally empty.
			# Redirect back to topic page in that case
			return redirect(topic.get_absolute_url())
	else:
		form = ReplyForm(instance=post)

	return render(request, 'forums/add_reply.html', {
		'menu_section': 'forums',
		'topic': topic,
		'form': form,
	})
Example #2
0
def reply(request, forum_slug, thread_id):
    """Reply to a thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    form = ReplyForm(request.POST)
    reply_preview = None
    if form.is_valid():
        thread = get_object_or_404(Thread, pk=thread_id, forum=forum)

        if not thread.is_locked:
            reply_ = form.save(commit=False)
            reply_.thread = thread
            reply_.author = request.user
            if 'preview' in request.POST:
                reply_preview = reply_
            else:
                reply_.save()

                # Send notifications to thread/forum watchers.
                NewPostEvent(reply_).fire(exclude=reply_.author)

                return HttpResponseRedirect(reply_.get_absolute_url())

    return posts(request, forum_slug, thread_id, form, reply_preview)
Example #3
0
File: api.py Project: octaflop/uttr
def reply_to_parent(request, parent_id):
    """
    Creates a form for sending replies
    Also creates a reply on POST
    Relies on jQuery's html-handling
    """
    ctx = {}
    template_name = 'forums/api/reply_to_parent.html'

    reply_form = ReplyForm()
    ctx['reply_form'] = reply_form
    ctx['parent_id'] = parent_id
    parent = Reply.objects.get(id=base36_to_int(parent_id))
    topic = parent.topic

    if request.method == 'POST':
        reply_form = ReplyForm(request.POST)
        reply_form.topic = topic
        reply_form.author = request.user
        if reply_form.is_valid:
            reply = reply_form.save(commit=False)
            reply.author = request.user
            reply.topic = topic
            reply.parent = parent
            reply.status = 'posted'
            reply.entry = reply.draft
            reply.save()
            messages.warning(request, "Thank you for your reply. It has been sent for moderation")
            return redirect(topic.get_absolute_url())

    return render(request, template_name, ctx)
Example #4
0
File: api.py Project: octaflop/uttr
def reply_to_topic(request, topic_id):
    """
    A direct reply to a topic
    """
    ctx = {}
    template_name = 'forums/api/reply_form.html'

    reply_form = ReplyForm()
    ctx['reply_form'] = reply_form
    ctx['topic_id'] = topic_id
    topic = Topic.objects.get(id=base36_to_int(topic_id))

    if request.method == 'POST':
        reply_form = ReplyForm(request.POST)
        reply_form.topic = topic
        reply_form.author = request.user
        if reply_form.is_valid:
            reply = reply_form.save(commit=False)
            reply.author = request.user
            reply.topic = topic
            reply.save()
            messages.warning(request, "Thank you for your reply. It has been sent for moderation")
            return redirect(topic.get_absolute_url())

            # we might have to use a JS return
            # response = HttpResponse()
            # response.type = 'text/javascript'
            # response.write('Success!')
            # return response

    return render(request, template_name, ctx)
Example #5
0
def reply_create(request, thread_id):

    member = request.user
    thread = get_object_or_404(ForumThread, id=thread_id)

    if thread.closed:
        messages.error(request, "This thread is closed.")
        return HttpResponseRedirect(reverse("forums_thread", args=[thread.id]))

    can_create_reply = request.user.has_perm("forums.add_forumreply", obj=thread)

    if not can_create_reply:
        messages.error(request, "You do not have permission to reply to this thread.")
        return HttpResponseRedirect(reverse("forums_thread", args=[thread.id]))

    if request.method == "POST":
        form = ReplyForm(request.POST)

        if form.is_valid():
            reply = form.save(commit=False)
            reply.thread = thread
            reply.author = request.user
            reply.save()

            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:
                thread.subscribe(reply.author, "email")

            # all users are automatically subscribed to onsite
            thread.subscribe(reply.author, "onsite")

            return HttpResponseRedirect(reverse("forums_thread", args=[thread_id]))
    else:
        quote = request.GET.get("quote")  # thread id to quote
        initial = {}

        if quote:
            quote_reply = ForumReply.objects.get(id=int(quote))
            initial["content"] = "\"%s\"" % quote_reply.content

        form = ReplyForm(initial=initial)

    first_reply = not ForumReply.objects.filter(thread=thread, author=request.user).exists()

    return render_to_response("forums/reply_create.html", {
        "form": form,
        "member": member,
        "thread": thread,
        "subscribed": thread.subscribed(request.user, "email"),
        "first_reply": first_reply,
    }, context_instance=RequestContext(request))
Example #6
0
def posts(request, forum_slug, thread_id, form=None, reply_preview=None):
    """View all the posts in a thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404

    thread = get_object_or_404(Thread, pk=thread_id, forum=forum)

    posts_ = thread.post_set.all()
    count = posts_.count()
    posts_ = posts_.select_related('author', 'updated_by')
    posts_ = posts_.extra(
        select={'author_post_count': 'SELECT COUNT(*) FROM forums_post WHERE '
                                     'forums_post.author_id = auth_user.id'})
    posts_ = paginate(request, posts_, constants.POSTS_PER_PAGE, count=count)

    if not form:
        form = ReplyForm()

    feed_urls = ((reverse('forums.posts.feed',
                          kwargs={'forum_slug': forum_slug,
                                  'thread_id': thread_id}),
                  PostsFeed().title(thread)),)

    is_watching_thread = (request.user.is_authenticated() and
                          NewPostEvent.is_notifying(request.user, thread))
    return jingo.render(request, 'forums/posts.html',
                        {'forum': forum, 'thread': thread,
                         'posts': posts_, 'form': form,
                         'reply_preview': reply_preview,
                         'is_watching_thread': is_watching_thread,
                         'feeds': feed_urls,
                         'forums': Forum.objects.all()})
Example #7
0
def posts(request, forum_slug, thread_id, form=None, reply_preview=None):
    """View all the posts in a thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404

    thread = get_object_or_404(Thread, pk=thread_id, forum=forum)

    posts_ = paginate(request, thread.post_set.all(), constants.POSTS_PER_PAGE)

    if not form:
        form = ReplyForm()

    feed_urls = ((reverse('forums.posts.feed',
                          kwargs={
                              'forum_slug': forum_slug,
                              'thread_id': thread_id
                          }), PostsFeed().title(thread)), )

    is_watching_thread = (request.user.is_authenticated()
                          and NewPostEvent.is_notifying(request.user, thread))
    return jingo.render(
        request, 'forums/posts.html', {
            'forum': forum,
            'thread': thread,
            'posts': posts_,
            'form': form,
            'reply_preview': reply_preview,
            'is_watching_thread': is_watching_thread,
            'feeds': feed_urls,
            'forums': Forum.objects.all()
        })
Example #8
0
def posts(request,
          forum_slug,
          thread_id,
          form=None,
          post_preview=None,
          is_reply=False):
    """View all the posts in a thread."""
    thread = get_object_or_404(Thread, pk=thread_id)
    forum = thread.forum

    if forum.slug != forum_slug and not is_reply:
        new_forum = get_object_or_404(Forum, slug=forum_slug)
        if new_forum.allows_viewing_by(request.user):
            return HttpResponseRedirect(thread.get_absolute_url())
        raise Http404  # User has no right to view destination forum.
    elif forum.slug != forum_slug:
        raise Http404

    if not forum.allows_viewing_by(request.user):
        raise Http404

    posts_ = thread.post_set.all()
    count = posts_.count()
    if count:
        last_post = posts_[count - 1]
    else:
        last_post = None
    posts_ = posts_.select_related('author', 'updated_by')
    posts_ = posts_.extra(
        select={
            'author_post_count':
            'SELECT COUNT(*) FROM forums_post WHERE '
            'forums_post.author_id = auth_user.id'
        })
    posts_ = paginate(request, posts_, constants.POSTS_PER_PAGE, count=count)

    if not form:
        form = ReplyForm()

    feed_urls = ((reverse('forums.posts.feed',
                          kwargs={
                              'forum_slug': forum_slug,
                              'thread_id': thread_id
                          }), PostsFeed().title(thread)), )

    is_watching_thread = (request.user.is_authenticated()
                          and NewPostEvent.is_notifying(request.user, thread))
    return render(
        request, 'forums/posts.html', {
            'forum': forum,
            'thread': thread,
            'posts': posts_,
            'form': form,
            'count': count,
            'last_post': last_post,
            'post_preview': post_preview,
            'is_watching_thread': is_watching_thread,
            'feeds': feed_urls,
            'forums': Forum.objects.all()
        })
Example #9
0
def topic_reply(request, topic_id):
    topic = get_object_or_404(Topic, id=topic_id)
    post = Post(topic=topic, user=request.user)

    if request.POST:
        form = ReplyForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            topic.last_post_at = post.created_at
            topic.last_post_by_user = request.user
            topic.reply_count = topic.posts.count() - 1
            topic.save()
            return redirect(topic.get_absolute_url() + ('#post-%d' % post.id))
        else:
            # the only possible error is leaving the box totally empty.
            # Redirect back to topic page in that case
            return redirect(topic.get_absolute_url())
    else:
        form = ReplyForm(instance=post)

    return render(request, 'forums/add_reply.html', {
        'menu_section': 'forums',
        'topic': topic,
        'form': form,
    })
Example #10
0
def forum_thread(request, thread_id):
    qs = ForumThread.objects.select_related("forum")
    thread = get_object_or_404(qs, id=thread_id)

    can_create_reply = all([
        request.user.has_perm("forums.add_forumreply", obj=thread),
        not thread.closed,
        not thread.forum.closed,
    ])

    if can_create_reply:
        if request.method == "POST":
            reply_form = ReplyForm(request.POST)

            if reply_form.is_valid():
                reply = reply_form.save(commit=False)
                reply.thread = thread
                reply.author = request.user
                reply.save()

                # subscribe the poster to the thread if requested (default value is True)
                if reply_form.cleaned_data["subscribe"]:
                    thread.subscribe(reply.author, "email")

                # all users are automatically subscribed to onsite
                thread.subscribe(reply.author, "onsite")

                return HttpResponseRedirect(reverse("forums_thread", args=[thread.id]))
        else:
            reply_form = ReplyForm()
    else:
        reply_form = None

    order_type = request.GET.get("order_type", "asc")
    posts = ForumThread.objects.posts(thread, reverse=(order_type == "desc"))
    thread.inc_views()

    return render_to_response("forums/thread.html", {
        "thread": thread,
        "posts": posts,
        "order_type": order_type,
        "subscribed": thread.subscribed(request.user, "email"),
        "reply_form": reply_form,
        "can_create_reply": can_create_reply,
    }, context_instance=RequestContext(request))
Example #11
0
def reply(request, forum_slug, thread_id):
    """Reply to a thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    form = ReplyForm(request.POST)
    post_preview = None
    if form.is_valid():
        thread = get_object_or_404(Thread, pk=thread_id, forum=forum)

        if not thread.is_locked:
            reply_ = form.save(commit=False)
            reply_.thread = thread
            reply_.author = request.user
            if 'preview' in request.POST:
                post_preview = reply_
                post_preview.author_post_count = \
                    reply_.author.post_set.count()
            else:
                reply_.save()
                statsd.incr('forums.reply')

                # Subscribe the user to the thread.
                if Setting.get_for_user(request.user,
                                        'forums_watch_after_reply'):
                    NewPostEvent.notify(request.user, thread)

                # Send notifications to thread/forum watchers.
                NewPostEvent(reply_).fire(exclude=reply_.author)

                return HttpResponseRedirect(thread.get_last_post_url())

    return posts(request,
                 forum_slug,
                 thread_id,
                 form,
                 post_preview,
                 is_reply=True)
Example #12
0
def reply_to_thread(request, pk):

    thread = get_object_or_404(Thread, id=pk)
    reply_form = ReplyForm(data=request.POST)

    """Reply to a thread."""
    if request.method == 'POST':

        if reply_form.is_valid():

            r = reply_form.save(commit=False)
            r.thread_id = thread.pk
            r.creator_id = request.user.pk
            r.save()

        else:
            print(reply_form.errors)

    return HttpResponseRedirect(reverse("forums.views.show_thread", args=[thread.pk]) + "?page=last")
Example #13
0
def reply(request, forum_slug, thread_id):
    """Reply to a thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    form = ReplyForm(request.POST)
    post_preview = None
    if form.is_valid():
        thread = get_object_or_404(Thread, pk=thread_id, forum=forum)

        if not thread.is_locked:
            reply_ = form.save(commit=False)
            reply_.thread = thread
            reply_.author = request.user
            if 'preview' in request.POST:
                post_preview = reply_
                post_preview.author_post_count = \
                    reply_.author.post_set.count()
            else:
                reply_.save()
                statsd.incr('forums.reply')

                # Subscribe the user to the thread.
                if Setting.get_for_user(request.user,
                                        'forums_watch_after_reply'):
                    NewPostEvent.notify(request.user, thread)

                # Send notifications to thread/forum watchers.
                NewPostEvent(reply_).fire(exclude=reply_.author)

                return HttpResponseRedirect(thread.get_last_post_url())

    return posts(request, forum_slug, thread_id, form, post_preview,
                 is_reply=True)
Example #14
0
def reply_create(request, thread_id):

    member = request.user.profile
    thread = get_object_or_404(ForumThread, id=thread_id)

    if thread.closed:
        messages.error(request, "This thread is closed.")
        return HttpResponseRedirect(reverse("forums_thread", args=[thread.id]))

    can_create_reply = request.user.has_perm("forums.add_forumreply",
                                             obj=thread)

    if not can_create_reply:
        messages.error(request,
                       "You do not have permission to reply to this thread.")
        return HttpResponseRedirect(reverse("forums_thread", args=[thread.id]))

    if request.method == "POST":
        form = ReplyForm(request.POST)

        if form.is_valid():
            reply = form.save(commit=False)
            reply.thread = thread
            reply.author = request.user
            reply.save()

            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:
                thread.subscribe(reply.author, "email")

            # all users are automatically subscribed to onsite
            thread.subscribe(reply.author, "onsite")

            return HttpResponseRedirect(
                reverse("forums_thread", args=[thread_id]))
    else:
        quote = request.GET.get("quote")  # thread id to quote
        initial = {}

        if quote:
            quote_reply = ForumReply.objects.get(id=int(quote))
            initial["content"] = "\"%s\"" % quote_reply.content

        form = ReplyForm(initial=initial)

    first_reply = not ForumReply.objects.filter(thread=thread,
                                                author=request.user).exists()

    return render_to_response(
        "forums/reply_create.html", {
            "form": form,
            "member": member,
            "thread": thread,
            "subscribed": thread.subscribed(request.user, "email"),
            "first_reply": first_reply,
        },
        context_instance=RequestContext(request))
Example #15
0
def post(request, post_id):
    """ topic view but ensuring that we display the page that contains the given post """
    post = get_object_or_404(Post, id=post_id)
    topic = post.topic
    posts = topic.posts.order_by('created_at').select_related('user')

    post_offset = topic.posts.filter(created_at__lt=post.created_at).count()
    paginator = Paginator(posts, POSTS_PER_PAGE)

    page = (post_offset / POSTS_PER_PAGE) + 1
    posts_page = paginator.page(page)

    return render(
        request, 'forums/topic.html', {
            'menu_section': 'forums',
            'topic': topic,
            'posts': posts_page,
            'form': ReplyForm(),
        })
Example #16
0
def topic(request, topic_id):
    topic = get_object_or_404(Topic, id=topic_id)
    posts = topic.posts.order_by('created_at').select_related('user')
    paginator = Paginator(posts, POSTS_PER_PAGE)

    page = request.GET.get('page')
    try:
        posts_page = paginator.page(page)
    except (PageNotAnInteger, EmptyPage):
        # If page is not an integer, or out of range (e.g. 9999), deliver last page of results.
        posts_page = paginator.page(paginator.num_pages)

    return render(
        request, 'forums/topic.html', {
            'menu_section': 'forums',
            'topic': topic,
            'posts': posts_page,
            'form': ReplyForm(),
        })
Example #17
0
def forum_thread(request, thread_id):
    qs = ForumThread.objects.select_related("forum")
    thread = get_object_or_404(qs, id=thread_id)

    can_create_reply = all([
        request.user.has_perm("forums.add_forumreply", obj=thread),
        not thread.closed,
        not thread.forum.closed,
    ])

    if can_create_reply:
        if request.method == "POST":
            reply_form = ReplyForm(request.POST)

            if reply_form.is_valid():
                reply = reply_form.save(commit=False)
                reply.thread = thread
                reply.author = request.user
                reply.save()

                # subscribe the poster to the thread if requested (default value is True)
                if reply_form.cleaned_data["subscribe"]:
                    thread.subscribe(reply.author, "email")

                # all users are automatically subscribed to onsite
                thread.subscribe(reply.author, "onsite")

                return HttpResponseRedirect(
                    reverse("forums_thread", args=[thread.id]))
        else:
            reply_form = ReplyForm()
    else:
        reply_form = None

    order_type = request.GET.get("order_type", "asc")
    posts = ForumThread.objects.posts(thread, reverse=(order_type == "desc"))
    thread.inc_views()

    return render_to_response(
        "forums/thread.html", {
            "thread": thread,
            "posts": posts,
            "order_type": order_type,
            "subscribed": thread.subscribed(request.user, "email"),
            "reply_form": reply_form,
            "can_create_reply": can_create_reply,
        },
        context_instance=RequestContext(request))
Example #18
0
def post(request, post_id):
    """ topic view but ensuring that we display the page that contains the given post """
    post = get_object_or_404(Post, id=post_id)
    topic = post.topic
    posts = topic.posts.order_by('created_at').select_related('user')

    post_offset = topic.posts.filter(created_at__lt=post.created_at).count()
    paginator = Paginator(posts, POSTS_PER_PAGE)

    page = (post_offset / POSTS_PER_PAGE) + 1
    try:
        posts_page = paginator.page(page)
    except (PageNotAnInteger, EmptyPage):
        # If page is not an integer, or out of range (e.g. 9999), deliver last page of results.
        posts_page = paginator.page(paginator.num_pages)

    return render(
        request, 'forums/topic.html', {
            'menu_section': 'forums',
            'topic': topic,
            'posts': posts_page,
            'form': ReplyForm(),
        })