taskapp/utilities/notification.py
changeset 123 a6b4234388c8
parent 78 c5bcafccc135
child 129 e747da8bc110
equal deleted inserted replaced
122:daee11bdfbaa 123:a6b4234388c8
       
     1 from datetime import datetime
       
     2 from django.contrib.auth.models import User
     1 from pytask.taskapp.models import Notification
     3 from pytask.taskapp.models import Notification
     2 from datetime import datetime
       
     3 
     4 
     4 def create_notification(to,subject,message):
     5 def create_notification(to,subject,message):
     5     """
     6     """
     6     creates a notification based on the passed arguments.
     7     creates a notification based on the passed arguments.
     7         to - a list of users to which the notification is to be sent
     8         to - a list of users to which the notification is to be sent
     8         subject - subject of the notification message to be sent
     9         subject - subject of the notification message to be sent
     9         message - message body of the notification
    10         message - message body of the notification
    10     """
    11     """
    11     notification = Notification(sent_date = datetime.now())
    12     notification = Notification(sent_date = datetime.now())
    12     notification.save()
    13     notification.save()
    13     notification.to = to
    14     notification.sent_to = to
    14     notification.sub = subject
    15     notification.sub = subject
    15     notification.message = message
    16     notification.message = message
    16     notification.save()
    17     notification.save()
    17 
    18 
    18 def mark_notification_read(notification_id):
    19 def mark_notification_read(notification_id):
    37     """
    38     """
    38     try:
    39     try:
    39         notification = Notification.objects.get(id = notification_id)
    40         notification = Notification.objects.get(id = notification_id)
    40     except Notification.DoesNotExist:
    41     except Notification.DoesNotExist:
    41         return False
    42         return False
    42     notification.deleted = True
    43     notification.is_deleted = True
    43     notification.save()
    44     notification.save()
    44     return True
    45     return True
       
    46 
       
    47 def get_notification(nid, user):
       
    48     """ if notification exists, and belongs to the current user, return it.
       
    49     else return None.
       
    50     """
       
    51 
       
    52     try:
       
    53         notify_obj = Notification.objects.get(id=nid)
       
    54     except Notification.DoesNotExist:
       
    55         return None
       
    56 
       
    57     try:
       
    58         notify_obj.sent_to.get(id=user.id)
       
    59     except User.DoesNotExist:
       
    60         return None
       
    61 
       
    62     if not notify_obj.is_deleted:
       
    63         return notify_obj