taskapp/utilities/notification.py
changeset 133 34187a80d279
parent 132 ca88bf4ad362
child 141 2489392ffb56
--- a/taskapp/utilities/notification.py	Sat Feb 27 19:21:15 2010 +0530
+++ b/taskapp/utilities/notification.py	Sun Feb 28 01:15:15 2010 +0530
@@ -71,18 +71,17 @@
     elif role in ["DV", "MG", "AD"]:
 
         accepting_user = sent_from
-        user_url = '<a href="/user/view/uid=%s">%s</a>'%(accepting_user.id,accepting_user.username) ## i mean the user who has accepted it
-        requested_by_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id,requested_by.username)
+        user_url = "/user/view/uid=%s"%(accepting_user.id) ## i mean the user who has accepted it
+        requested_by_url = "/user/view/uid=%s"%(requested_by.id)
         role_rights = dict(RIGHTS_CHOICES)[role]
         role_learn_url = "/about/%s"%role_rights.lower()
-        a_or_an = "an" if role == "AD" else "a"
 
         if reply:
             notification.sub = "New %s for the site"%role_rights
-            notification.message = "%s has accepted request made by %s asking him to act as %s %s for the website.<br />"%(user_url, requested_by_url, a_or_an,  role_rights)
+            notification.message = "%s has accepted request made by %s asking him to act as a %s for the website.<br />"(user_url, requested_by_url, role_rights)
         else:
             notification.sub = "Rejected your request to act as %s"%role_rights
-            notification.message = "%s has rejected your request asking him to act as %s %s for the website.<br />"%(user_url, a_or_an, role_rights)
+            notification.message = "%s has rejected your request asking him to act as a %s.<br />"%(new_mentor_url, task_url)
             if remarks:
                 notification.message += "Remarks: %s<br />"%remarks
 
@@ -110,7 +109,6 @@
 
     notification.save()
 
-
 def mark_notification_read(notification_id):
     """
     makes a notification identified by the notification_id read.
@@ -144,10 +142,28 @@
     else return None.
     """
 
-    try:
-        notify_obj = Notification.objects.get(id=nid)
-    except Notification.DoesNotExist:
-        return None
+    user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
+    current_notifications = user_notifications.filter(id=nid)
+    if user_notifications:
+        current_notification = current_notifications[0]
+
+        try:
+            newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
+            newest_notification = user_notifications.reverse()[0]
+            if newest_notification == newer_notification:
+                newest_notification = None
+        except Notification.DoesNotExist:
+            newest_notification, newer_notification = None, None
 
-    if notify_obj.sent_to == user and ( not notify_obj.is_deleted ):
-        return notify_obj
+        try:
+            older_notification = current_notification.get_previous_by_sent_date(sent_to=user, is_deleted=False)
+            oldest_notification = user_notifications[0]
+            if oldest_notification == older_notification:
+                oldest_notification = None
+        except:
+            oldest_notification, older_notification = None, None
+
+        return newest_notification, newer_notification, current_notification, older_notification, oldest_notification
+
+    else:
+        return None, None, None, None, None