def action_takeover(self, request, thread): participants.set_thread_owner(thread, request.user) messages.success(request, _("You are now owner of this thread.")) message = _("%(user)s took over this thread.") record_event(request.user, thread, 'user', message, { 'user': request.user, }) thread.save(update_fields=['has_events'])
def test_set_thread_owner(self): """set_thread_owner sets user as thread owner""" User = get_user_model() user = User.objects.create_user("Bob", "*****@*****.**", "Pass.123") set_thread_owner(self.thread, user) owner = self.thread.threadparticipant_set.get(is_owner=True) self.assertEqual(user, owner.user)
def test_set_thread_owner(self): """set_thread_owner sets user as thread owner""" User = get_user_model() user = User.objects.create_user( "Bob", "*****@*****.**", "Pass.123") set_thread_owner(self.thread, user) owner = self.thread.threadparticipant_set.get(is_owner=True) self.assertEqual(user, owner.user)
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 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_make_owner(self, request, thread, new_owner_id): new_owner_id = int(new_owner_id) new_owner = None for participant in thread.participants_list: if participant.user.id == int(new_owner_id): new_owner = participant.user break if new_owner: participants.set_thread_owner(thread, new_owner) message = _("You have passed thread ownership to %(user)s.") messages.success(request, message % {'user': new_owner.username}) message = _("%(user)s passed thread ownership to %(participant)s.") record_event(request.user, thread, 'user', message, { 'user': request.user, 'participant': new_owner }) thread.save(update_fields=['has_events'])