12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- from django.contrib.auth import get_user_model
- from django.utils.translation import ugettext as _
- from misago.core.mail import build_mail, send_messages
- from .models import ThreadParticipant
- from .signals import remove_thread_participant
- def has_participants(thread):
- return thread.threadparticipant_set.exists()
- def make_participants_aware(user, thread):
- thread.participants_list = []
- thread.participant = None
- participants_qs = ThreadParticipant.objects.filter(thread=thread)
- participants_qs = participants_qs.select_related('user')
- for participant in participants_qs.order_by('-is_owner', 'user__slug'):
- participant.thread = thread
- thread.participants_list.append(participant)
- if participant.user == user:
- thread.participant = participant
- return thread.participants_list
- def set_owner(thread, user):
- """
- Remove user's ownership over thread
- """
- ThreadParticipant.objects.set_owner(thread, user)
- def set_users_unread_private_threads_sync(users):
- User = get_user_model()
- User.objects.filter(id__in=[u.pk for u in users]).update(
- sync_unread_private_threads=True
- )
- def add_participant(request, thread, user):
- """
- Shortcut for adding single participant to thread
- """
- add_participants(request, thread, [user])
- def add_participants(request, thread, users):
- """
- Add multiple participants to thread, set "recound private threads" flag on them
- notify them about being added to thread
- """
- ThreadParticipant.objects.add_participants(thread, users)
- set_users_unread_private_threads_sync(users)
- emails = []
- for user in users:
- emails.append(build_noticiation_email(request, thread, user))
- send_messages(emails)
- def build_noticiation_email(request, thread, user):
- subject = _('%(user)s has invited you to participate in private thread "%(thread)s"')
- subject_formats = {
- 'thread': thread.title,
- 'user': request.user.username
- }
- return build_mail(
- request,
- user,
- subject % subject_formats,
- 'misago/emails/privatethread/added',
- {
- 'thread': thread
- }
- )
- def remove_participant(thread, user):
- """
- Remove thread participant, set "recound private threads" flag on user
- """
- thread.threadparticipant_set.filter(user=user).delete()
- set_users_unread_private_threads_sync([user])
- remove_thread_participant.send(thread, user=user)
|