pytask/profile/utils.py
changeset 69 c6bca38c1cbf
parent 50 869a9ab7e2df
child 140 8fcde6f8f750
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 from pytask.profile.models import Notification
       
     2 
       
     3 def get_notification(nid, user):
       
     4     """ if notification exists, and belongs to the current user, return it.
       
     5     else return None.
       
     6     """
       
     7 
       
     8     user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
       
     9     current_notifications = user_notifications.filter(uniq_key=nid)
       
    10     if user_notifications:
       
    11         current_notification = current_notifications[0]
       
    12 
       
    13         try:
       
    14             newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
       
    15             newest_notification = user_notifications.reverse()[0]
       
    16             if newest_notification == newer_notification:
       
    17                 newest_notification = None
       
    18         except Notification.DoesNotExist:
       
    19             newest_notification, newer_notification = None, None
       
    20 
       
    21         try:
       
    22             older_notification = current_notification.get_previous_by_sent_date(sent_to=user, is_deleted=False)
       
    23             oldest_notification = user_notifications[0]
       
    24             if oldest_notification == older_notification:
       
    25                 oldest_notification = None
       
    26         except:
       
    27             oldest_notification, older_notification = None, None
       
    28 
       
    29         return newest_notification, newer_notification, current_notification, older_notification, oldest_notification
       
    30 
       
    31     else:
       
    32         return None, None, None, None, None