participants.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from django.utils.translation import ugettext as _
  2. from misago.notifications import notify_user
  3. from misago.threads.models import ThreadParticipant
  4. from misago.threads.signals import remove_thread_participant
  5. def thread_has_participants(thread):
  6. return thread.threadparticipant_set.exists()
  7. def make_thread_participants_aware(user, thread):
  8. thread.participants_list = []
  9. thread.participant = None
  10. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  11. participants_qs = participants_qs.select_related('user')
  12. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  13. participant.thread = thread
  14. thread.participants_list.append(participant)
  15. if participant.user == user:
  16. thread.participant = participant
  17. return thread.participants_list
  18. def set_thread_owner(thread, user):
  19. ThreadParticipant.objects.set_owner(thread, user)
  20. def set_user_unread_private_threads_sync(user):
  21. user.sync_unread_private_threads = True
  22. user.save(update_fields=['sync_unread_private_threads'])
  23. def add_participant(request, thread, user):
  24. """
  25. Add participant to thread, set "recound private threads" flag on user,
  26. notify user about being added to thread and mail him about it
  27. """
  28. ThreadParticipant.objects.add_participant(thread, user)
  29. notify_user(
  30. user,
  31. _("%(user)s added you to %(thread)s private thread."),
  32. thread.get_new_reply_url(),
  33. 'see_thread_%s' % thread.pk,
  34. {'user': request.user.username, 'thread': thread.title},
  35. request.user)
  36. set_user_unread_private_threads_sync(user)
  37. def add_owner(thread, user):
  38. """
  39. Add owner to thread, set "recound private threads" flag on user,
  40. notify user about being added to thread
  41. """
  42. ThreadParticipant.objects.add_participant(thread, user, True)
  43. set_user_unread_private_threads_sync(user)
  44. def remove_participant(thread, user):
  45. """
  46. Remove thread participant, set "recound private threads" flag on user
  47. """
  48. thread.threadparticipant_set.filter(user=user).delete()
  49. set_user_unread_private_threads_sync(user)
  50. remove_thread_participant.send(thread, user=user)