def delete(self, request, pk=None): profile = self.get_user(request, pk) allow_delete_user(request.user, profile) if request.method == 'POST': with transaction.atomic(): profile.lock() if request.data.get('with_content'): profile.delete_content() else: categories_to_sync = set() threads = profile.thread_set.select_related('category', 'first_post') for thread in threads.filter(is_hidden=False).iterator(): categories_to_sync.add(thread.category_id) hide_thread(request, thread) posts = profile.post_set.select_related( 'category', 'thread', 'thread__category' ) for post in posts.filter(is_hidden=False).iterator(): categories_to_sync.add(post.category_id) hide_post(request.user, post) post.thread.synchronize() post.thread.save() categories = Category.objects.filter(id__in=categories_to_sync) for category in categories.iterator(): category.synchronize() category.save() profile.delete() return Response({})
def delete(self, request, pk=None): profile = self.get_user(request, pk) allow_delete_user(request.user, profile) if request.method == 'POST': with transaction.atomic(): profile.lock() if request.data.get('with_content'): profile.delete_content() else: categories_to_sync = set() threads = profile.thread_set.select_related('category', 'first_post') for thread in threads.filter(is_hidden=False).iterator(): categories_to_sync.add(thread.category_id) hide_thread(request, thread) posts = profile.post_set.select_related( 'category', 'thread', 'thread__category' ) for post in posts.filter(is_hidden=False).iterator(): categories_to_sync.add(post.category_id) hide_post(request.user, post) post.thread.synchronize() post.thread.save() categories = Category.objects.filter(id__in=categories_to_sync) for category in categories.iterator(): category.synchronize() category.save() profile.delete() return Response({'detail': 'ok'})
def patch_is_hidden(request, event, value): if value: allow_hide_event(request.user, event) moderation.hide_post(request.user, event) else: allow_unhide_event(request.user, event) moderation.unhide_post(request.user, event) return {'is_hidden': event.is_hidden}
def patch_is_hidden(request, post, value): if value is True: allow_hide_post(request.user, post) allow_hide_best_answer(request.user, post) moderation.hide_post(request.user, post) elif value is False: allow_unhide_post(request.user, post) moderation.unhide_post(request.user, post) return {'is_hidden': post.is_hidden}
def test_anonymous_user_view_no_showstoppers_display(self): """kitchensink thread view has no showstoppers for anons""" poll = testutils.post_poll(self.thread, self.user) event = record_event(MockRequest(self.user), self.thread, 'closed') hidden_event = record_event(MockRequest(self.user), self.thread, 'opened') hide_post(self.user, hidden_event) unapproved_post = testutils.reply_thread(self.thread, is_unapproved=True) post = testutils.reply_thread(self.thread) self.logout_user() response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, poll.question) self.assertContains(response, event.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertNotContains(response, hidden_event.get_absolute_url()) self.assertNotContains(response, unapproved_post.get_absolute_url())
def test_hidden_post_visibility(self): """hidden post renders correctly""" post = testutils.reply_thread(self.thread, message="Hello, I'm hidden post!") hide_post(self.user, post) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains( response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # posts authors are not extempt from seeing hidden posts content post.posted_by = self.user post.save() response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains( response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # permission to hide own posts isn't enought to see post content self.override_acl({'can_hide_own_posts': 1}) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains( response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # post's content is displayed after permission to see posts is granted self.override_acl({'can_hide_posts': 1}) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains( response, "This post is hidden. Only users with permission may see its contents." ) self.assertNotContains( response, "This post is hidden. You cannot not see its contents.") self.assertContains(response, post.parsed)
def test_hide_post(self): """hide_post hides post""" self.assertFalse(self.post.is_hidden) self.assertTrue(moderation.hide_post(self.user, self.post)) self.reload_post() self.assertTrue(self.post.is_hidden) self.assertEqual(self.post.hidden_by, self.user) self.assertEqual(self.post.hidden_by_name, self.user.username) self.assertEqual(self.post.hidden_by_slug, self.user.slug) self.assertIsNotNone(self.post.hidden_on)
def test_hidden_post_visibility(self): """hidden post renders correctly""" post = testutils.reply_thread(self.thread, message="Hello, I'm hidden post!") hide_post(self.user, post) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains(response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # posts authors are not extempt from seeing hidden posts content post.posted_by = self.user post.save() response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains(response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # permission to hide own posts isn't enought to see post content self.override_acl({'can_hide_own_posts': 1}) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains(response, "This post is hidden. You cannot not see its contents.") self.assertNotContains(response, post.parsed) # post's content is displayed after permission to see posts is granted self.override_acl({'can_hide_posts': 1}) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, post.get_absolute_url()) self.assertContains( response, "This post is hidden. Only users with permission may see its contents." ) self.assertNotContains(response, "This post is hidden. You cannot not see its contents.") self.assertContains(response, post.parsed)
def action_hide(self, request, posts): changed_posts = 0 for post in posts: if moderation.hide_post(request.user, post): changed_posts += 1 if changed_posts: message = ungettext( '%(changed)d post was hidden.', '%(changed)d posts were hidden.', changed_posts) messages.success(request, message % {'changed': changed_posts}) else: message = _("No posts were hidden.") messages.info(request, message)
def test_hide_hidden_post(self): """hide_post fails to hide hidden post gracefully""" self.post.is_hidden = True self.assertFalse(moderation.hide_post(self.user, self.post))
def test_thread_events_render(self): """different thread events render""" TEST_ACTIONS = [ (threads_moderation.pin_thread_globally, "Thread has been pinned globally."), (threads_moderation.pin_thread_locally, "Thread has been pinned locally."), (threads_moderation.unpin_thread, "Thread has been unpinned."), (threads_moderation.approve_thread, "Thread has been approved."), (threads_moderation.close_thread, "Thread has been closed."), (threads_moderation.open_thread, "Thread has been opened."), (threads_moderation.hide_thread, "Thread has been made hidden."), (threads_moderation.unhide_thread, "Thread has been revealed."), ] self.thread.is_unapproved = True self.thread.save() for action, message in TEST_ACTIONS: self.override_acl({'can_approve_content': 1, 'can_hide_threads': 1}) self.thread.post_set.filter(is_event=True).delete() action(MockRequest(self.user), self.thread) event = self.thread.post_set.filter(is_event=True)[0] # event renders response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, event.get_absolute_url()) self.assertContains(response, message) # hidden events don't render without permission hide_post(self.user, event) self.override_acl({'can_approve_content': 1, 'can_hide_threads': 1}) response = self.client.get(self.thread.get_absolute_url()) self.assertNotContains(response, event.get_absolute_url()) self.assertNotContains(response, message) # hidden event renders with permission hide_post(self.user, event) self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1, 'can_hide_events': 1, }) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, event.get_absolute_url()) self.assertContains(response, message) self.assertContains(response, "Hidden by") # Event is only loaded if thread has events flag self.thread.has_events = False self.thread.save() self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1, 'can_hide_events': 1, }) response = self.client.get(self.thread.get_absolute_url()) self.assertNotContains(response, event.get_absolute_url())
def test_hide_original_post(self): """hide_post fails for first post in thread""" with self.assertRaises(moderation.ModerationError): moderation.hide_post(self.user, self.thread.first_post)
def real_dispatch(self, request, post): permissions.allow_hide_post(request.user, post) moderation.hide_post(request.user, post) messages.success(request, _("Post has been hidden."))
def test_thread_events_render(self): """different thread events render""" TEST_ACTIONS = [ (threads_moderation.pin_thread_globally, "Thread has been pinned globally."), (threads_moderation.pin_thread_locally, "Thread has been pinned locally."), (threads_moderation.unpin_thread, "Thread has been unpinned."), (threads_moderation.approve_thread, "Thread has been approved."), (threads_moderation.close_thread, "Thread has been closed."), (threads_moderation.open_thread, "Thread has been opened."), (threads_moderation.hide_thread, "Thread has been made hidden."), (threads_moderation.unhide_thread, "Thread has been revealed."), ] self.thread.is_unapproved = True self.thread.save() for action, message in TEST_ACTIONS: self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1 }) self.thread.post_set.filter(is_event=True).delete() action(MockRequest(self.user), self.thread) event = self.thread.post_set.filter(is_event=True)[0] # event renders response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, event.get_absolute_url()) self.assertContains(response, message) # hidden events don't render without permission hide_post(self.user, event) self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1 }) response = self.client.get(self.thread.get_absolute_url()) self.assertNotContains(response, event.get_absolute_url()) self.assertNotContains(response, message) # hidden event renders with permission hide_post(self.user, event) self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1, 'can_hide_events': 1, }) response = self.client.get(self.thread.get_absolute_url()) self.assertContains(response, event.get_absolute_url()) self.assertContains(response, message) self.assertContains(response, "Hidden by") # Event is only loaded if thread has events flag self.thread.has_events = False self.thread.save() self.override_acl({ 'can_approve_content': 1, 'can_hide_threads': 1, 'can_hide_events': 1, }) response = self.client.get(self.thread.get_absolute_url()) self.assertNotContains(response, event.get_absolute_url())