added notifications for approval and rejection of a mentor.
authornishanth
Sat, 27 Feb 2010 17:20:22 +0530
changeset 131 85276c5aee5c
parent 130 9417ae986d2a
child 132 ca88bf4ad362
added notifications for approval and rejection of a mentor.
taskapp/events/request.py
taskapp/events/task.py
taskapp/models.py
taskapp/utilities/notification.py
taskapp/views/task.py
--- a/taskapp/events/request.py	Sat Feb 27 14:38:01 2010 +0530
+++ b/taskapp/events/request.py	Sat Feb 27 17:20:22 2010 +0530
@@ -27,20 +27,27 @@
             for a_mentor in task.mentors.all():
                 if reply:
                     addCredits(task, request_obj.sent_by, request_obj.receiving_user, pynts)
-                    create_notification(request_obj.role, a_mentor, replied_by, True, task, receiving_user, pynts, requested_by)
+                    create_notification(request_obj.role, a_mentor, replied_by, True, task, request_obj.remarks, requested_by, receiving_user, pynts)
                 else:
-                    create_notification(request_obj.role, a_mentor, replied_by, False, task, receiving_user, pynts, requested_by, request_obj.remarks)
+                    create_notification(request_obj.role, a_mentor, replied_by, False, task, request_obj.remarks, requested_by, receiving_user, pynts)
 
         elif request_obj.role == "MT":
-            ## add him as a mentor to the task
+            task = request_obj.task
+            requested_by = request_obj.sent_by
             if reply:
-                ## check for the current rights of request_obj.sent_by
-                ## what if he is no more a mentor to the task
-                addMentor(request_obj.task, request_obj.replied_by)
-                ## pass on notification of request_obj.sent_by
+                ## tell the replied user that he is mentor for this task and give him learn more link
+                create_notification("NT", request_obj.replied_by, task=task) 
+
+                ## alert all the mentors including who made request and all assigned users
+                for a_mentor in task.mentors.all():
+                    create_notification(request_obj.role, a_mentor, replied_by, True, task, request_obj.remarks, requested_by)
+                for a_user in task.assigned_users.all():
+                    create_notification(request_obj.role, a_user, replied_by, True, task, request_obj.remarks, requested_by)
+
+                addMentor(task, request_obj.replied_by)
             else:
-                print "request for mentor rejected"
-                ## pass on notification to request_obj.sent_by
+                ## tell the requested user that his request was rejected due to these reasons.
+                create_notification(request_obj.role, requested_by, replied_by, False, task, request_obj.remarks, requested_by)
 
         elif request_obj.role in ["AD", "MG", "DV"]:
             if reply:
--- a/taskapp/events/task.py	Sat Feb 27 14:38:01 2010 +0530
+++ b/taskapp/events/task.py	Sat Feb 27 17:20:22 2010 +0530
@@ -3,6 +3,7 @@
 from pytask.taskapp.utilities.task import getTask
 from pytask.taskapp.utilities.request import create_request
 from pytask.taskapp.utilities.helper import get_key
+from pytask.taskapp.utilities.notification import create_notification
 
 def publishTask(task, rem_mentors=True, rem_comments=True):
     """ set the task status to open """
--- a/taskapp/models.py	Sat Feb 27 14:38:01 2010 +0530
+++ b/taskapp/models.py	Sat Feb 27 17:20:22 2010 +0530
@@ -35,7 +35,8 @@
     ("CD", "Task closed"),
     ("DL", "Task deleted"),
     ("KD", "Kicked off"),
-    ("MS", "Message"),
+    ("NU", "New User"),
+    ("NT", "New Mentor"),
 )
 
 
--- a/taskapp/utilities/notification.py	Sat Feb 27 14:38:01 2010 +0530
+++ b/taskapp/utilities/notification.py	Sat Feb 27 17:20:22 2010 +0530
@@ -2,12 +2,22 @@
 from django.contrib.auth.models import User
 from pytask.taskapp.models import Notification
 
-def create_notification(role, sent_to, sent_from=None, reply=None, task=None, receiving_user=None, pynts=None, requested_by=None, remarks=None):
+def create_notification(role, sent_to, sent_from=None, reply=None, task=None, remarks=None, requested_by=None, receiving_user=None, pynts=None):
     """
     creates a notification based on the passed arguments.
-        to - a list of users to which the notification is to be sent
-        subject - subject of the notification message to be sent
-        message - message body of the notification
+        role: role of the notification - look at choices in models 
+        sent_to: a user to which the notification is to be sent
+        sent_from : a user from which the message has originated
+            A user who approves/rejects in case of request
+            A mentor who closes/complets the task
+        reply: A boolean
+        task: a task if applicable
+        requested_by: a user makes the request
+            A mentor who assigns credits in case of pynts
+            A mentor who requests to act as a mentor
+        remarks: any remarks for rejecting
+        receiving_user: user receiving pynts
+        pynts: the obvious
     """
 
     notification = Notification(sent_date = datetime.now())
@@ -28,19 +38,60 @@
         user_url = '<a href="/user/view/uid=%s">%s</a>'%(receiving_user.id, receiving_user.username)
 
         if reply:
-            notification.sub = "Approved request for assign of credits"
+            notification.sub = "Approved request for assign of credits for %s"%task.title[:20]
             notification.message  = """ Request made by %s to assign %s pynts to %s for the task %s has been approved by %s<br />
                                     %s if you want the view/assign pynts page of the task.<br />"""%(mentor_url, pynts, user_url, task_url, admin_url, credits_url)
 
         else:
-            notification.sub = "Rejected request for assign of credits"
+            notification.sub = "Rejected request for assign of credits for %s"%task.title[:20]
             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)
             if remarks:
                 notification.remarks = remarks
                 notification.message += "Reason: %s<br />"%remarks
             notification.message += "<br />"
 
-        notification.save()
+    elif role == "MT":
+
+        task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
+        requested_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
+        new_mentor = sent_from
+        new_mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(new_mentor.id, new_mentor.username)
+        
+        if reply:
+            notification.sub = "New mentor for the task %s"%task.title[:20]
+            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)
+            notification.message += "He can be contacted on %s"%new_mentor.email
+
+        else:
+            notification.sub = "Rejected request to act as a mentor for %s"%task.title[:20]
+            notification.message = "%s has rejected your request asking him to act as a mentor for %s.<br />"%(new_mentor_url, task_url)
+            if remarks:
+                notification.message += "Remarks: %s<br />"%remarks
+
+
+    elif role == "NT":
+
+        new_mentor = sent_to
+        mentor_learn_url = '<sup><a href="/about/mentor">learn more</a></sup>'
+        task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
+
+        notification.sub = "You are mentoring the task %s"%task.title[:20]
+        notification.message = "You have accepted to act as a mentor%s for the task %s.<br />"%(mentor_learn_url, task_url)
+        notification.message += " Here is a list of other mentors and their email addresses.<br /> <ul>"
+
+        for a_mentor in task.mentors.exclude(id=new_mentor.id):
+            notification.message += "<li> %s - %s </li>"%(a_mentor.username, a_mentor.email)
+        notification.message += "</ul> List of users working on the task.<br />"
+
+        working_users = task.assigned_users.all()
+        if working_users:
+            notification_message += "<ul>"
+            for a_user in working_users:
+                notification.message += "<li> %s - %s </li>"%(a_user.username, a_user.email)
+            notification.message += "</ul><br />"
+        notification.message += "Happy Mentoring."
+
+    notification.save()
 
 
 def mark_notification_read(notification_id):
--- a/taskapp/views/task.py	Sat Feb 27 14:38:01 2010 +0530
+++ b/taskapp/views/task.py	Sat Feb 27 17:20:22 2010 +0530
@@ -180,7 +180,7 @@
             reqMentor(task, new_mentor, user)
             return redirect(task_url)
         else:
-            return render_to_response('task/addmentor.html', {'form':form, 'errors':errors})
+            return render_to_response('task/addmentor.html', {'user':user,'form':form, 'errors':errors})
         
     else:
         return show_msg(user, 'You are not authorised to add mentors for this task', task_url, 'view the task')