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 post_new(request, topic_id, quoted_post_id=None): topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) if topic.is_closed: if not request.user.is_staff: messages.error(request, "You are not allowed to post in closed topics.") return redirect(topic.get_absolute_url()) else: messages.info(request, "Note: This topic is closed.") if not can_post_reply(request.user, topic.forum): messages.error(request, "You are not allowed to reply on this forum.") return redirect(topic.get_absolute_url()) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): content = form.cleaned_data['content'] post = Post(topic=topic, author=request.user, author_ip=request.META['REMOTE_ADDR'], content=content) post.save() messages.success(request, "Your reply has been saved.") return redirect(post.get_absolute_url()) else: form = PostForm() if quoted_post_id: try: quoted_post = Post.objects.get(pk=quoted_post_id, topic=topic) form.initial = {'content': "[quote=%s]%s[/quote]" % (quoted_post.author, quoted_post.content)} except Post.DoesNotExist: messages.warning(request, "You tried to quote a post which " "doesn't exist or it doesn't belong to topic you are " "replying to. Nice try, Kevin.") return {'topic': topic, 'form': form}
def add_thread(request, forum_id): # First try finding the (sub)forum forum = get_object_or_404(Subforum, pk=forum_id) # Load template template = loader.get_template('forums/add_thread.html') # We do different things for POST and GET if request.method == 'POST': # Grab the info from the post thingy try: title = request.POST['title'] content = request.POST['content'] # If something goes wrong... except (KeyError): return render(request, 'forums/add_thread.html', { 'forum': forum, 'error_message': "You didn't provide needed data!", }) # If we get both info out well? else: # If content is empty, error out if not content or not title: return render(request, 'forums/add_thread.html', { 'forum': forum, 'error_message': "Please do not leave content or title empty :< !", }) # Create and write post into the database, wee t = Thread(subforum=Subforum.objects.get(pk=forum.id), creator=request.user, title=title, creation_date=timezone.now(), sticky=False) if not t: return render(request, 'forums/add_thread.html', { 'forum': forum, 'error_message': "Gooby please... I have no idea what just happened, but you errored out (thread object creation).", }) t.save() p = Post(thread=t, poster=request.user, title=title, content=content, is_op=True, pub_date=timezone.now()) if not p: t.delete() return render(request, 'forums/add_thread.html', { 'forum': forum, 'error_message': "Gooby please... I have no idea what just happened, but you errored out (post object creation).", }) p.save() # For good measure, do a HttpResponseRedirect return HttpResponseRedirect(reverse(show_thread, args=(t.id,))) else: return render(request, 'forums/add_thread.html', { 'forum': forum })
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_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 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 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 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 if show_new_sumo(request): template = 'forums/includes/post_preview-new.html' else: template = 'forums/includes/post_preview.html' return jingo.render(request, template, {'post_preview': post})
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 save_model(self, request, obj, form, change): if change: obj.modified_by = request.user obj.first_post.modified_by = request.user obj.first_post.content = form.cleaned_data['content'] obj.first_post.save() obj.save() else: obj.author = request.user obj.author_ip = request.META['REMOTE_ADDR'] obj.save() post = Post(topic=obj, author=request.user, author_ip=request.META['REMOTE_ADDR'], content=form.cleaned_data['content']) post.save()
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 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 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() 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 jingo.render(request, 'forums/new_thread.html', {'form': form, 'forum': forum, 'post_preview': post_preview})
def form_valid(self, form): body = form.cleaned_data['message'] user = UserProfile.objects.get(user=User.objects.get(username=self.request.user.username)) post = Post(topic=self.topic, body=body, user=user) post.save() post.topic.last_post = post post.topic.save() view = View.objects.filter(topic=self.topic) for v in view: v.visited = False v.save() topic_page = self.topic.posts_range()[-1] self.success_url = ('/forums/topic/' + str(self.topic.id) + '/page' + str(topic_page)) 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 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 form_valid(self, form): topic_name = form.cleaned_data['topic'] post_body = form.cleaned_data['message'] user = UserProfile.objects.get(user=User.objects.get(username=self.request.user.username)) topic = Topic(forum=self.forum, name=topic_name, counter=0) topic.save() post = Post(topic=topic, body=post_body, user=user) post.save() topic.last_post = post topic.save() users = User.objects.all() for u in users: view = View(user=u, topic=topic, visited=False) view.save() self.success_url = ('/forums/topic/' + str(topic.id) + '/page1') return super(TopicCreateView, self).form_valid(form)
def add_post(request, thread_id): # First try finding the thread thread = get_object_or_404(Thread, pk=thread_id) # Load template template = loader.get_template('forums/add_post.html') # We do different things for POST and GET if request.method == 'POST': # Grab the info from the post thingy try: title = request.POST['title'] content = request.POST['content'] # If something goes wrong... except (KeyError): return render(request, 'forums/add_post.html', { 'thread': thread, 'error_message': "You didn't provide needed data!", }) # If we get both info out well? else: # If content is empty, error out if not content: return render(request, 'forums/add_post.html', { 'thread': thread, 'error_message': "Please do not leave content empty :< !", }) # If title is empty we just use Re: <thread title> if not title: title = "Re: " + thread.title # Create and write post into the database, wee p = Post(thread=thread, poster=request.user, title=title, content=content, pub_date=timezone.now()) p.save() # For good measure, do a HttpResponseRedirect return HttpResponseRedirect(reverse(show_thread, args=(thread.id,))) # And here is what GET n' shit does else: return render(request, 'forums/add_post.html', { 'thread': thread, })
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 topic_new(request, forum_id): forum = get_object_or_404(Forum, pk=forum_id) if not can_post_topic(request.user, forum): messages.error(request, "You are not allowed to to post new topics \ on this forum.") return redirect(forum.get_absolute_url()) if request.method == 'POST': form = TopicForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] topic = Topic(title=title, author=request.user, forum=forum) topic.save() post = Post(topic=topic, author=request.user, author_ip=request.META['REMOTE_ADDR'], content=content) post.save() messages.success(request, "Your topic has been saved.") return redirect(topic.get_absolute_url()) else: form = TopicForm() return {'forum': forum, 'form': form}
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 postData(request): if request.META["HTTP_USER_AGENT"] == "7a3acdfef4bc49b461827b01f365ba": post_data = json.loads(request.raw_post_data) operation = post_data["Type"] if operation == "Post": post_user = User.objects.get(pk=post_data["postedby"]) post_thread = Thread.objects.get(pk=post_data["threadid"]) new_post = Post(name=post_data["name"], message=post_data["message"], posted_by=post_user, thread=post_thread) new_post.save() elif operation == "PostDel": Post.objects.filter(pk=post_data["id"]).delete() elif operation == "PostEdit": edit_post = Post.objects.get(pk=post_data["id"]) edit_post.name = post_data["name"] edit_post.message = post_data["message"] edit_post.save() elif operation == "ThreadRename": edit_thread = Thread.objects.get(pk=post_data["id"]) edit_thread.name = post_data["name"] edit_thread.save() elif operation == "ThreadDel": del_thread = Thread.objects.filter(pk=post_data["id"]) Post.objects.filter(thread=del_thread).delete() del_thread.delete() elif operation == "Thread": post_user = User.objects.get(pk=post_data["postedby"]) new_thread = Thread(name=post_data["name"], posted_by=post_user) new_thread.save() return render_to_response('forums/post.html')
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)