def test_thread_has_participants(self): """thread_has_participants returns true if thread has participants""" User = get_user_model() user = User.objects.create_user("Bob", "*****@*****.**", "Pass.123") other_user = User.objects.create_user("Bob2", "*****@*****.**", "Pass.123") self.assertFalse(thread_has_participants(self.thread)) ThreadParticipant.objects.add_participant(self.thread, user) self.assertTrue(thread_has_participants(self.thread)) ThreadParticipant.objects.add_participant(self.thread, other_user) self.assertTrue(thread_has_participants(self.thread)) self.thread.threadparticipant_set.all().delete() self.assertFalse(thread_has_participants(self.thread))
def test_thread_has_participants(self): """thread_has_participants returns true if thread has participants""" User = get_user_model() user = User.objects.create_user( "Bob", "*****@*****.**", "Pass.123") other_user = User.objects.create_user( "Bob2", "*****@*****.**", "Pass.123") self.assertFalse(thread_has_participants(self.thread)) ThreadParticipant.objects.add_participant(self.thread, user) self.assertTrue(thread_has_participants(self.thread)) ThreadParticipant.objects.add_participant(self.thread, other_user) self.assertTrue(thread_has_participants(self.thread)) self.thread.threadparticipant_set.all().delete() self.assertFalse(thread_has_participants(self.thread))
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})