participants.py 2.5 KB

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