def post_preview_async(request): """Ajax preview of posts.""" statsd.incr('forums.preview') post = Post(author=request.user, content=request.POST.get('content', '')) post.author_post_count = 1 return jingo.render(request, 'forums/includes/post_preview.html', {'post_preview': post})
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, })
def test_save_new_post_timestamps(self): # Saving a new post should allow you to override auto_add_now- # and auto_now-like functionality. created_ = datetime(1992, 1, 12, 10, 12, 32) p = Post(thread=self.thread, content='bar', author=self.user, created=created_, updated=created_) p.save() eq_(created_, p.created) eq_(created_, p.updated)
def test_save_new_post_no_timestamps(self): """ Saving a new post should behave as if auto_add_now was set on created and auto_now set on updated. """ p = Post(thread=self.thread, content='bar', author=self.user) p.save() now = datetime.datetime.now() self.assertDateTimeAlmostEqual(now, p.created, self.delta) self.assertDateTimeAlmostEqual(now, p.updated, self.delta)
def test_delete_last_and_only_post_in_thread(self): """Deleting the only post in a thread should delete the thread""" forum = Forum.objects.get(pk=1) thread = Thread(title="test", forum=forum, creator_id=118533) thread.save() post = Post(thread=thread, content="test", author=thread.creator) post.save() eq_(1, thread.post_set.count()) post.delete() eq_(0, Thread.uncached.filter(pk=thread.id).count())
def test_with_activity(self): """Test the page with some activity.""" # Add a reply post = Post(thread_id=4, content='lorem ipsum', author_id=118577) post.save() # Verify activity on the page response = self.client.get(reverse('dashboards.review'), follow=True) eq_(200, response.status_code) doc = pq(response.content) eq_(1, len(doc('ol.actions li')))
def form_valid(self, form): body = form.cleaned_data['message'] user = self.request.user post = Post(topic=self.topic, body=body, user=user) post.save() post.topic.last_post = post post.topic.save() self.success_url = reverse('forums:topic', args=[self.topic.id]) return super(PostCreateView, self).form_valid(form)
def new_thread(request, forum_slug): """Start a new 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 if request.method == 'GET': form = NewThreadForm() return render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum }) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, author=request.user, content=form.cleaned_data['content']) post_preview.author_post_count = \ post_preview.author.post_set.count() else: thread = forum.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() statsd.incr('forums.thread') post = thread.new_post(author=request.user, content=form.cleaned_data['content']) post.save() NewThreadEvent(post).fire(exclude=post.author) # Add notification automatically if needed. if Setting.get_for_user(request.user, 'forums_watch_new_thread'): NewPostEvent.notify(request.user, thread) url = reverse('forums.posts', args=[forum_slug, thread.id]) return HttpResponseRedirect(urlparams(url, last=post.id)) return render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum, 'post_preview': post_preview })
def form_valid(self, form): topic_name = form.cleaned_data['topic'] post_body = form.cleaned_data['message'] user = self.request.user topic = Topic(forum=self.forum, name=topic_name) topic.save() post = Post(topic=topic, body=post_body, user=user) post.save() topic.last_post = post topic.save() self.success_url = reverse('forums:topic', args=[topic.id]) return super(TopicCreateView, self).form_valid(form)
def new_thread(request, forum_slug): """Start a new 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 if request.method == 'GET': form = NewThreadForm() return jingo.render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum }) form = NewThreadForm(request.POST) post_preview = None if form.is_valid(): if 'preview' in request.POST: thread = Thread(creator=request.user, title=form.cleaned_data['title']) post_preview = Post(thread=thread, author=request.user, content=form.cleaned_data['content']) post_preview.author_post_count = \ post_preview.author.post_set.count() else: thread = forum.thread_set.create(creator=request.user, title=form.cleaned_data['title']) thread.save() post = thread.new_post(author=request.user, content=form.cleaned_data['content']) post.save() NewThreadEvent(post).fire(exclude=post.author) return HttpResponseRedirect( reverse('forums.posts', args=[forum_slug, thread.id])) return jingo.render(request, 'forums/new_thread.html', { 'form': form, 'forum': forum, 'post_preview': post_preview })
def test_delete_thread_with_last_forum_post(self): """Deleting the thread with a forum's last post should update the last_post field on the forum """ forum = Forum.objects.get(pk=1) last_post = forum.last_post # add a new thread and post, verify last_post updated thread = Thread(title="test", forum=forum, creator_id=118533) thread.save() post = Post(thread=thread, content="test", author=thread.creator) post.save() forum = Forum.objects.get(pk=1) eq_(forum.last_post.id, post.id) # delete the post, verify last_post updated thread.delete() forum = Forum.objects.get(pk=1) eq_(forum.last_post.id, last_post.id) eq_(Thread.objects.filter(pk=thread.id).count(), 0)
def test_last_post_updated(self): """Adding/Deleting the last post in a thread and forum should update the last_post field """ thread = Thread.objects.get(pk=4) user = User.objects.get(pk=118533) # add a new post, then check that last_post is updated new_post = Post(thread=thread, content="test", author=user) new_post.save() forum = Forum.objects.get(pk=1) thread = Thread.objects.get(pk=thread.id) eq_(forum.last_post.id, new_post.id) eq_(thread.last_post.id, new_post.id) # delete the new post, then check that last_post is updated new_post.delete() forum = Forum.objects.get(pk=1) thread = Thread.objects.get(pk=thread.id) eq_(forum.last_post.id, 25) eq_(thread.last_post.id, 25)
def post(**kwargs): defaults = dict() defaults.update(kwargs) if 'author' not in kwargs and 'author_id' not in kwargs: defaults['author'] = user(save=True) return Post(**defaults)