app/soc/logic/models/notification.py
changeset 757 b11cee4ab535
parent 726 ba3d399ec9be
child 819 08a79c3e8817
--- a/app/soc/logic/models/notification.py	Wed Dec 17 19:33:03 2008 +0000
+++ b/app/soc/logic/models/notification.py	Wed Dec 17 22:15:21 2008 +0000
@@ -22,6 +22,9 @@
   ]
 
 
+from google.appengine.ext import db
+
+from soc.logic.helper import notifications
 from soc.logic.models import base
 from soc.logic.models import user as user_logic
 
@@ -37,6 +40,22 @@
     """
     super(Logic, self).__init__(model=soc.models.notification.Notification,
          base_model=None, scope_logic=user_logic)
+    
+  def _onCreate(self, entity):
+    """Sends out a message if there is only one unread notification.
+    """
+    
+    # create a special query on which we can call count
+    query = db.Query(self._model)
+    query.filter('scope =', entity.scope)
+    query.filter('unread = ', True)
+    
+    # count the number of results with a maximum of two
+    unread_count = query.count(2)
+    
+    if unread_count == 1:
+      # there is only one unread notification so send out an email
+      notifications.sendNewNotificationMessage(entity)
 
 
 logic = Logic()