changeset 5114:76abae776a3e

notifications: use different strings for descriptions with age and datetime Improves both English wording and makes it easier to translate correctly.
author Peter Vitt <peter.vitt2@uni-siegen.de>
date Sun, 10 May 2015 21:40:12 +0200
parents c69cb0647c8a
children 4cad3a52e0ed
files kallithea/model/notification.py kallithea/tests/functional/test_admin_notifications.py
diffstat 2 files changed, 59 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/model/notification.py	Wed May 06 17:48:15 2015 -0400
+++ b/kallithea/model/notification.py	Sun May 10 21:40:12 2015 +0200
@@ -248,25 +248,31 @@
         """
         #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):
--- a/kallithea/tests/functional/test_admin_notifications.py	Wed May 06 17:48:15 2015 -0400
+++ b/kallithea/tests/functional/test_admin_notifications.py	Sun May 10 21:40:12 2015 +0200
@@ -4,6 +4,7 @@
 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 @@
 
         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)
+                )
+            )