# Path: open_connect/notifications/models.py
# class Subscription(TimestampModel):
#     """Model for tracking a user's subscription preferences to a group."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name='subscriptions')
#     period = models.CharField(
#         max_length=30, choices=NOTIFICATION_PERIODS, default='immediate',
#         db_index=True)
#     group = models.ForeignKey('groups.Group')
# 
#     class Meta(object):
#         """Meta options for Subcription model."""
#         unique_together = ['user', 'group']
# 
#     def save(self, *args, **kwargs):
#         """Save method for Subscription"""
#         # Importing from models to address circular import
#         from open_connect.connectmessages.models import UserThread
#         # Check to see if there is already a primary key (aka, this is an edit)
#         if self.pk:
#             # Get the original version of this record
#             orig = Subscription.objects.get(pk=self.pk)
#             # Check to see if the version has changed to none
#             if self.period == 'none' and orig.period != 'none':
#                 # If the original record was not none (aka the period has
#                 # changed to 'none' from something else) update all UserThreads
#                 UserThread.objects.filter(
#                     thread__group_id=self.group_id,
#                     user_id=self.user_id,
#                     subscribed_email=True
#                 ).update(subscribed_email=False)
#             elif orig.period == 'none':
#                 UserThread.objects.filter(
#                     thread__group_id=self.group_id,
#                     user_id=self.user_id,
#                     subscribed_email=False
#                 ).update(subscribed_email=True)
# 
#         return super(Subscription, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         """Unicode representation of the model."""
#         return u'Subscription to {group} for {user}.'.format(
#             group=self.group, user=self.user)
# 
# Path: open_connect/connectmessages/tasks.py
# def import_image_attachment():
# def process_image_attachment(image_id):
# def send_message(message_id, shorten=True):
# def send_system_message(recipient, subject, message_content):


from bs4 import BeautifulSoup
from datetime import timedelta
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import models
from django.db.models import Q, ObjectDoesNotExist
from django.utils.encoding import smart_text
from django.utils.timezone import now
from unidecode import unidecode
from open_connect.media.models import Image, ShortenedURL
from open_connect.notifications.models import Subscription
from open_connect.connectmessages import tasks
from open_connect.connect_core.utils.models import TimestampModel
from open_connect.connect_core.utils.stringhelp import unicode_or_empty
import logging
import re
import pytz
        return "Thread %s" % self.subject

    @property
    def is_system_thread(self):
        """Returns true if this is a thread started by the system message"""
        return self.first_message.is_system_message

    def get_absolute_url(self):
        """Return absolute URL for a thread"""
        return reverse('thread', args=(self.pk,))

    def get_unsubscribe_url(self):
        """Return the URL of the unsubscribe view"""
        return reverse('thread_unsubscribe', args=(self.pk,))

    def add_user_to_thread(self, user):
        """Add a user to a thread."""
        defaults = {}
        if self.thread_type == 'group':
            # This is put inside a try/except because it is possible for staff
            # to send messages to groups they are not members of and thus
            # do not have a subscription to.
            try:
                subscription = Subscription.objects.get(
                    user=user, group=self.group)

                # By default everyone is subscribed to new threads
                # If this user has determined to not subscribe, unmark them
                if subscription.period == 'none':
                    defaults['subscribed_email'] = False

# Path: open_connect/notifications/models.py
# class Subscription(TimestampModel):
#     """Model for tracking a user's subscription preferences to a group."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name='subscriptions')
#     period = models.CharField(
#         max_length=30, choices=NOTIFICATION_PERIODS, default='immediate',
#         db_index=True)
#     group = models.ForeignKey('groups.Group')
# 
#     class Meta(object):
#         """Meta options for Subcription model."""
#         unique_together = ['user', 'group']
# 
#     def save(self, *args, **kwargs):
#         """Save method for Subscription"""
#         # Importing from models to address circular import
#         from open_connect.connectmessages.models import UserThread
#         # Check to see if there is already a primary key (aka, this is an edit)
#         if self.pk:
#             # Get the original version of this record
#             orig = Subscription.objects.get(pk=self.pk)
#             # Check to see if the version has changed to none
#             if self.period == 'none' and orig.period != 'none':
#                 # If the original record was not none (aka the period has
#                 # changed to 'none' from something else) update all UserThreads
#                 UserThread.objects.filter(
#                     thread__group_id=self.group_id,
#                     user_id=self.user_id,
#                     subscribed_email=True
#                 ).update(subscribed_email=False)
#             elif orig.period == 'none':
#                 UserThread.objects.filter(
#                     thread__group_id=self.group_id,
#                     user_id=self.user_id,
#                     subscribed_email=False
#                 ).update(subscribed_email=True)
# 
#         return super(Subscription, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         """Unicode representation of the model."""
#         return u'Subscription to {group} for {user}.'.format(
#             group=self.group, user=self.user)
# 
# Path: open_connect/connectmessages/tasks.py
# def import_image_attachment():
# def process_image_attachment(image_id):
# def send_message(message_id, shorten=True):
# def send_system_message(recipient, subject, message_content):


# Path: open_connect/connectmessages/models.py
from bs4 import BeautifulSoup
from datetime import timedelta
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import models
from django.db.models import Q, ObjectDoesNotExist
from django.utils.encoding import smart_text
from django.utils.timezone import now
from unidecode import unidecode
from open_connect.media.models import Image, ShortenedURL
from open_connect.notifications.models import Subscription
from open_connect.connectmessages import tasks
from open_connect.connect_core.utils.models import TimestampModel
from open_connect.connect_core.utils.stringhelp import unicode_or_empty
import logging
import re
import pytz

        return "Thread %s" % self.subject

    @property
    def is_system_thread(self):
        """Returns true if this is a thread started by the system message"""
        return self.first_message.is_system_message

    def get_absolute_url(self):
        """Return absolute URL for a thread"""
        return reverse('thread', args=(self.pk,))

    def get_unsubscribe_url(self):
        """Return the URL of the unsubscribe view"""
        return reverse('thread_unsubscribe', args=(self.pk,))

    def add_user_to_thread(self, user):
        """Add a user to a thread."""
        defaults = {}
        if self.thread_type == 'group':
            # This is put inside a try/except because it is possible for staff
            # to send messages to groups they are not members of and thus
            # do not have a subscription to.
            try:
                subscription = Subscription.objects.get(
                    user=user, group=self.group)

                # By default everyone is subscribed to new threads
                # If this user has determined to not subscribe, unmark them
                if subscription.period == 'none':
                    defaults['subscribed_email'] = False

            except Subscription.DoesNotExist: