def test_make_threads_participants_aware(self): """ make_participants_aware sets participants_list and participant annotations on list of threads """ user = UserModel.objects.create_user("Bob", "*****@*****.**", "Pass.123") other_user = UserModel.objects.create_user("Bob2", "*****@*****.**", "Pass.123") self.assertFalse(hasattr(self.thread, 'participants_list')) self.assertFalse(hasattr(self.thread, 'participant')) make_participants_aware(user, [self.thread]) self.assertFalse(hasattr(self.thread, 'participants_list')) self.assertTrue(hasattr(self.thread, 'participant')) self.assertIsNone(self.thread.participant) ThreadParticipant.objects.set_owner(self.thread, user) ThreadParticipant.objects.add_participants(self.thread, [other_user]) make_participants_aware(user, [self.thread]) self.assertFalse(hasattr(self.thread, 'participants_list')) self.assertEqual(self.thread.participant.user, user)
def test_make_thread_participants_aware(self): """ make_participants_aware sets participants_list and participant annotations on thread model """ user = UserModel.objects.create_user("Bob", "*****@*****.**", "Pass.123") other_user = UserModel.objects.create_user("Bob2", "*****@*****.**", "Pass.123") self.assertFalse(hasattr(self.thread, 'participants_list')) self.assertFalse(hasattr(self.thread, 'participant')) make_participants_aware(user, self.thread) self.assertTrue(hasattr(self.thread, 'participants_list')) self.assertTrue(hasattr(self.thread, 'participant')) self.assertEqual(self.thread.participants_list, []) self.assertIsNone(self.thread.participant) ThreadParticipant.objects.set_owner(self.thread, user) ThreadParticipant.objects.add_participants(self.thread, [other_user]) make_participants_aware(user, self.thread) self.assertEqual(self.thread.participant.user, user) for participant in self.thread.participants_list: if participant.user == user: break else: self.fail("thread.participants_list didn't contain user")
def patch_add_participant(request, thread, value): allow_add_participants(request.user, thread) try: username = six.text_type(value).strip().lower() if not username: raise ValidationError( _("You have to enter new participant's username.")) participant = UserModel.objects.get(slug=username) except UserModel.DoesNotExist: raise ValidationError(_("No user with such name exists.")) if participant in [p.user for p in thread.participants_list]: raise ValidationError(_("This user is already thread participant.")) max_participants = request.user.acl_cache[ 'max_private_thread_participants'] if max_participants: current_participants = len(thread.participants_list) if current_participants >= max_participants: raise ValidationError( _("You can't add any more new users to this thread.")) try: allow_add_participant(request.user, participant) except PermissionDenied as e: raise ValidationError(six.text_type(e)) add_participant(request, thread, participant) make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return {'participants': participants.data}
def patch_remove_participant(request, thread, value): try: user_id = int(value) except (ValueError, TypeError): raise PermissionDenied(_("A valid integer is required.")) for participant in thread.participants_list: if participant.user_id == user_id: break else: raise PermissionDenied(_("Participant doesn't exist.")) allow_remove_participant(request.user, thread, participant.user) remove_participant(request, thread, participant.user) if len(thread.participants_list) == 1: return {'deleted': True} else: make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return { 'deleted': False, 'participants': participants.data, }
def patch_remove_participant(request, thread, value): try: user_id = int(value) except (ValueError, TypeError): user_id = 0 for participant in thread.participants_list: if participant.user_id == user_id: break else: raise PermissionDenied(_("Participant doesn't exist.")) allow_remove_participant(request.user, thread, participant.user) remove_participant(request, thread, participant.user) if len(thread.participants_list) == 1: return {'deleted': True} else: make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return { 'deleted': False, 'participants': participants.data, }
def patch_add_participant(request, thread, value): allow_add_participants(request.user, thread) try: username = six.text_type(value).strip().lower() if not username: raise PermissionDenied( _("You have to enter new participant's username.")) participant = UserModel.objects.get(slug=username) except UserModel.DoesNotExist: raise PermissionDenied(_("No user with such name exists.")) if participant in [p.user for p in thread.participants_list]: raise PermissionDenied(_("This user is already thread participant.")) allow_add_participant(request.user, participant) add_participant(request, thread, participant) make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer( thread.participants_list, many=True) return { 'participants': participants.data }
def get_thread(self, request, pk, slug=None): allow_use_private_threads(request.user) thread = get_object_or_404( Thread.objects.select_related(*BASE_RELATIONS), pk=pk, category__tree_id=trees_map.get_tree_id_for_root(PRIVATE_THREADS_ROOT_NAME), ) make_participants_aware(request.user, thread) allow_see_private_thread(request.user, thread) if slug: validate_slug(thread, slug) return thread
def test_anonymize_added_participant_event(self): """added participant event is anonymized by user.anonymize_content""" user = get_mock_user() request = self.get_request() set_owner(self.thread, self.user) make_participants_aware(self.user, self.thread) add_participant(request, self.thread, user) user.anonymize_content() event = Post.objects.get(event_type='added_participant') self.assertEqual(event.event_context, { 'user': { 'id': None, 'username': user.username, 'url': reverse('misago:index'), }, })
def patch_replace_owner(request, thread, value): try: user_id = int(value) except (ValueError, TypeError): user_id = 0 for participant in thread.participants_list: if participant.user_id == user_id: if participant.is_owner: raise PermissionDenied(_("This user already is thread owner.")) else: break else: raise PermissionDenied(_("Participant doesn't exist.")) allow_change_owner(request.user, thread) change_owner(request, thread, participant.user) make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return {'participants': participants.data}
def patch_add_participant(request, thread, value): allow_add_participants(request.user, thread) try: username = six.text_type(value).strip().lower() if not username: raise PermissionDenied(_("You have to enter new participant's username.")) participant = UserModel.objects.get(slug=username) except UserModel.DoesNotExist: raise PermissionDenied(_("No user with such name exists.")) if participant in [p.user for p in thread.participants_list]: raise PermissionDenied(_("This user is already thread participant.")) allow_add_participant(request.user, participant) add_participant(request, thread, participant) make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return {'participants': participants.data}
def patch_replace_owner(request, thread, value): try: user_id = int(value) except (TypeError, ValueError): raise ValidationError(_("A valid integer is required.")) for participant in thread.participants_list: if participant.user_id == user_id: if participant.is_owner: raise ValidationError(_("This user already is thread owner.")) else: break else: raise ValidationError(_("Participant doesn't exist.")) allow_change_owner(request.user, thread) change_owner(request, thread, participant.user) make_participants_aware(request.user, thread) participants = ThreadParticipantSerializer(thread.participants_list, many=True) return {'participants': participants.data}
def filter_threads(self, request, threads): make_participants_aware(request.user, threads)
def filter_threads(self, request, threads): make_participants_aware(request.user, threads)