participants.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext as _
  3. from misago.core.mail import build_mail, send_messages
  4. from .events import record_event
  5. from .models import ThreadParticipant
  6. from .signals import remove_thread_participant
  7. def has_participants(thread):
  8. return thread.threadparticipant_set.exists()
  9. def make_participants_aware(user, thread):
  10. thread.participants_list = []
  11. thread.participant = None
  12. participants_qs = ThreadParticipant.objects.filter(thread=thread)
  13. participants_qs = participants_qs.select_related('user')
  14. for participant in participants_qs.order_by('-is_owner', 'user__slug'):
  15. participant.thread = thread
  16. thread.participants_list.append(participant)
  17. if participant.user == user:
  18. thread.participant = participant
  19. return thread.participants_list
  20. def set_owner(thread, user):
  21. """
  22. Remove user's ownership over thread
  23. """
  24. ThreadParticipant.objects.set_owner(thread, user)
  25. def set_users_unread_private_threads_sync(users):
  26. User = get_user_model()
  27. User.objects.filter(id__in=[u.pk for u in users]).update(
  28. sync_unread_private_threads=True
  29. )
  30. def add_participant(request, thread, user):
  31. """
  32. Adds single participant to thread, registers this on the event
  33. """
  34. add_participants(request, thread, [user])
  35. record_event(request, thread, 'added_participant', {
  36. 'user': {
  37. 'username': user.username,
  38. 'url': user.get_absolute_url(),
  39. }
  40. })
  41. def add_participants(request, thread, users):
  42. """
  43. Add multiple participants to thread, set "recound private threads" flag on them
  44. notify them about being added to thread
  45. """
  46. ThreadParticipant.objects.add_participants(thread, users)
  47. set_users_unread_private_threads_sync(users)
  48. emails = []
  49. for user in users:
  50. emails.append(build_noticiation_email(request, thread, user))
  51. send_messages(emails)
  52. def build_noticiation_email(request, thread, user):
  53. subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
  54. subject_formats = {
  55. 'thread': thread.title,
  56. 'user': request.user.username
  57. }
  58. return build_mail(
  59. request,
  60. user,
  61. subject % subject_formats,
  62. 'misago/emails/privatethread/added',
  63. {
  64. 'thread': thread
  65. }
  66. )
  67. def remove_participant(thread, user):
  68. """
  69. Remove thread participant, set "recound private threads" flag on user
  70. """
  71. thread.threadparticipant_set.filter(user=user).delete()
  72. set_users_unread_private_threads_sync([user])
  73. remove_thread_participant.send(thread, user=user)