Mercurial > kallithea
comparison rhodecode/model/notification.py @ 1712:cac5109ac3b6 beta
Notification system improvements
- deleting
- tests
- ui
- moved to separate controller
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 23 Nov 2011 00:55:05 +0200 |
parents | f23828b00b21 |
children | 54687aa00724 |
comparison
equal
deleted
inserted
replaced
1711:b369bec5d468 | 1712:cac5109ac3b6 |
---|---|
27 import logging | 27 import logging |
28 import traceback | 28 import traceback |
29 | 29 |
30 from pylons.i18n.translation import _ | 30 from pylons.i18n.translation import _ |
31 | 31 |
32 from rhodecode.lib import safe_unicode | 32 from rhodecode.lib.helpers import age |
33 from rhodecode.lib.caching_query import FromCache | |
34 | 33 |
35 from rhodecode.model import BaseModel | 34 from rhodecode.model import BaseModel |
36 from rhodecode.model.db import Notification, User, UserNotification | 35 from rhodecode.model.db import Notification, User, UserNotification |
37 | 36 |
37 log = logging.getLogger(__name__) | |
38 | 38 |
39 class NotificationModel(BaseModel): | 39 class NotificationModel(BaseModel): |
40 | |
41 | |
42 def __get_user(self, user): | |
43 if isinstance(user, User): | |
44 return user | |
45 elif isinstance(user, basestring): | |
46 return User.get_by_username(username=user) | |
47 elif isinstance(user, int): | |
48 return User.get(user) | |
49 else: | |
50 raise Exception('Unsupported user must be one of int,' | |
51 'str or User object') | |
52 | |
53 def __get_notification(self, notification): | |
54 if isinstance(notification, Notification): | |
55 return notification | |
56 elif isinstance(notification, int): | |
57 return Notification.get(notification) | |
58 else: | |
59 if notification: | |
60 raise Exception('notification must be int or Instance' | |
61 ' of Notification got %s' % type(notification)) | |
62 | |
40 | 63 |
41 def create(self, created_by, subject, body, recipients, | 64 def create(self, created_by, subject, body, recipients, |
42 type_=Notification.TYPE_MESSAGE): | 65 type_=Notification.TYPE_MESSAGE): |
43 """ | 66 """ |
44 | 67 |
53 """ | 76 """ |
54 | 77 |
55 if not getattr(recipients, '__iter__', False): | 78 if not getattr(recipients, '__iter__', False): |
56 raise Exception('recipients must be a list of iterable') | 79 raise Exception('recipients must be a list of iterable') |
57 | 80 |
58 created_by_obj = created_by | 81 created_by_obj = self.__get_user(created_by) |
59 if not isinstance(created_by, User): | |
60 created_by_obj = User.get(created_by) | |
61 | |
62 | 82 |
63 recipients_objs = [] | 83 recipients_objs = [] |
64 for u in recipients: | 84 for u in recipients: |
65 if isinstance(u, User): | 85 recipients_objs.append(self.__get_user(u)) |
66 recipients_objs.append(u) | 86 recipients_objs = set(recipients_objs) |
67 elif isinstance(u, basestring): | 87 return Notification.create(created_by=created_by_obj, subject=subject, |
68 recipients_objs.append(User.get_by_username(username=u)) | 88 body=body, recipients=recipients_objs, |
69 elif isinstance(u, int): | |
70 recipients_objs.append(User.get(u)) | |
71 else: | |
72 raise Exception('Unsupported recipient must be one of int,' | |
73 'str or User object') | |
74 | |
75 Notification.create(created_by=created_by_obj, subject=subject, | |
76 body = body, recipients = recipients_objs, | |
77 type_=type_) | 89 type_=type_) |
78 | 90 |
91 def delete(self, notification_id): | |
92 # we don't want to remove actuall notification just the assignment | |
93 try: | |
94 notification_id = int(notification_id) | |
95 no = self.__get_notification(notification_id) | |
96 if no: | |
97 UserNotification.delete(no.notifications_to_users.user_to_notification_id) | |
98 return True | |
99 except Exception: | |
100 log.error(traceback.format_exc()) | |
101 raise | |
79 | 102 |
80 def get_for_user(self, user_id): | 103 def get_for_user(self, user_id): |
81 return User.get(user_id).notifications | 104 return User.get(user_id).notifications |
82 | 105 |
83 def get_unread_cnt_for_user(self, user_id): | 106 def get_unread_cnt_for_user(self, user_id): |
84 return UserNotification.query()\ | 107 return UserNotification.query()\ |
85 .filter(UserNotification.sent_on == None)\ | 108 .filter(UserNotification.read == False)\ |
86 .filter(UserNotification.user_id == user_id).count() | 109 .filter(UserNotification.user_id == user_id).count() |
87 | 110 |
88 def get_unread_for_user(self, user_id): | 111 def get_unread_for_user(self, user_id): |
89 return [x.notification for x in UserNotification.query()\ | 112 return [x.notification for x in UserNotification.query()\ |
90 .filter(UserNotification.sent_on == None)\ | 113 .filter(UserNotification.read == False)\ |
91 .filter(UserNotification.user_id == user_id).all()] | 114 .filter(UserNotification.user_id == user_id).all()] |
115 | |
116 def get_user_notification(self, user, notification): | |
117 user = self.__get_user(user) | |
118 notification = self.__get_notification(notification) | |
119 | |
120 return UserNotification.query()\ | |
121 .filter(UserNotification.notification == notification)\ | |
122 .filter(UserNotification.user == user).scalar() | |
123 | |
124 def make_description(self, notification): | |
125 """ | |
126 Creates a human readable description based on properties | |
127 of notification object | |
128 """ | |
129 | |
130 _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'), | |
131 notification.TYPE_MESSAGE:_('sent message'), | |
132 notification.TYPE_MENTION:_('mentioned you')} | |
133 | |
134 tmpl = "%(user)s %(action)s %(when)s" | |
135 data = dict(user=notification.created_by_user.username, | |
136 action=_map[notification.type_], | |
137 when=age(notification.created_on)) | |
138 return tmpl % data |