# Path: pytask/profile/models.py
# class Notification(models.Model):
#     """ A model to hold notifications.
#     All these are sent by the site to users.
#     Hence there is no sent_from option.
#     """
# 
#     sent_to = models.ForeignKey(User,
#                                 related_name = "%(class)s_sent_to",
#                                 blank = False)
# 
#     subject = models.CharField(max_length=100, blank=True)
# 
#     message = models.TextField()
# 
#     sent_date = models.DateTimeField()
# 
#     is_read = models.BooleanField(default = False)
# 
#     is_deleted = models.BooleanField(default = False)


from django import shortcuts
from django.http import Http404
from django.contrib.auth.models import User
from pytask.profile.models import Notification
# PyTask is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyTask.  If not, see <http://www.gnu.org/licenses/>.


__authors__ = [
    '"Madhusudan.C.S" <madhusudancs@fossee.in>',
    ]



def get_notification(nid, user):
    """ if notification exists, and belongs to the current user, return it.
    else return None.
    """

    user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
    current_notifications = user_notifications.filter(pk=nid)
    if user_notifications:
        current_notification = current_notifications[0]

        try:
            newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
            newest_notification = user_notifications.reverse()[0]
            if newest_notification == newer_notification:
                newest_notification = None

# Path: pytask/profile/models.py
# class Notification(models.Model):
#     """ A model to hold notifications.
#     All these are sent by the site to users.
#     Hence there is no sent_from option.
#     """
# 
#     sent_to = models.ForeignKey(User,
#                                 related_name = "%(class)s_sent_to",
#                                 blank = False)
# 
#     subject = models.CharField(max_length=100, blank=True)
# 
#     message = models.TextField()
# 
#     sent_date = models.DateTimeField()
# 
#     is_read = models.BooleanField(default = False)
# 
#     is_deleted = models.BooleanField(default = False)


# Path: pytask/profile/utils.py
from django import shortcuts
from django.http import Http404
from django.contrib.auth.models import User
from pytask.profile.models import Notification

# PyTask is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyTask.  If not, see <http://www.gnu.org/licenses/>.


__authors__ = [
    '"Madhusudan.C.S" <madhusudancs@fossee.in>',
    ]



def get_notification(nid, user):
    """ if notification exists, and belongs to the current user, return it.
    else return None.
    """

    user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
    current_notifications = user_notifications.filter(pk=nid)
    if user_notifications:
        current_notification = current_notifications[0]

        try:
            newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
            newest_notification = user_notifications.reverse()[0]
            if newest_notification == newer_notification:
                newest_notification = None

        except Notification.DoesNotExist: