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 post_save(self, serializer): allowed_pin = self.thread.category.acl['can_pin_threads'] if allowed_pin > 0: pin = serializer.validated_data['pin'] if pin <= allowed_pin: if pin == Thread.WEIGHT_GLOBAL: moderation.pin_thread_globally(self.request, self.thread) elif pin == Thread.WEIGHT_PINNED: moderation.pin_thread_locally(self.request, self.thread)
def post_save(self, serializer): allowed_pin = self.thread.category.acl['can_pin_threads'] if allowed_pin > 0: try: pin = int(self.request.data['pin']) except (TypeError, ValueError): pin = 0 if pin <= allowed_pin: if pin == Thread.WEIGHT_GLOBAL: moderation.pin_thread_globally(self.request, self.thread) elif pin == Thread.WEIGHT_PINNED: moderation.pin_thread_locally(self.request, self.thread)
def test_unpin_locally_pinned_thread(self): """unpin_thread unpins locally pinned thread""" moderation.pin_thread_locally(self.request, self.thread) self.assertEqual(self.thread.weight, 1) self.assertTrue(moderation.unpin_thread(self.request, self.thread)) self.reload_thread() self.assertEqual(self.thread.weight, 0) event = self.thread.last_post self.assertTrue(event.is_event) self.assertEqual(event.event_type, 'unpinned')
def test_unpin_locally_pinned_thread(self): """unpin_thread unpins locally pinned thread""" moderation.pin_thread_locally(self.user, self.thread) self.assertEqual(self.thread.weight, 1) self.assertTrue(moderation.unpin_thread(self.user, self.thread)) self.reload_thread() self.assertEqual(self.thread.weight, 0) self.assertTrue(self.thread.has_events) event = self.thread.event_set.last() self.assertIn("unpinned thread.", event.message) self.assertEqual(event.icon, "circle")
def test_pin_invalid_thread(self): """pin_thread_locally returns false for already locally pinned thread""" self.thread.weight = 1 self.assertFalse( moderation.pin_thread_locally(self.request, self.thread)) self.assertEqual(self.thread.weight, 1)
def test_pin_invalid_thread(self): """ pin_thread_locally returns false for already locally pinned thread """ self.thread.weight = 1 self.assertFalse(moderation.pin_thread_locally(self.user, self.thread)) self.assertEqual(self.thread.weight, 1)
def patch_weight(request, thread, value): allow_pin_thread(request.user, thread) if not thread.acl.get('can_pin_globally') and thread.weight == 2: raise PermissionDenied(_("You can't change globally pinned threads weights in this category.")) if value == 2: if thread.acl.get('can_pin_globally'): moderation.pin_thread_globally(request, thread) else: raise PermissionDenied(_("You can't pin threads globally in this category.")) elif value == 1: moderation.pin_thread_locally(request, thread) elif value == 0: moderation.unpin_thread(request, thread) return {'weight': thread.weight}
def test_pin_thread_locally(self): """pin_thread_locally makes thread pinned locally""" self.assertEqual(self.thread.weight, 0) self.assertTrue(moderation.pin_thread_locally(self.request, self.thread)) self.reload_thread() self.assertEqual(self.thread.weight, 1) event = self.thread.last_post self.assertTrue(event.is_event) self.assertEqual(event.event_type, 'pinned_locally')
def test_pin_thread_locally(self): """pin_thread_locally makes thread pinned locally""" self.assertEqual(self.thread.weight, 0) self.assertTrue(moderation.pin_thread_locally(self.user, self.thread)) self.reload_thread() self.assertEqual(self.thread.weight, 1) self.assertTrue(self.thread.has_events) event = self.thread.event_set.last() self.assertEqual(event.icon, "bookmark") self.assertIn("pinned thread locally.", event.message)
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)