diff --git a/kallithea/model/notification.py b/kallithea/model/notification.py --- a/kallithea/model/notification.py +++ b/kallithea/model/notification.py @@ -248,25 +248,31 @@ class NotificationModel(BaseModel): """ #alias _n = notification - _map = { - _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset at %(when)s'), - _n.TYPE_MESSAGE: _('%(user)s sent message at %(when)s'), - _n.TYPE_MENTION: _('%(user)s mentioned you at %(when)s'), - _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea at %(when)s'), - _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request at %(when)s'), - _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request at %(when)s') - } - tmpl = _map[notification.type_] if show_age: - when = h.age(notification.created_on) + return { + _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset %(age)s'), + _n.TYPE_MESSAGE: _('%(user)s sent message %(age)s'), + _n.TYPE_MENTION: _('%(user)s mentioned you %(age)s'), + _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea %(age)s'), + _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request %(age)s'), + _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request %(age)s'), + }[notification.type_] % dict( + user=notification.created_by_user.username, + age=h.age(notification.created_on), + ) else: - when = h.fmt_date(notification.created_on) - - return tmpl % dict( - user=notification.created_by_user.username, - when=when, - ) + return { + _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset at %(when)s'), + _n.TYPE_MESSAGE: _('%(user)s sent message at %(when)s'), + _n.TYPE_MENTION: _('%(user)s mentioned you at %(when)s'), + _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea at %(when)s'), + _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request at %(when)s'), + _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request at %(when)s'), + }[notification.type_] % dict( + user=notification.created_by_user.username, + when=h.fmt_date(notification.created_on), + ) class EmailNotificationModel(BaseModel): diff --git a/kallithea/tests/functional/test_admin_notifications.py b/kallithea/tests/functional/test_admin_notifications.py --- a/kallithea/tests/functional/test_admin_notifications.py +++ b/kallithea/tests/functional/test_admin_notifications.py @@ -4,6 +4,7 @@ from kallithea.model.db import Notificat from kallithea.model.user import UserModel from kallithea.model.notification import NotificationModel from kallithea.model.meta import Session +from kallithea.lib import helpers as h class TestNotificationsController(TestController): @@ -89,3 +90,39 @@ class TestNotificationsController(TestCo response.mustcontain(subject) response.mustcontain(notif_body) + + def test_description_with_age(self): + self.log_user() + cur_user = self._get_logged_user() + subject = u'test' + notify_body = u'hi there' + notification = NotificationModel().create(created_by = cur_user, + subject = subject, + body = notify_body) + + description = NotificationModel().make_description(notification) + self.assertEqual( + description, + "{0} sent message {1}".format( + cur_user.username, + h.age(notification.created_on) + ) + ) + + def test_description_with_datetime(self): + self.log_user() + cur_user = self._get_logged_user() + subject = u'test' + notify_body = u'hi there' + notification = NotificationModel().create(created_by = cur_user, + subject = subject, + body = notify_body) + + description = NotificationModel().make_description(notification, False) + self.assertEqual( + description, + "{0} sent message at {1}".format( + cur_user.username, + h.fmt_date(notification.created_on) + ) + )