taskapp/utilities/notification.py
changeset 131 85276c5aee5c
parent 129 e747da8bc110
child 132 ca88bf4ad362
equal deleted inserted replaced
130:9417ae986d2a 131:85276c5aee5c
     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(role, sent_to, sent_from=None, reply=None, task=None, receiving_user=None, pynts=None, requested_by=None, remarks=None):
     5 def create_notification(role, sent_to, sent_from=None, reply=None, task=None, remarks=None, requested_by=None, receiving_user=None, pynts=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         role: role of the notification - look at choices in models 
     9         subject - subject of the notification message to be sent
     9         sent_to: a user to which the notification is to be sent
    10         message - message body of the notification
    10         sent_from : a user from which the message has originated
       
    11             A user who approves/rejects in case of request
       
    12             A mentor who closes/complets the task
       
    13         reply: A boolean
       
    14         task: a task if applicable
       
    15         requested_by: a user makes the request
       
    16             A mentor who assigns credits in case of pynts
       
    17             A mentor who requests to act as a mentor
       
    18         remarks: any remarks for rejecting
       
    19         receiving_user: user receiving pynts
       
    20         pynts: the obvious
    11     """
    21     """
    12 
    22 
    13     notification = Notification(sent_date = datetime.now())
    23     notification = Notification(sent_date = datetime.now())
    14     notification.role = role
    24     notification.role = role
    15     notification.sent_to = sent_to
    25     notification.sent_to = sent_to
    26         mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
    36         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)
    37         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)
    38         user_url = '<a href="/user/view/uid=%s">%s</a>'%(receiving_user.id, receiving_user.username)
    29 
    39 
    30         if reply:
    40         if reply:
    31             notification.sub = "Approved request for assign of credits"
    41             notification.sub = "Approved request for assign of credits for %s"%task.title[:20]
    32             notification.message  = """ Request made by %s to assign %s pynts to %s for the task %s has been approved by %s<br />
    42             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)
    43                                     %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 
    44 
    35         else:
    45         else:
    36             notification.sub = "Rejected request for assign of credits"
    46             notification.sub = "Rejected request for assign of credits for %s"%task.title[:20]
    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)
    47             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:
    48             if remarks:
    39                 notification.remarks = remarks
    49                 notification.remarks = remarks
    40                 notification.message += "Reason: %s<br />"%remarks
    50                 notification.message += "Reason: %s<br />"%remarks
    41             notification.message += "<br />"
    51             notification.message += "<br />"
    42 
    52 
    43         notification.save()
    53     elif role == "MT":
       
    54 
       
    55         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
       
    56         requested_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
       
    57         new_mentor = sent_from
       
    58         new_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(new_mentor.id, new_mentor.username)
       
    59         
       
    60         if reply:
       
    61             notification.sub = "New mentor for the task %s"%task.title[:20]
       
    62             notification.message = "%s has accepted the request made by %s, asking him act as a mentor for the task %s<br />"%(new_mentor_url, requested_mentor_url, task_url)
       
    63             notification.message += "He can be contacted on %s"%new_mentor.email
       
    64 
       
    65         else:
       
    66             notification.sub = "Rejected request to act as a mentor for %s"%task.title[:20]
       
    67             notification.message = "%s has rejected your request asking him to act as a mentor for %s.<br />"%(new_mentor_url, task_url)
       
    68             if remarks:
       
    69                 notification.message += "Remarks: %s<br />"%remarks
       
    70 
       
    71 
       
    72     elif role == "NT":
       
    73 
       
    74         new_mentor = sent_to
       
    75         mentor_learn_url = '<sup><a href="/about/mentor">learn more</a></sup>'
       
    76         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
       
    77 
       
    78         notification.sub = "You are mentoring the task %s"%task.title[:20]
       
    79         notification.message = "You have accepted to act as a mentor%s for the task %s.<br />"%(mentor_learn_url, task_url)
       
    80         notification.message += " Here is a list of other mentors and their email addresses.<br /> <ul>"
       
    81 
       
    82         for a_mentor in task.mentors.exclude(id=new_mentor.id):
       
    83             notification.message += "<li> %s - %s </li>"%(a_mentor.username, a_mentor.email)
       
    84         notification.message += "</ul> List of users working on the task.<br />"
       
    85 
       
    86         working_users = task.assigned_users.all()
       
    87         if working_users:
       
    88             notification_message += "<ul>"
       
    89             for a_user in working_users:
       
    90                 notification.message += "<li> %s - %s </li>"%(a_user.username, a_user.email)
       
    91             notification.message += "</ul><br />"
       
    92         notification.message += "Happy Mentoring."
       
    93 
       
    94     notification.save()
    44 
    95 
    45 
    96 
    46 def mark_notification_read(notification_id):
    97 def mark_notification_read(notification_id):
    47     """
    98     """
    48     makes a notification identified by the notification_id read.
    99     makes a notification identified by the notification_id read.