participants.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from django.utils.translation import ugettext as _
  2. from misago.core.mail import mail_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. set_user_unread_private_threads_sync(user)
  30. mail_subject = _("%(thread)s - %(user)s added you to private thread")
  31. subject_formats = {'thread': thread.title, 'user': request.user.username}
  32. mail_user(request, user, mail_subject % subject_formats,
  33. 'misago/emails/privatethread/added',
  34. {'thread': thread})
  35. def add_owner(thread, user):
  36. """
  37. Add owner to thread, set "recound private threads" flag on user,
  38. notify user about being added to thread
  39. """
  40. ThreadParticipant.objects.add_participant(thread, user, True)
  41. set_user_unread_private_threads_sync(user)
  42. def remove_participant(thread, user):
  43. """
  44. Remove thread participant, set "recound private threads" flag on user
  45. """
  46. thread.threadparticipant_set.filter(user=user).delete()
  47. set_user_unread_private_threads_sync(user)
  48. remove_thread_participant.send(thread, user=user)