Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
signals: Extract function to convert datetime to readable string.
Aman Agrawala694fe18657c
diff --git a/zerver/lib/timestamp.py b/zerver/lib/timestamp.pyindex dfc61df21c0..71c9283877f 100644--- a/zerver/lib/timestamp.py+++ b/zerver/lib/timestamp.py@@ -47,3 +47,11 @@ def timestamp_to_datetime(timestamp: float) -> datetime:def datetime_to_timestamp(dt: datetime) -> int:verify_UTC(dt)return int(dt.timestamp())+++def format_datetime_to_string(dt: datetime, use_twenty_four_hour_time: bool) -> str:+ if use_twenty_four_hour_time:+ hhmm_string = dt.strftime("%H:%M")+ else:+ hhmm_string = dt.strftime("%I:%M %p")+ return dt.strftime(f"%A, %B %d, %Y at {hhmm_string} %Z")diff --git a/zerver/signals.py b/zerver/signals.pyindex bdf6e9f8a9a..dc84f6dc726 100644--- a/zerver/signals.py+++ b/zerver/signals.py@@ -11,6 +11,7 @@ from django.utils.translation import gettext as _from confirmation.models import one_click_unsubscribe_linkfrom zerver.lib.queue import queue_json_publish_rollback_unsafefrom zerver.lib.send_email import EMAIL_DATE_FORMAT, FromAddress+from zerver.lib.timestamp import format_datetime_to_stringfrom zerver.lib.timezone import canonicalize_timezonefrom zerver.models import UserProfile@@ -93,11 +94,7 @@ def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: Aif user_tz == "":user_tz = timezone_get_current_timezone_name()local_time = timezone_now().astimezone(zoneinfo.ZoneInfo(canonicalize_timezone(user_tz)))- 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(f"%A, %B %d, %Y at {hhmm_string} %Z")+ context["login_time"] = format_datetime_to_string(local_time, user.twenty_four_hour_time)context["device_ip"] = request.META.get("REMOTE_ADDR") or _("Unknown IP address")context["device_os"] = get_device_os(user_agent) or _("an unknown operating system")context["device_browser"] = get_device_browser(user_agent) or _("An unknown browser")