comparison rhodecode/model/notification.py @ 1702:8cb7f5c4d494 beta

#302 - basic notification system, models+tests
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 20 Nov 2011 01:53:00 +0200
parents
children f23828b00b21
comparison
equal deleted inserted replaced
1701:b702d0d4b030 1702:8cb7f5c4d494
1 # -*- coding: utf-8 -*-
2 """
3 rhodecode.model.notification
4 ~~~~~~~~~~~~~~
5
6 Model for notifications
7
8
9 :created_on: Nov 20, 2011
10 :author: marcink
11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
12 :license: GPLv3, see COPYING for more details.
13 """
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 3 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26
27 import logging
28 import traceback
29
30 from pylons.i18n.translation import _
31
32 from rhodecode.lib import safe_unicode
33 from rhodecode.lib.caching_query import FromCache
34
35 from rhodecode.model import BaseModel
36 from rhodecode.model.db import Notification, User, UserNotification
37
38
39 class NotificationModel(BaseModel):
40
41 def create(self, subject, body, recipients):
42
43 if not getattr(recipients, '__iter__', False):
44 raise Exception('recipients must be a list of iterable')
45
46 for x in recipients:
47 if not isinstance(x, User):
48 raise Exception('recipient is not instance of %s got %s' % \
49 (User, type(x)))
50
51
52 Notification.create(subject, body, recipients)
53
54
55 def get_for_user(self, user_id):
56 return User.get(user_id).notifications
57
58 def get_unread_cnt_for_user(self, user_id):
59 return UserNotification.query()\
60 .filter(UserNotification.sent_on == None)\
61 .filter(UserNotification.user_id == user_id).count()
62
63 def get_unread_for_user(self, user_id):
64 return [x.notification for x in UserNotification.query()\
65 .filter(UserNotification.sent_on == None)\
66 .filter(UserNotification.user_id == user_id).all()]