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 split_posts_to_new_thread(request, thread, validated_data): new_thread = Thread( category=validated_data['category'], started_on=thread.started_on, last_post_on=thread.last_post_on, ) new_thread.set_title(validated_data['title']) new_thread.save() for post in validated_data['posts']: post.move(new_thread) post.save() thread.synchronize() thread.save() new_thread.synchronize() new_thread.save() if validated_data.get('weight') == Thread.WEIGHT_GLOBAL: moderation.pin_thread_globally(request, new_thread) elif validated_data.get('weight'): moderation.pin_thread_locally(request, new_thread) if validated_data.get('is_hidden', False): moderation.hide_thread(request, new_thread) if validated_data.get('is_closed', False): moderation.close_thread(request, new_thread) thread.category.synchronize() thread.category.save() if new_thread.category != thread.category: new_thread.category.synchronize() new_thread.category.save()
def action_hide(self, request, thread): with atomic(): self.forum.lock() moderation.hide_thread(request.user, thread) self.forum.synchronize() self.forum.save() messages.success(request, _("Thread was hid."))
def post_save(self, serializer): if self.thread.category.acl['can_hide_threads']: if serializer.validated_data.get('hide'): moderation.hide_thread(self.request, self.thread) self.thread.update_all = True self.thread.save(update_fields=['is_hidden']) self.thread.category.synchronize() self.thread.category.update_all = True
def patch_is_hidden(request, thread, value): if value: allow_hide_thread(request.user, thread) moderation.hide_thread(request, thread) else: allow_unhide_thread(request.user, thread) moderation.unhide_thread(request, thread) return {'is_hidden': thread.is_hidden}
def post_save(self, serializer): if self.thread.category.acl['can_hide_threads']: try: hide = bool(self.request.data['hide']) except (TypeError, ValueError): hide = False if hide: moderation.hide_thread(self.request, self.thread) self.thread.update_all = True self.thread.save(update_fields=['is_hidden']) self.thread.category.synchronize() self.thread.category.update_all = True
def test_unhide_thread(self): """unhide_thread unhides thread""" moderation.hide_thread(self.request, self.thread) self.reload_thread() self.assertTrue(self.thread.is_hidden) self.assertTrue(moderation.unhide_thread(self.request, self.thread)) self.reload_thread() self.assertFalse(self.thread.is_hidden) event = self.thread.last_post self.assertTrue(event.is_event) self.assertEqual(event.event_type, 'unhid')
def test_unhide_thread(self): """unhide_thread unhides thread""" moderation.hide_thread(self.user, self.thread) self.reload_thread() self.assertTrue(self.thread.is_hidden) self.assertTrue(moderation.unhide_thread(self.user, self.thread)) self.reload_thread() self.assertFalse(self.thread.is_hidden) self.assertTrue(self.thread.has_events) event = self.thread.event_set.last() self.assertIn("made thread visible.", event.message) self.assertEqual(event.icon, "eye")
def test_hide_thread(self): """hide_thread hides thread""" self.assertFalse(self.thread.is_hidden) self.assertTrue(moderation.hide_thread(self.user, self.thread)) self.reload_thread() self.assertTrue(self.thread.is_hidden) self.assertTrue(self.thread.has_events) event = self.thread.event_set.last() self.assertIn("hidden thread.", event.message) self.assertEqual(event.icon, "eye-slash")
def action_hide(self, request, threads): changed_threads = 0 for thread in threads: if moderation.hide_thread(request.user, thread): changed_threads += 1 if changed_threads: with atomic(): self.forum.synchronize() self.forum.save() message = ungettext('%(changed)d thread was hidden.', '%(changed)d threads were hidden.', changed_threads) messages.success(request, message % {'changed': changed_threads}) else: message = _("No threads were hidden.") messages.info(request, message)
def action_hide(self, request, threads): changed_threads = 0 for thread in threads: if moderation.hide_thread(request.user, thread): changed_threads += 1 if changed_threads: with atomic(): self.forum.synchronize() self.forum.save() message = ungettext( '%(changed)d thread was hidden.', '%(changed)d threads were hidden.', changed_threads) messages.success(request, message % {'changed': changed_threads}) else: message = _("No threads were hidden.") messages.info(request, message)
def test_hide_hidden_thread(self): """hide_thread fails gracefully for hidden thread""" self.thread.is_hidden = True self.assertFalse(moderation.hide_thread(self.request, self.thread))
def test_hide_hidden_thread(self): """hide_thread fails gracefully for hidden thread""" self.thread.is_hidden = True self.assertFalse(moderation.hide_thread(self.user, self.thread))
def threads_merge_endpoint(request): serializer = MergeThreadsSerializer( data=request.data, context={'user': request.user}, ) serializer.is_valid(raise_exception=True) threads = serializer.validated_data['threads'] data = serializer.validated_data threads = data['threads'] new_thread = Thread( category=data['category'], started_on=threads[0].started_on, last_post_on=threads[0].last_post_on, ) new_thread.set_title(data['title']) new_thread.save() # handle merge conflict best_answer = data.get('best_answer') if best_answer: new_thread.best_answer_id = best_answer.best_answer_id new_thread.best_answer_is_protected = best_answer.best_answer_is_protected new_thread.best_answer_marked_on = best_answer.best_answer_marked_on new_thread.best_answer_marked_by_id = best_answer.best_answer_marked_by_id new_thread.best_answer_marked_by_name = best_answer.best_answer_marked_by_name new_thread.best_answer_marked_by_slug = best_answer.best_answer_marked_by_slug poll = data.get('poll') if poll: poll.move(new_thread) categories = [] for thread in threads: categories.append(thread.category) new_thread.merge(thread) thread.delete() record_event( request, new_thread, 'merged', {'merged_thread': thread.title}, commit=False, ) new_thread.synchronize() new_thread.save() if data.get('weight') == Thread.WEIGHT_GLOBAL: moderation.pin_thread_globally(request, new_thread) elif data.get('weight'): moderation.pin_thread_locally(request, new_thread) if data.get('is_hidden', False): moderation.hide_thread(request, new_thread) if data.get('is_closed', False): moderation.close_thread(request, new_thread) if new_thread.category not in categories: categories.append(new_thread.category) for category in categories: category.synchronize() category.save() # set extra attrs on thread for UI new_thread.is_read = False new_thread.subscription = None add_acl(request.user, new_thread) return Response(ThreadsListSerializer(new_thread).data)