annotate rhodecode/model/notification.py @ 1703:f23828b00b21 beta

notification fixes and improvements
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 20 Nov 2011 22:26:55 +0200
parents 8cb7f5c4d494
children cac5109ac3b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 rhodecode.model.notification
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Model for notifications
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 :created_on: Nov 20, 2011
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 :author: marcink
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 :license: GPLv3, see COPYING for more details.
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 """
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # This program is free software: you can redistribute it and/or modify
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 # it under the terms of the GNU General Public License as published by
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # the Free Software Foundation, either version 3 of the License, or
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 # (at your option) any later version.
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 #
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # This program is distributed in the hope that it will be useful,
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 # GNU General Public License for more details.
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 #
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # You should have received a copy of the GNU General Public License
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 import logging
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import traceback
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 from pylons.i18n.translation import _
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 from rhodecode.lib import safe_unicode
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 from rhodecode.lib.caching_query import FromCache
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 from rhodecode.model import BaseModel
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 from rhodecode.model.db import Notification, User, UserNotification
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 class NotificationModel(BaseModel):
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40
1703
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
41 def create(self, created_by, subject, body, recipients,
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
42 type_=Notification.TYPE_MESSAGE):
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
43 """
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
44
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
45 Creates notification of given type
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
46
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
47 :param created_by: int, str or User instance. User who created this
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
48 notification
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
49 :param subject:
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
50 :param body:
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
51 :param recipients: list of int, str or User objects
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
52 :param type_: type of notification
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
53 """
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 if not getattr(recipients, '__iter__', False):
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 raise Exception('recipients must be a list of iterable')
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57
1703
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
58 created_by_obj = created_by
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
59 if not isinstance(created_by, User):
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
60 created_by_obj = User.get(created_by)
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62
1703
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
63 recipients_objs = []
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
64 for u in recipients:
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
65 if isinstance(u, User):
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
66 recipients_objs.append(u)
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
67 elif isinstance(u, basestring):
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
68 recipients_objs.append(User.get_by_username(username=u))
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
69 elif isinstance(u, int):
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
70 recipients_objs.append(User.get(u))
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
71 else:
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
72 raise Exception('Unsupported recipient must be one of int,'
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
73 'str or User object')
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
74
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
75 Notification.create(created_by=created_by_obj, subject=subject,
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
76 body = body, recipients = recipients_objs,
f23828b00b21 notification fixes and improvements
Marcin Kuzminski <marcin@python-works.com>
parents: 1702
diff changeset
77 type_=type_)
1702
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 def get_for_user(self, user_id):
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 return User.get(user_id).notifications
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 def get_unread_cnt_for_user(self, user_id):
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 return UserNotification.query()\
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 .filter(UserNotification.sent_on == None)\
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 .filter(UserNotification.user_id == user_id).count()
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 def get_unread_for_user(self, user_id):
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 return [x.notification for x in UserNotification.query()\
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 .filter(UserNotification.sent_on == None)\
8cb7f5c4d494 #302 - basic notification system, models+tests
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 .filter(UserNotification.user_id == user_id).all()]