taskapp/utilities/notification.py
changeset 218 59107ce0a618
parent 201 c429873d8ac8
child 219 f04a1ec7a07f
equal deleted inserted replaced
217:307f699e6102 218:59107ce0a618
     7     creates a notification based on the passed arguments.
     7     creates a notification based on the passed arguments.
     8         role: role of the notification - look at choices in models 
     8         role: role of the notification - look at choices in models 
     9         sent_to: a user to which the notification is to be sent
     9         sent_to: a user to which the notification is to be sent
    10         sent_from : a user from which the message has originated
    10         sent_from : a user from which the message has originated
    11             A user who approves/rejects in case of request
    11             A user who approves/rejects in case of request
    12             A mentor who closes/complets the task
    12             A reviewer who closes/complets the task
    13         reply: A boolean
    13         reply: A boolean
    14         task: a task if applicable
    14         task: a task if applicable
    15         requested_by: a user makes the request
    15         requested_by: a user makes the request
    16             A mentor who assigns credits in case of pynts
    16             A reviewer who assigns credits in case of pynts
    17             A mentor who requests to act as a mentor
    17             A reviewer who requests to act as a reviewer
    18         remarks: any remarks for rejecting
    18         remarks: any remarks for rejecting
    19         receiving_user: user receiving pynts
    19         receiving_user: user receiving pynts
    20         pynts: the obvious
    20         pynts: the obvious
    21     """
    21     """
    22 
    22 
    31         notification.task = task
    31         notification.task = task
    32         notification.pynts = pynts
    32         notification.pynts = pynts
    33 
    33 
    34         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
    34         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
    35         credits_url = '<a href="/task/assigncredits/tid=%s">%s</a>'%(task.id, "click here")
    35         credits_url = '<a href="/task/assigncredits/tid=%s">%s</a>'%(task.id, "click here")
    36         mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
    36         reviewer_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
    37         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)
    38         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)
    39 
    39 
    40         if reply:
    40         if reply:
    41             notification.sub = "Approved request for assign of credits for %s"%task.title[:20]
    41             notification.sub = "Approved request for assign of credits for %s"%task.title[:20]
    42             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 />
    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)
    43                                     %s if you want the view/assign pynts page of the task.<br />"""%(reviewer_url, pynts, user_url, task_url, admin_url, credits_url)
    44 
    44 
    45         else:
    45         else:
    46             notification.sub = "Rejected request for assign of credits for %s"%task.title[:20]
    46             notification.sub = "Rejected request for assign of credits for %s"%task.title[:20]
    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)
    47             notification.message = """ Request made by %s to assign %s pynts to %s for the task %s has been rejected by %s.<br /> """%(reviewer_url, pynts, user_url, task_url, admin_url)
    48             if remarks:
    48             if remarks:
    49                 notification.remarks = remarks
    49                 notification.remarks = remarks
    50                 notification.message += "Reason: %s<br />"%remarks
    50                 notification.message += "Reason: %s<br />"%remarks
    51             notification.message += "<br />"
    51             notification.message += "<br />"
    52 
    52 
    54 
    54 
    55         notification.task = task
    55         notification.task = task
    56         notification.sent_from = sent_from
    56         notification.sent_from = sent_from
    57 
    57 
    58         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
    58         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
    59         requested_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
    59         requested_reviewer_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
    60         new_mentor = sent_from
    60         new_reviewer = sent_from
    61         new_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(new_mentor.id, new_mentor.username)
    61         new_reviewer_url = '<a href="/user/view/uid=%s">%s</a>'%(new_reviewer.id, new_reviewer.username)
    62         
    62         
    63         if reply:
    63         if reply:
    64             notification.sub = "New mentor for the task %s"%task.title[:20]
    64             notification.sub = "New reviewer for the task %s"%task.title[:20]
    65             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)
    65             notification.message = "%s has accepted the request made by %s, asking him act as a reviewer for the task %s<br />"%(new_reviewer_url, requested_reviewer_url, task_url)
    66             notification.message += "He can be contacted on %s"%new_mentor.email
    66             notification.message += "He can be contacted on %s"%new_reviewer.email
    67 
    67 
    68         else:
    68         else:
    69             notification.sub = "%s rejected request to act as a mentor"%new_mentor.username
    69             notification.sub = "%s rejected request to act as a reviewer"%new_reviewer.username
    70             notification.message = "%s has rejected your request asking him to act as a mentor for %s.<br />"%(new_mentor_url, task_url)
    70             notification.message = "%s has rejected your request asking him to act as a reviewer for %s.<br />"%(new_reviewer_url, task_url)
    71             if remarks:
    71             if remarks:
    72                 notification.remarks = remarks
    72                 notification.remarks = remarks
    73                 notification.message += "Remarks: %s<br />"%remarks
    73                 notification.message += "Remarks: %s<br />"%remarks
    74 
    74 
    75     elif role in ["DV", "MG", "AD"]:
    75     elif role in ["DV", "MG", "AD"]:
    93                 notification.message += "Remarks: %s<br />"%remarks
    93                 notification.message += "Remarks: %s<br />"%remarks
    94 
    94 
    95     elif role == "NT":
    95     elif role == "NT":
    96 
    96 
    97         notification.task = task
    97         notification.task = task
    98         new_mentor = sent_to
    98         new_reviewer = sent_to
    99         mentor_learn_url = '<sup><a href="/about/mentor/">learn more</a></sup>'
    99         reviewer_learn_url = '<sup><a href="/about/reviewer/">learn more</a></sup>'
   100         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   100         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   101 
   101 
   102         notification.sub = "You are mentoring the task %s"%task.title[:20]
   102         notification.sub = "You are reviewering the task %s"%task.title[:20]
   103         notification.message = "You have accepted to act as a mentor%s for the task %s.<br />"%(mentor_learn_url, task_url)
   103         notification.message = "You have accepted to act as a reviewer%s for the task %s.<br />"%(reviewer_learn_url, task_url)
   104         notification.message += " Here is a list of other mentors and their email addresses.<br /> <ul>"
   104         notification.message += " Here is a list of other reviewers and their email addresses.<br /> <ul>"
   105 
   105 
   106         for a_mentor in task.mentors.exclude(id=new_mentor.id):
   106         for a_reviewer in task.reviewers.exclude(id=new_reviewer.id):
   107             notification.message += "<li> %s - %s </li>"%(a_mentor.username, a_mentor.email)
   107             notification.message += "<li> %s - %s </li>"%(a_reviewer.username, a_reviewer.email)
   108         notification.message += "</ul>"
   108         notification.message += "</ul>"
   109 
   109 
   110         working_users = task.assigned_users.all()
   110         working_users = task.assigned_users.all()
   111         if working_users:
   111         if working_users:
   112             notification.message += "List of users working on the task.<br />"
   112             notification.message += "List of users working on the task.<br />"
   113             notification.message += "<ul>"
   113             notification.message += "<ul>"
   114             for a_user in working_users:
   114             for a_user in working_users:
   115                 notification.message += "<li> %s - %s </li>"%(a_user.username, a_user.email)
   115                 notification.message += "<li> %s - %s </li>"%(a_user.username, a_user.email)
   116             notification.message += "</ul><br />"
   116             notification.message += "</ul><br />"
   117         notification.message += "Happy Mentoring."
   117         notification.message += "Happy Reviewering."
   118 
   118 
   119     elif role == "NU":
   119     elif role == "NU":
   120 
   120 
   121         start_here_url = '<a href="/about/starthere/" taget="_blank">click here</a>'
   121         start_here_url = '<a href="/about/starthere/" taget="_blank">click here</a>'
   122         notification.sub = "Welcome %s"%sent_to.username
   122         notification.sub = "Welcome %s"%sent_to.username
   148         notification.sent_from = sent_from
   148         notification.sent_from = sent_from
   149         notification.role = role
   149         notification.role = role
   150         notification.task = task
   150         notification.task = task
   151         notification.remarks = remarks
   151         notification.remarks = remarks
   152 
   152 
   153         mentor = sent_from
   153         reviewer = sent_from
   154         mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(mentor.id, mentor.username)
   154         reviewer_url = '<a href="/user/view/uid=%s">%s</a>'%(reviewer.id, reviewer.username)
   155         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   155         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   156         
   156         
   157         if role == "CM":
   157         if role == "CM":
   158             notification.sub = "%s has been marked complete"%task.title
   158             notification.sub = "%s has been marked complete"%task.title
   159             notification.message = "The task %s has been marked complete by %s.<br />"%(task_url, mentor_url)
   159             notification.message = "The task %s has been marked complete by %s.<br />"%(task_url, reviewer_url)
   160 
   160 
   161         elif role == "CD":
   161         elif role == "CD":
   162             notification.sub = "%s has been closed"%task.title
   162             notification.sub = "%s has been closed"%task.title
   163             notification.message = "The task %s has been closed by %s.<br />"%(task_url, mentor_url)
   163             notification.message = "The task %s has been closed by %s.<br />"%(task_url, reviewer_url)
   164 
   164 
   165         if remarks:
   165         if remarks:
   166             notification.remarks = remarks
   166             notification.remarks = remarks
   167             notification.message += "<b>Remarks:</b> %s"%remarks
   167             notification.message += "<b>Remarks:</b> %s"%remarks
   168 
   168 
   169     elif role == "AU":
   169     elif role == "AU":
   170 
   170 
   171         notification.task = task
   171         notification.task = task
   172         notification.sent_from = sent_from
   172         notification.sent_from = sent_from
   173         added_user = sent_to
   173         added_user = sent_to
   174         mentor = sent_from
   174         reviewer = sent_from
   175         assigned_by_url = '<a href="/user/view/uid=%s">%s</a>'%(mentor.id, mentor.username)
   175         assigned_by_url = '<a href="/user/view/uid=%s">%s</a>'%(reviewer.id, reviewer.username)
   176         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   176         task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   177 
   177 
   178         notification.sub = "Your claim for the task %s accepted."%task.title[:20]
   178         notification.sub = "Your claim for the task %s accepted."%task.title[:20]
   179         notification.message = "You have been selected to work on the task %s by %s.<br />"%(task_url, assigned_by_url)
   179         notification.message = "You have been selected to work on the task %s by %s.<br />"%(task_url, assigned_by_url)
   180         notification.message += "You can now start working on the task and will be credited by the mentors for your work.<br />"
   180         notification.message += "You can now start working on the task and will be credited by the reviewers for your work.<br />"
   181 
   181 
   182         notification.message += " Here is a list of mentors for the task and their email addresses.<br /> <ul>"
   182         notification.message += " Here is a list of reviewers for the task and their email addresses.<br /> <ul>"
   183         for a_mentor in task.mentors.all():
   183         for a_reviewer in task.reviewers.all():
   184             notification.message += "<li> %s - %s </li>"%(a_mentor.username, a_mentor.email)
   184             notification.message += "<li> %s - %s </li>"%(a_reviewer.username, a_reviewer.email)
   185         notification.message += "</ul>"
   185         notification.message += "</ul>"
   186 
   186 
   187         working_users = task.assigned_users.exclude(id=added_user.id)
   187         working_users = task.assigned_users.exclude(id=added_user.id)
   188         if working_users:
   188         if working_users:
   189             notification.message += "List of other users working on the task.<br />"
   189             notification.message += "List of other users working on the task.<br />"
   195     elif role == "RU":
   195     elif role == "RU":
   196 
   196 
   197         notification.task = task
   197         notification.task = task
   198         notification.sent_from = sent_from
   198         notification.sent_from = sent_from
   199         removed_user = sent_to
   199         removed_user = sent_to
   200         mentor = sent_from
   200         reviewer = sent_from
   201         removed_by_url = '<a href="/user/view/uid=%s">%s</a>'%(mentor.id, mentor.username)
   201         removed_by_url = '<a href="/user/view/uid=%s">%s</a>'%(reviewer.id, reviewer.username)
   202         task_url = '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   202         task_url = '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   203         claim_url = '<a href="/task/claim/tid=%s">%s</a>'%(task.id, "clicking here")
   203         claim_url = '<a href="/task/claim/tid=%s">%s</a>'%(task.id, "clicking here")
   204 
   204 
   205         notification.sub = "You have been removed from working users of %s"%task.title[:20]
   205         notification.sub = "You have been removed from working users of %s"%task.title[:20]
   206         notification.message = "%s has removed you from the working users list of %s.<br />"%(removed_by_url, task_url)
   206         notification.message = "%s has removed you from the working users list of %s.<br />"%(removed_by_url, task_url)
   231         claimed_by_url = '<a href="/user/view/uid=%s">%s</a>'%(sent_from.id, sent_from.username)
   231         claimed_by_url = '<a href="/user/view/uid=%s">%s</a>'%(sent_from.id, sent_from.username)
   232         claim_url = '<a href="/task/claim/tid=%s">claim</a>'%(task.id)
   232         claim_url = '<a href="/task/claim/tid=%s">claim</a>'%(task.id)
   233         task_url = '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   233         task_url = '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
   234 
   234 
   235         notification.sub = 'New claim for the task "%s"'%(task.title[:20])
   235         notification.sub = 'New claim for the task "%s"'%(task.title[:20])
   236         notification.message = '%s has submitted a %s for the task "%s" mentored by you.<br />'%(claimed_by_url, claim_url, task_url)
   236         notification.message = '%s has submitted a %s for the task "%s" reviewered by you.<br />'%(claimed_by_url, claim_url, task_url)
   237         notification.message += '<b>Claim proposal:</b> %s'%(remarks)
   237         notification.message += '<b>Claim proposal:</b> %s'%(remarks)
   238 
   238 
   239 
   239 
   240 
   240 
   241     notification.save()
   241     notification.save()