Fix unusable LOCALTZ

Fix #803

On some systems, there is no configured local timezone, so LOCALTZ is
a fallback object, that don't fit use case for flask-babel.
Detect this and use 'UTC' instead
This commit is contained in:
Glandos 2021-07-18 01:32:18 +02:00
parent 9444c1099a
commit 81c3b8edc7

View file

@ -9,6 +9,7 @@ from flask_mail import Mail
from flask_migrate import Migrate, stamp, upgrade from flask_migrate import Migrate, stamp, upgrade
from jinja2 import pass_context from jinja2 import pass_context
from markupsafe import Markup from markupsafe import Markup
import pytz
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
from ihatemoney import default_settings from ihatemoney import default_settings
@ -159,7 +160,15 @@ def create_app(
# Translations and time zone (used to display dates). The timezone is # Translations and time zone (used to display dates). The timezone is
# taken from the BABEL_DEFAULT_TIMEZONE settings, and falls back to # taken from the BABEL_DEFAULT_TIMEZONE settings, and falls back to
# the local timezone of the server OS by using LOCALTZ. # the local timezone of the server OS by using LOCALTZ.
babel = Babel(app, default_timezone=str(LOCALTZ))
# On some bare systems, LOCALTZ is a fake object unusable by Flask-Babel, so use UTC instead
default_timezone = "UTC"
try:
pytz.timezone(str(LOCALTZ))
default_timezone = str(LOCALTZ)
except pytz.exceptions.UnknownTimeZoneError:
pass
babel = Babel(app, default_timezone=default_timezone)
# Undocumented currencyformat filter from flask_babel is forwarding to Babel format_currency # Undocumented currencyformat filter from flask_babel is forwarding to Babel format_currency
# We overwrite it to remove the currency sign ¤ when there is no currency # We overwrite it to remove the currency sign ¤ when there is no currency