def test_activity_multiple_forums(self): """Checks links are correct when there is activity from >1 forum.""" jsocol = User.objects.get(username='******') rrosario = User.objects.get(username='******') forum = Forum.objects.get(slug='another-forum') # Create new thread in Another Forum thread = Thread(creator=jsocol, title='foobartest', forum=forum) thread.save() post = thread.new_post(author=jsocol, content='loremipsumdolor') post.save() # Add a reply post = thread.new_post(author=rrosario, content='replyhi') post.save() # Verify links self.client.login(username='******', password='******') response = self.client.get(reverse('dashboards.review'), follow=True) eq_(200, response.status_code) doc = pq(response.content) links = doc('ol.threads div.title a') hrefs = [link.attrib['href'] for link in links] eq_(5, len(hrefs)) for i in range(5): if i == 2: assert hrefs[i].startswith('/en-US/forums/another-forum/') else: assert hrefs[i].startswith('/en-US/forums/test-forum/')
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_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 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 test_admin_delete_user_with_watched_thread(self, get_current): """Test the admin delete view for a user with a watched thread.""" get_current.return_value.domain = 'testserver' self.client.login(username='******', password='******') u = user(save=True) f = Forum.objects.all()[0] t = Thread(creator=u, forum=f, title='title') t.save() self._toggle_watch_thread_as('pcraciunoiu', thread_id=t.id, turn_on=True) url = reverse('admin:auth_user_delete', args=[u.id]) request = test_utils.RequestFactory().get(url) request.user = User.objects.get(username='******') request.session = self.client.session # The following blows up without our monkeypatch. ModelAdmin(User, admin.site).delete_view(request, str(u.id))
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 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_thread_model(): forum = Forum(title='Test Forum', description='This is a Test Forum') forum.save() user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', ) thread = Thread(title='A new thread', text='The text of the thread', forum=forum, user=user) thread.save() assert thread.title == 'A new thread' assert thread.text == 'The text of the thread' assert thread.forum == forum assert thread.user == user assert thread.added assert thread.edited assert str( thread) == f'Thread: {thread.title} - (started by {thread.user})'
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']) 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 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')