participants.py 2.0 KB

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