# Path: open_connect/notifications/models.py
# class Notification(TimestampModel):
#     """Information about an individual notification."""
#     recipient = models.ForeignKey(settings.AUTH_USER_MODEL)
#     consumed = models.BooleanField(default=False, db_index=True)
#     subscription = models.ForeignKey(Subscription, blank=True, null=True)
#     message = models.ForeignKey('connectmessages.Message')
# 
#     class Meta(object):
#         """Meta options for Notification model."""
#         # There should only ever be 1 notification per message
#         unique_together = ['recipient', 'message']


from datetime import timedelta
from email.utils import formataddr
from celery import shared_task
from django.conf import settings
from django.db import IntegrityError
from django.db.models import Q
from django.utils.dateparse import parse_datetime
from django.utils.timezone import now
from django.utils.translation import ngettext
from django.template.loader import render_to_string
from open_connect.mailer.utils import send_email
from open_connect.notifications.models import Notification
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Message
    from open_connect.accounts.models import User
    from open_connect.accounts.models import User
    from open_connect.accounts.models import User
import logging
        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 create_group_notification(message_id, userthread_id):
    """Create a notification for a specific user"""
    # Import here to avoid circular import
    message = Message.objects.select_related('thread').get(pk=message_id)
    userthread = UserThread.objects.select_related('user').extra(
        select={
            'subscription_id': 'notifications_subscription.id',
            'subscription_period': 'notifications_subscription.period'
        },
        tables=['notifications_subscription'],
        where=[
            'notifications_subscription.user_id=accounts_user.id',
            'notifications_subscription.group_id=%s'
        ],
        params=[message.thread.group_id]
    ).get(id=userthread_id)

    try:

# Path: open_connect/notifications/models.py
# class Notification(TimestampModel):
#     """Information about an individual notification."""
#     recipient = models.ForeignKey(settings.AUTH_USER_MODEL)
#     consumed = models.BooleanField(default=False, db_index=True)
#     subscription = models.ForeignKey(Subscription, blank=True, null=True)
#     message = models.ForeignKey('connectmessages.Message')
# 
#     class Meta(object):
#         """Meta options for Notification model."""
#         # There should only ever be 1 notification per message
#         unique_together = ['recipient', 'message']


# Path: open_connect/notifications/tasks.py
from datetime import timedelta
from email.utils import formataddr
from celery import shared_task
from django.conf import settings
from django.db import IntegrityError
from django.db.models import Q
from django.utils.dateparse import parse_datetime
from django.utils.timezone import now
from django.utils.translation import ngettext
from django.template.loader import render_to_string
from open_connect.mailer.utils import send_email
from open_connect.notifications.models import Notification
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Message, UserThread
    from open_connect.connectmessages.models import Message
    from open_connect.accounts.models import User
    from open_connect.accounts.models import User
    from open_connect.accounts.models import User
import logging

        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 create_group_notification(message_id, userthread_id):
    """Create a notification for a specific user"""
    # Import here to avoid circular import
    message = Message.objects.select_related('thread').get(pk=message_id)
    userthread = UserThread.objects.select_related('user').extra(
        select={
            'subscription_id': 'notifications_subscription.id',
            'subscription_period': 'notifications_subscription.period'
        },
        tables=['notifications_subscription'],
        where=[
            'notifications_subscription.user_id=accounts_user.id',
            'notifications_subscription.group_id=%s'
        ],
        params=[message.thread.group_id]
    ).get(id=userthread_id)

    try:

        notification = Notification.objects.create(