Changeset - 74f2910f7ad9
[Not reviewed]
codereview
0 3 0
Marcin Kuzminski - 13 years ago 2012-06-10 00:08:29
marcin@python-works.com
Added pull requests filter into notification inbox.
- Mark all read now also marks only filtered results
3 files changed with 44 insertions and 12 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/notifications.py
Show inline comments
 
@@ -57,25 +57,29 @@ class NotificationsController(BaseContro
 
        super(NotificationsController, self).__before__()
 

	
 
    def index(self, format='html'):
 
        """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')
 

	
 
    def create(self):
 
        """POST /_admin/notifications: Create a new item"""
 
        # url('notifications')
rhodecode/model/notification.py
Show inline comments
 
@@ -33,12 +33,13 @@ from pylons.i18n.translation import _
 

	
 
import rhodecode
 
from rhodecode.config.conf import DATETIME_FORMAT
 
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__)
 

	
 

	
 
class NotificationModel(BaseModel):
 

	
 
@@ -133,21 +134,47 @@ class NotificationModel(BaseModel):
 
                self.sa.delete(obj)
 
                return True
 
        except Exception:
 
            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)
 
        return UserNotification.query()\
 
                .filter(UserNotification.read == False)\
 
                .filter(UserNotification.user == user).count()
 
@@ -173,13 +200,14 @@ class NotificationModel(BaseModel):
 
        """
 

	
 
        _map = {
 
            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"
 
        if show_age:
 
            when = h.age(notification.created_on)
 
        else:
rhodecode/templates/admin/notifications/notifications.html
Show inline comments
 
@@ -23,14 +23,14 @@
 
        ##      <span style="text-transform: uppercase;"><a href="#">${_('Compose message')}</a></span>
 
        ##    </li>
 
        ##</ul>
 
    </div>
 
    %if c.notifications:
 
      <div style="padding:14px 18px;text-align: right;float:left">
 
      <span id='all' class="ui-btn">${_('All')}</span>
 
      <span id='pull_request' class="ui-btn">${_('Pull requests')}</span>
 
      <span id='all' class="ui-btn"><a href="${h.url.current()}">${_('All')}</a></span>
 
      <span id='pull_request' class="ui-btn"><a href="${h.url.current(type=c.pull_request_type)}">${_('Pull requests')}</a></span>
 
      </div>
 
      <div style="padding:14px 18px;text-align: right;float:right">
 
      <span id='mark_all_read' class="ui-btn">${_('Mark all read')}</span>
 
      </div>
 
    %endif
 
  <div id='notification_data'>
 
@@ -41,13 +41,13 @@
 
var url_del = "${url('notification', notification_id='__NOTIFICATION_ID__')}";
 
YUE.on(YUQ('.delete-notification'),'click',function(e){
 
 var notification_id = e.currentTarget.id;
 
 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){
 
    		notification_counter.innerHTML=0;
 
    	}
 
    	YUE.on(YUQ('.delete-notification'),'click',function(e){
0 comments (0 inline, 0 general)