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_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 test_remove_participant(self): """remove_participant removes user from thread""" User = get_user_model() user = User.objects.create_user("Bob", "*****@*****.**", "Pass.123") add_owner(self.thread, user) remove_participant(self.thread, user) with self.assertRaises(ThreadParticipant.DoesNotExist): self.thread.threadparticipant_set.get(user=user) set_user_unread_private_threads_sync(user) self.assertTrue(user.sync_unread_private_threads) db_user = User.objects.get(pk=user.pk) self.assertTrue(db_user.sync_unread_private_threads)
def dispatch(self, request, *args, **kwargs): thread = self.get_thread(request, lock=True, **kwargs) try: if not request.method == "POST": raise RuntimeError(_("Wrong action received.")) if not thread.participant: raise RuntimeError( _("You have to be thread participant in " "order to be able to leave thread.")) user_qs = thread.threadparticipant_set.select_related('user') try: participant = user_qs.get(user_id=request.user.id) except ThreadParticipant.DoesNotExist: raise RuntimeError( _("You need to be thread " "participant to leave it.")) except RuntimeError as e: messages.error(request, unicode(e)) return redirect(thread.get_absolute_url()) participants.remove_participant(thread, request.user) if not thread.threadparticipant_set.exists(): thread.delete() elif thread.participant.is_owner: new_owner = user_qs.order_by('id')[:1][0].user participants.set_thread_owner(thread, new_owner) message = _("%(user)s left this thread. " "%(new_owner)s is now thread owner.") record_event(request.user, thread, 'user', message, { 'user': request.user, 'new_owner': new_owner }) thread.save(update_fields=['has_events']) else: message = _("%(user)s left this thread.") record_event(request.user, thread, 'user', message, { 'user': request.user, }) thread.save(update_fields=['has_events']) message = _('You have left "%(thread)s" thread.') message = message % {'thread': thread.title} messages.info(request, message) return redirect('misago:private_threads')
def action(self, request, thread, kwargs): user_qs = thread.threadparticipant_set.select_related('user') try: participant = user_qs.get(user_id=kwargs['user_id']) except ThreadParticipant.DoesNotExist: return JsonResponse({ 'message': _("Requested participant couldn't be found."), 'is_error': True, }) if participant.user == request.user: return JsonResponse({ 'message': _('To leave thread use "Leave thread" option.'), 'is_error': True, }) participants_count = len(thread.participants_list) - 1 if participants_count == 0: return JsonResponse({ 'message': _("You can't remove last thread participant."), 'is_error': True, }) participants.remove_participant(thread, participant.user) if not participants.thread_has_participants(thread): thread.delete() else: message = _("%(user)s removed %(participant)s from this thread.") record_event(request.user, thread, 'user', message, { 'user': request.user, 'participant': participant.user }) thread.save(update_fields=['has_events']) participants_count = len(thread.participants_list) - 1 message = ungettext("%(users)s participant", "%(users)s participants", participants_count) message = message % {'users': participants_count} return JsonResponse({'is_error': False, 'message': message})
def test_remove_participant(self): """remove_participant removes user from thread""" User = get_user_model() user = User.objects.create_user( "Bob", "*****@*****.**", "Pass.123") add_owner(self.thread, user) remove_participant(self.thread, user) with self.assertRaises(ThreadParticipant.DoesNotExist): self.thread.threadparticipant_set.get(user=user) set_user_unread_private_threads_sync(user) self.assertTrue(user.sync_unread_private_threads) db_user = User.objects.get(pk=user.pk) self.assertTrue(db_user.sync_unread_private_threads)
def dispatch(self, request, *args, **kwargs): thread = self.get_thread(request, lock=True, **kwargs) try: if not request.method == "POST": raise RuntimeError(_("Wrong action received.")) if not thread.participant: raise RuntimeError(_("You have to be thread participant in " "order to be able to leave thread.")) user_qs = thread.threadparticipant_set.select_related('user') try: participant = user_qs.get(user_id=request.user.id) except ThreadParticipant.DoesNotExist: raise RuntimeError(_("You need to be thread " "participant to leave it.")) except RuntimeError as e: messages.error(request, unicode(e)) return redirect(thread.get_absolute_url()) participants.remove_participant(thread, request.user) if not thread.threadparticipant_set.exists(): thread.delete() elif thread.participant.is_owner: new_owner = user_qs.order_by('id')[:1][0].user participants.set_thread_owner(thread, new_owner) message = _("%(user)s left this thread. " "%(new_owner)s is now thread owner.") record_event(request.user, thread, 'user', message, { 'user': request.user, 'new_owner': new_owner }) thread.save(update_fields=['has_events']) else: message = _("%(user)s left this thread.") record_event(request.user, thread, 'user', message, { 'user': request.user, }) thread.save(update_fields=['has_events']) message = _('You have left "%(thread)s" thread.') message = message % {'thread': thread.title} messages.info(request, message) return redirect('misago:private_threads')
def test_anonymize_owner_left_event(self): """owner left event is anonymized by user.anonymize_content""" user = get_mock_user() request = self.get_request(user) set_owner(self.thread, user) make_participants_aware(user, self.thread) add_participant(request, self.thread, self.user) make_participants_aware(user, self.thread) remove_participant(request, self.thread, user) user.anonymize_content() event = Post.objects.get(event_type='owner_left') self.assertEqual(event.event_context, { 'user': { 'id': None, 'username': user.username, 'url': reverse('misago:index'), }, })