taskapp/utilities/notification.py
changeset 129 e747da8bc110
parent 123 a6b4234388c8
child 131 85276c5aee5c
equal deleted inserted replaced
128:f34e3a3e1439 129:e747da8bc110
     1 from datetime import datetime
     1 from datetime import datetime
     2 from django.contrib.auth.models import User
     2 from django.contrib.auth.models import User
     3 from pytask.taskapp.models import Notification
     3 from pytask.taskapp.models import Notification
     4 
     4 
     5 def create_notification(to,subject,message):
     5 def create_notification(role, sent_to, sent_from=None, reply=None, task=None, receiving_user=None, pynts=None, requested_by=None, remarks=None):
     6     """
     6     """
     7     creates a notification based on the passed arguments.
     7     creates a notification based on the passed arguments.
     8         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
     9         subject - subject of the notification message to be sent
     9         subject - subject of the notification message to be sent
    10         message - message body of the notification
    10         message - message body of the notification
    11     """
    11     """
       
    12 
    12     notification = Notification(sent_date = datetime.now())
    13     notification = Notification(sent_date = datetime.now())
       
    14     notification.role = role
       
    15     notification.sent_to = sent_to
    13     notification.save()
    16     notification.save()
    14     notification.sent_to = to
    17 
    15     notification.sub = subject
    18     if role == "PY":
    16     notification.message = message
    19 
    17     notification.save()
    20         notification.sent_from = sent_from
       
    21         notification.task = task
       
    22         notification.pynts = pynts
       
    23 
       
    24         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
       
    25         credits_url = '<a href="/task/assigncredits/tid=%s">%s</a>'%(task.id, "click here")
       
    26         mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
       
    27         admin_url = '<a href="/user/view/uid=%s">%s</a>'%(sent_from.id, sent_from.username)
       
    28         user_url = '<a href="/user/view/uid=%s">%s</a>'%(receiving_user.id, receiving_user.username)
       
    29 
       
    30         if reply:
       
    31             notification.sub = "Approved request for assign of credits"
       
    32             notification.message  = """ Request made by %s to assign %s pynts to %s for the task %s has been approved by %s<br />
       
    33                                     %s if you want the view/assign pynts page of the task.<br />"""%(mentor_url, pynts, user_url, task_url, admin_url, credits_url)
       
    34 
       
    35         else:
       
    36             notification.sub = "Rejected request for assign of credits"
       
    37             notification.message = """ Request made by %s to assign %s pynts to %s for the task %s has been rejected by %s.<br /> """%(mentor_url, pynts, user_url, task_url, admin_url)
       
    38             if remarks:
       
    39                 notification.remarks = remarks
       
    40                 notification.message += "Reason: %s<br />"%remarks
       
    41             notification.message += "<br />"
       
    42 
       
    43         notification.save()
       
    44 
    18 
    45 
    19 def mark_notification_read(notification_id):
    46 def mark_notification_read(notification_id):
    20     """
    47     """
    21     makes a notification identified by the notification_id read.
    48     makes a notification identified by the notification_id read.
    22     arguments:
    49     arguments:
    52     try:
    79     try:
    53         notify_obj = Notification.objects.get(id=nid)
    80         notify_obj = Notification.objects.get(id=nid)
    54     except Notification.DoesNotExist:
    81     except Notification.DoesNotExist:
    55         return None
    82         return None
    56 
    83 
    57     try:
    84     if notify_obj.sent_to == user and ( not notify_obj.is_deleted ):
    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
    85         return notify_obj