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() elif not is_ratelimited(request, "forum-post", "15/d"): reply_.save() # 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)
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() })
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() elif (_skip_post_ratelimit(request) or not is_ratelimited(request, increment=True, rate='5/d', ip=False, keys=user_or_ip('forum-post'))): 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)
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(), }, )