diff rhodecode/model/notification.py @ 2433:74f2910f7ad9 codereview

Added pull requests filter into notification inbox. - Mark all read now also marks only filtered results
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 10 Jun 2012 00:08:29 +0200
parents d3ac7491a5c8
children f29469677319
line wrap: on
line diff
--- a/rhodecode/model/notification.py	Sat Jun 09 20:23:48 2012 +0200
+++ b/rhodecode/model/notification.py	Sun Jun 10 00:08:29 2012 +0200
@@ -36,6 +36,7 @@
 from rhodecode.lib import helpers as h
 from rhodecode.model import BaseModel
 from rhodecode.model.db import Notification, User, UserNotification
+from sqlalchemy.orm import joinedload
 
 log = logging.getLogger(__name__)
 
@@ -136,15 +137,41 @@
             log.error(traceback.format_exc())
             raise
 
-    def get_for_user(self, user):
+    def get_for_user(self, user, filter_=None):
+        """
+        Get mentions for given user, filter them if filter dict is given
+
+        :param user:
+        :type user:
+        :param filter:
+        """
         user = self._get_user(user)
-        return user.notifications
+
+        q = UserNotification.query()\
+            .filter(UserNotification.user == user)\
+            .join((Notification, UserNotification.notification_id ==
+                                 Notification.notification_id))
+
+        if filter_:
+            q = q.filter(Notification.type_ == filter_.get('type'))
 
-    def mark_all_read_for_user(self, user):
+        return q.all()
+
+    def mark_all_read_for_user(self, user, filter_=None):
         user = self._get_user(user)
-        UserNotification.query()\
+        q = UserNotification.query()\
+            .filter(UserNotification.user == user)\
             .filter(UserNotification.read == False)\
-            .update({'read': True})
+            .join((Notification, UserNotification.notification_id ==
+                                 Notification.notification_id))
+        if filter_:
+            q = q.filter(Notification.type_ == filter_.get('type'))
+
+        # this is a little inefficient but sqlalchemy doesn't support
+        # update on joined tables :(
+        for obj in q.all():
+            obj.read = True
+            self.sa.add(obj)
 
     def get_unread_cnt_for_user(self, user):
         user = self._get_user(user)
@@ -176,7 +203,8 @@
             notification.TYPE_CHANGESET_COMMENT: _('commented on commit'),
             notification.TYPE_MESSAGE: _('sent message'),
             notification.TYPE_MENTION: _('mentioned you'),
-            notification.TYPE_REGISTRATION: _('registered in RhodeCode')
+            notification.TYPE_REGISTRATION: _('registered in RhodeCode'),
+            notification.TYPE_PULL_REQUEST: _('opened new pull request')
         }
 
         tmpl = "%(user)s %(action)s %(when)s"