# Path: open_connect/notifications/tasks.py
# @shared_task()
# def create_group_notifications(message_id):
#     """Create notifications for messages sent to a group."""
#     # Import here to avoid circular import
#     from open_connect.connectmessages.models import Message, UserThread
#     message = Message.objects.get(pk=message_id)
#     userthreads = UserThread.objects.filter(
#         thread_id=message.thread_id,
#         subscribed_email=True,
#         user__unsubscribed=False
#     ).exclude(
#         user_id=message.sender_id
#     )
# 
#     # User.objects.filter(userthread__thread=message.thread)
#     for userthread in userthreads:
#         create_group_notification.delay(message.pk, userthread.pk)
# 
# @shared_task()
# def send_immediate_notification(notification_id):
#     """Send an email for a given notification."""
#     notification = Notification.objects.select_related(
#         'recipient', 'message', 'message__thread').get(pk=notification_id)
#     recipient = notification.recipient
#     message = notification.message
#     context = {
#         'notification': notification,
#         'message': message,
#         'recipient': recipient,
#         'email': recipient.email
#     }
#     text = render_to_string(
#         'notifications/email/email_immediate.txt', context)
#     html = render_to_string(
#         'notifications/email/email_immediate.html', context)
# 
#     # Determine the correct format of the subject line of the notification
#     if message.thread.thread_type == 'direct':
#         subject_format = u"{subject}"
#     else:
#         subject_format = u"[{group}] {subject}"
#     subject = subject_format.format(
#         group=message.thread.group, subject=message.thread.subject)
# 
#     if message.sender.is_staff:
#         from_name = u'{name}, Staff Member, {brand}'.format(
#             name=message.sender.get_full_name(),
#             brand=settings.BRAND_TITLE)
#     else:
#         from_name = u'{name}, {brand}'.format(
#             name=message.sender.get_full_name(),
#             brand=settings.BRAND_TITLE)
#     from_email = formataddr((from_name, settings.DEFAULT_FROM_ADDRESS))
# 
#     to_email_tup = (recipient.get_full_name(), notification.recipient.email)
#     to_address = formataddr(to_email_tup)
# 
#     send_email(
#         email=to_address,
#         from_email=from_email,
#         subject=subject,
#         text=text,
#         html=html
#     )
# 
#     notification.consumed = True
#     notification.save()


from celery import shared_task
from django.conf import settings
from django.db import connection
from open_connect.notifications.tasks import (
    create_group_notifications, send_immediate_notification
)
    from open_connect.connectmessages.models import ImageAttachment
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Thread, Message
    from open_connect.notifications.models import Notification
    from open_connect.accounts.models import User
@shared_task(name='send-system-message')
def send_system_message(recipient, subject, message_content):
    """Send a direct message to a user coming from the system user"""

    # Handle calls where we're passed the user_id instead of a User object.
    if not isinstance(recipient, User):
        recipient = User.objects.get(pk=recipient)

    sysuser = User.objects.get(email=settings.SYSTEM_USER_EMAIL)
    thread = Thread.objects.create(
        subject=subject[:100],
        thread_type='direct',
        closed=True
    )
    message = Message(
        thread=thread,
        sender=sysuser,
        text=message_content
    )
    message.save(shorten=False)
    thread.add_user_to_thread(recipient)

    # Use BeautifulSoup to remove any HTML from the message to make the
    # plaintext email version of the message
    notification, created = Notification.objects.get_or_create(
        recipient_id=recipient.pk,
        message=message
    )

    if created and recipient.group_notification_period == 'immediate':

# Path: open_connect/notifications/tasks.py
# @shared_task()
# def create_group_notifications(message_id):
#     """Create notifications for messages sent to a group."""
#     # Import here to avoid circular import
#     from open_connect.connectmessages.models import Message, UserThread
#     message = Message.objects.get(pk=message_id)
#     userthreads = UserThread.objects.filter(
#         thread_id=message.thread_id,
#         subscribed_email=True,
#         user__unsubscribed=False
#     ).exclude(
#         user_id=message.sender_id
#     )
# 
#     # User.objects.filter(userthread__thread=message.thread)
#     for userthread in userthreads:
#         create_group_notification.delay(message.pk, userthread.pk)
# 
# @shared_task()
# def send_immediate_notification(notification_id):
#     """Send an email for a given notification."""
#     notification = Notification.objects.select_related(
#         'recipient', 'message', 'message__thread').get(pk=notification_id)
#     recipient = notification.recipient
#     message = notification.message
#     context = {
#         'notification': notification,
#         'message': message,
#         'recipient': recipient,
#         'email': recipient.email
#     }
#     text = render_to_string(
#         'notifications/email/email_immediate.txt', context)
#     html = render_to_string(
#         'notifications/email/email_immediate.html', context)
# 
#     # Determine the correct format of the subject line of the notification
#     if message.thread.thread_type == 'direct':
#         subject_format = u"{subject}"
#     else:
#         subject_format = u"[{group}] {subject}"
#     subject = subject_format.format(
#         group=message.thread.group, subject=message.thread.subject)
# 
#     if message.sender.is_staff:
#         from_name = u'{name}, Staff Member, {brand}'.format(
#             name=message.sender.get_full_name(),
#             brand=settings.BRAND_TITLE)
#     else:
#         from_name = u'{name}, {brand}'.format(
#             name=message.sender.get_full_name(),
#             brand=settings.BRAND_TITLE)
#     from_email = formataddr((from_name, settings.DEFAULT_FROM_ADDRESS))
# 
#     to_email_tup = (recipient.get_full_name(), notification.recipient.email)
#     to_address = formataddr(to_email_tup)
# 
#     send_email(
#         email=to_address,
#         from_email=from_email,
#         subject=subject,
#         text=text,
#         html=html
#     )
# 
#     notification.consumed = True
#     notification.save()


# Path: open_connect/connectmessages/tasks.py
from celery import shared_task
from django.conf import settings
from django.db import connection
from open_connect.notifications.tasks import (
    create_group_notifications, send_immediate_notification
)
    from open_connect.connectmessages.models import ImageAttachment
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Thread, Message
    from open_connect.notifications.models import Notification
    from open_connect.accounts.models import User

@shared_task(name='send-system-message')
def send_system_message(recipient, subject, message_content):
    """Send a direct message to a user coming from the system user"""

    # Handle calls where we're passed the user_id instead of a User object.
    if not isinstance(recipient, User):
        recipient = User.objects.get(pk=recipient)

    sysuser = User.objects.get(email=settings.SYSTEM_USER_EMAIL)
    thread = Thread.objects.create(
        subject=subject[:100],
        thread_type='direct',
        closed=True
    )
    message = Message(
        thread=thread,
        sender=sysuser,
        text=message_content
    )
    message.save(shorten=False)
    thread.add_user_to_thread(recipient)

    # Use BeautifulSoup to remove any HTML from the message to make the
    # plaintext email version of the message
    notification, created = Notification.objects.get_or_create(
        recipient_id=recipient.pk,
        message=message
    )

    if created and recipient.group_notification_period == 'immediate':

        send_immediate_notification.delay(notification.pk)