Mercurial > kallithea
diff 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 |
line wrap: on
line diff
--- a/rhodecode/model/notification.py Tue Nov 22 14:10:33 2011 +0200 +++ b/rhodecode/model/notification.py Wed Nov 23 00:55:05 2011 +0200 @@ -29,15 +29,38 @@ from pylons.i18n.translation import _ -from rhodecode.lib import safe_unicode -from rhodecode.lib.caching_query import FromCache +from rhodecode.lib.helpers import age from rhodecode.model import BaseModel from rhodecode.model.db import Notification, User, UserNotification +log = logging.getLogger(__name__) class NotificationModel(BaseModel): + + def __get_user(self, user): + if isinstance(user, User): + return user + elif isinstance(user, basestring): + return User.get_by_username(username=user) + elif isinstance(user, int): + return User.get(user) + else: + raise Exception('Unsupported user must be one of int,' + 'str or User object') + + def __get_notification(self, notification): + if isinstance(notification, Notification): + return notification + elif isinstance(notification, int): + return Notification.get(notification) + else: + if notification: + raise Exception('notification must be int or Instance' + ' of Notification got %s' % type(notification)) + + def create(self, created_by, subject, body, recipients, type_=Notification.TYPE_MESSAGE): """ @@ -55,37 +78,61 @@ if not getattr(recipients, '__iter__', False): raise Exception('recipients must be a list of iterable') - created_by_obj = created_by - if not isinstance(created_by, User): - created_by_obj = User.get(created_by) - + created_by_obj = self.__get_user(created_by) recipients_objs = [] for u in recipients: - if isinstance(u, User): - recipients_objs.append(u) - elif isinstance(u, basestring): - recipients_objs.append(User.get_by_username(username=u)) - elif isinstance(u, int): - recipients_objs.append(User.get(u)) - else: - raise Exception('Unsupported recipient must be one of int,' - 'str or User object') - - Notification.create(created_by=created_by_obj, subject=subject, - body = body, recipients = recipients_objs, + recipients_objs.append(self.__get_user(u)) + recipients_objs = set(recipients_objs) + return Notification.create(created_by=created_by_obj, subject=subject, + body=body, recipients=recipients_objs, type_=type_) + def delete(self, notification_id): + # we don't want to remove actuall notification just the assignment + try: + notification_id = int(notification_id) + no = self.__get_notification(notification_id) + if no: + UserNotification.delete(no.notifications_to_users.user_to_notification_id) + return True + except Exception: + log.error(traceback.format_exc()) + raise def get_for_user(self, user_id): return User.get(user_id).notifications def get_unread_cnt_for_user(self, user_id): return UserNotification.query()\ - .filter(UserNotification.sent_on == None)\ + .filter(UserNotification.read == False)\ .filter(UserNotification.user_id == user_id).count() def get_unread_for_user(self, user_id): return [x.notification for x in UserNotification.query()\ - .filter(UserNotification.sent_on == None)\ + .filter(UserNotification.read == False)\ .filter(UserNotification.user_id == user_id).all()] + + def get_user_notification(self, user, notification): + user = self.__get_user(user) + notification = self.__get_notification(notification) + + return UserNotification.query()\ + .filter(UserNotification.notification == notification)\ + .filter(UserNotification.user == user).scalar() + + def make_description(self, notification): + """ + Creates a human readable description based on properties + of notification object + """ + + _map = {notification.TYPE_CHANGESET_COMMENT:_('commented on commit'), + notification.TYPE_MESSAGE:_('sent message'), + notification.TYPE_MENTION:_('mentioned you')} + + tmpl = "%(user)s %(action)s %(when)s" + data = dict(user=notification.created_by_user.username, + action=_map[notification.type_], + when=age(notification.created_on)) + return tmpl % data