diff --git a/rhodecode/controllers/admin/notifications.py b/rhodecode/controllers/admin/notifications.py --- a/rhodecode/controllers/admin/notifications.py +++ b/rhodecode/controllers/admin/notifications.py @@ -60,19 +60,23 @@ class NotificationsController(BaseContro """GET /_admin/notifications: All items in the collection""" # url('notifications') c.user = self.rhodecode_user - notif = NotificationModel().get_for_user(self.rhodecode_user.user_id) + notif = NotificationModel().get_for_user(self.rhodecode_user.user_id, + filter_=request.GET) p = int(request.params.get('page', 1)) c.notifications = Page(notif, page=p, items_per_page=10) + c.pull_request_type = Notification.TYPE_PULL_REQUEST return render('admin/notifications/notifications.html') def mark_all_read(self): if request.environ.get('HTTP_X_PARTIAL_XHR'): nm = NotificationModel() # mark all read - nm.mark_all_read_for_user(self.rhodecode_user.user_id) + nm.mark_all_read_for_user(self.rhodecode_user.user_id, + filter_=request.GET) Session.commit() c.user = self.rhodecode_user - notif = nm.get_for_user(self.rhodecode_user.user_id) + notif = nm.get_for_user(self.rhodecode_user.user_id, + filter_=request.GET) c.notifications = Page(notif, page=1, items_per_page=10) return render('admin/notifications/notifications_data.html') diff --git a/rhodecode/model/notification.py b/rhodecode/model/notification.py --- a/rhodecode/model/notification.py +++ b/rhodecode/model/notification.py @@ -36,6 +36,7 @@ from rhodecode.config.conf import DATETI 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 @@ class NotificationModel(BaseModel): 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 @@ class NotificationModel(BaseModel): 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" diff --git a/rhodecode/templates/admin/notifications/notifications.html b/rhodecode/templates/admin/notifications/notifications.html --- a/rhodecode/templates/admin/notifications/notifications.html +++ b/rhodecode/templates/admin/notifications/notifications.html @@ -26,8 +26,8 @@ %if c.notifications:
- ${_('All')} - ${_('Pull requests')} + ${_('All')} + ${_('Pull requests')}
${_('Mark all read')} @@ -44,7 +44,7 @@ YUE.on(YUQ('.delete-notification'),'clic deleteNotification(url_del,notification_id) }) YUE.on('mark_all_read','click',function(e){ - var url = "${h.url('notifications_mark_all_read')}"; + var url = "${h.url('notifications_mark_all_read', **request.GET)}"; ypjax(url,'notification_data',function(){ var notification_counter = YUD.get('notification_counter'); if(notification_counter){