Mercurial > kallithea
comparison 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 |
comparison
equal
deleted
inserted
replaced
2432:d3ac7491a5c8 | 2433:74f2910f7ad9 |
---|---|
34 import rhodecode | 34 import rhodecode |
35 from rhodecode.config.conf import DATETIME_FORMAT | 35 from rhodecode.config.conf import DATETIME_FORMAT |
36 from rhodecode.lib import helpers as h | 36 from rhodecode.lib import helpers as h |
37 from rhodecode.model import BaseModel | 37 from rhodecode.model import BaseModel |
38 from rhodecode.model.db import Notification, User, UserNotification | 38 from rhodecode.model.db import Notification, User, UserNotification |
39 from sqlalchemy.orm import joinedload | |
39 | 40 |
40 log = logging.getLogger(__name__) | 41 log = logging.getLogger(__name__) |
41 | 42 |
42 | 43 |
43 class NotificationModel(BaseModel): | 44 class NotificationModel(BaseModel): |
134 return True | 135 return True |
135 except Exception: | 136 except Exception: |
136 log.error(traceback.format_exc()) | 137 log.error(traceback.format_exc()) |
137 raise | 138 raise |
138 | 139 |
139 def get_for_user(self, user): | 140 def get_for_user(self, user, filter_=None): |
140 user = self._get_user(user) | 141 """ |
141 return user.notifications | 142 Get mentions for given user, filter them if filter dict is given |
142 | 143 |
143 def mark_all_read_for_user(self, user): | 144 :param user: |
144 user = self._get_user(user) | 145 :type user: |
145 UserNotification.query()\ | 146 :param filter: |
147 """ | |
148 user = self._get_user(user) | |
149 | |
150 q = UserNotification.query()\ | |
151 .filter(UserNotification.user == user)\ | |
152 .join((Notification, UserNotification.notification_id == | |
153 Notification.notification_id)) | |
154 | |
155 if filter_: | |
156 q = q.filter(Notification.type_ == filter_.get('type')) | |
157 | |
158 return q.all() | |
159 | |
160 def mark_all_read_for_user(self, user, filter_=None): | |
161 user = self._get_user(user) | |
162 q = UserNotification.query()\ | |
163 .filter(UserNotification.user == user)\ | |
146 .filter(UserNotification.read == False)\ | 164 .filter(UserNotification.read == False)\ |
147 .update({'read': True}) | 165 .join((Notification, UserNotification.notification_id == |
166 Notification.notification_id)) | |
167 if filter_: | |
168 q = q.filter(Notification.type_ == filter_.get('type')) | |
169 | |
170 # this is a little inefficient but sqlalchemy doesn't support | |
171 # update on joined tables :( | |
172 for obj in q.all(): | |
173 obj.read = True | |
174 self.sa.add(obj) | |
148 | 175 |
149 def get_unread_cnt_for_user(self, user): | 176 def get_unread_cnt_for_user(self, user): |
150 user = self._get_user(user) | 177 user = self._get_user(user) |
151 return UserNotification.query()\ | 178 return UserNotification.query()\ |
152 .filter(UserNotification.read == False)\ | 179 .filter(UserNotification.read == False)\ |
174 | 201 |
175 _map = { | 202 _map = { |
176 notification.TYPE_CHANGESET_COMMENT: _('commented on commit'), | 203 notification.TYPE_CHANGESET_COMMENT: _('commented on commit'), |
177 notification.TYPE_MESSAGE: _('sent message'), | 204 notification.TYPE_MESSAGE: _('sent message'), |
178 notification.TYPE_MENTION: _('mentioned you'), | 205 notification.TYPE_MENTION: _('mentioned you'), |
179 notification.TYPE_REGISTRATION: _('registered in RhodeCode') | 206 notification.TYPE_REGISTRATION: _('registered in RhodeCode'), |
207 notification.TYPE_PULL_REQUEST: _('opened new pull request') | |
180 } | 208 } |
181 | 209 |
182 tmpl = "%(user)s %(action)s %(when)s" | 210 tmpl = "%(user)s %(action)s %(when)s" |
183 if show_age: | 211 if show_age: |
184 when = h.age(notification.created_on) | 212 when = h.age(notification.created_on) |