# 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
                -- notifications_subscription table and the same data exists
                -- in both tables
                FROM notifications_subscription s

                WHERE
                    s.group_id = %s
                    AND
                    -- In order to prevent an IntegrityError, grab all users
                    -- with existing UserThread object and exclude them from
                    -- this insert. The only users who should have a UserThread
                    -- is the author and anyone who joined to the group between
                    -- the time the author posted the thread and the time this
                    -- task runs (in other words, not many users)
                    s.user_id NOT IN 
                        (SELECT user_id 
                        FROM connectmessages_userthread
                        WHERE thread_id = %s)
                """, [
                    thread.pk,
                    thread.group.pk,
                    thread.pk])

    else:
        UserThread.objects.filter(
            thread=thread, read=True).exclude(user=sender).update(read=False)
        UserThread.objects.filter(
            thread=thread, status='archived'
        ).exclude(user=sender).update(status='active')

    if thread.thread_type == 'group':

# 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

                -- notifications_subscription table and the same data exists
                -- in both tables
                FROM notifications_subscription s

                WHERE
                    s.group_id = %s
                    AND
                    -- In order to prevent an IntegrityError, grab all users
                    -- with existing UserThread object and exclude them from
                    -- this insert. The only users who should have a UserThread
                    -- is the author and anyone who joined to the group between
                    -- the time the author posted the thread and the time this
                    -- task runs (in other words, not many users)
                    s.user_id NOT IN 
                        (SELECT user_id 
                        FROM connectmessages_userthread
                        WHERE thread_id = %s)
                """, [
                    thread.pk,
                    thread.group.pk,
                    thread.pk])

    else:
        UserThread.objects.filter(
            thread=thread, read=True).exclude(user=sender).update(read=False)
        UserThread.objects.filter(
            thread=thread, status='archived'
        ).exclude(user=sender).update(status='active')

    if thread.thread_type == 'group':

        create_group_notifications.delay(message_id)