emails: Apply the user's hour format in login notification timestamps.

The timestamp used for new login notifications always used the 12-hour
format. Instead of that, we use now the one preferred by the user, as
reflected in their settings.
This commit is contained in:
Yago González 2018-08-13 23:00:51 +02:00 committed by Tim Abbott
parent 75b39866c7
commit 53b9118e97
2 changed files with 17 additions and 1 deletions

View File

@ -79,7 +79,11 @@ def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: A
if user_tz == '':
user_tz = timezone_get_current_timezone_name()
local_time = timezone_now().astimezone(get_timezone(user_tz))
context['login_time'] = local_time.strftime('%A, %B %d, %Y at %I:%M%p %Z')
if user.twenty_four_hour_time:
hhmm_string = local_time.strftime('%H:%M')
else:
hhmm_string = local_time.strftime('%I:%M%p')
context['login_time'] = local_time.strftime('%A, %B %d, %Y at {} %Z'.format(hhmm_string))
context['device_ip'] = request.META.get('REMOTE_ADDR') or _("Unknown IP address")
context['device_os'] = get_device_os(user_agent)
context['device_browser'] = get_device_browser(user_agent)

View File

@ -30,6 +30,7 @@ class SendLoginEmailTest(ZulipTestCase):
user = self.example_user('hamlet')
user.timezone = 'US/Pacific'
user.twenty_four_hour_time = False
user.date_joined = mock_time - datetime.timedelta(seconds=JUST_CREATED_THRESHOLD + 1)
user.save()
password = initial_password(user.email)
@ -48,6 +49,17 @@ class SendLoginEmailTest(ZulipTestCase):
# local time is correct and in email's body
self.assertIn(reference_time, mail.outbox[0].body)
# Try again with the 24h time format setting enabled for this user
self.logout() # We just logged in, we'd be redirected without this
user.twenty_four_hour_time = True
user.save()
with mock.patch('zerver.signals.timezone_now', return_value=mock_time):
self.client_post("/accounts/login/", info={"username": user.email, "password": password},
HTTP_USER_AGENT=firefox_windows)
reference_time = mock_time.astimezone(user_tz).strftime('%A, %B %d, %Y at %H:%M %Z')
self.assertIn(reference_time, mail.outbox[1].body)
def test_dont_send_login_emails_if_send_login_emails_is_false(self) -> None:
self.assertFalse(settings.SEND_LOGIN_EMAILS)
email = self.example_email('hamlet')