Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Cache the IANA name lookup

Roland Geider8e1213988cee
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 5dbe61c56..da7117f35 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -17,7 +17,6 @@
# Standard Library
from collections import defaultdict
from datetime import date
-from zoneinfo import available_timezones
# Django
from django import forms
@@ -57,6 +56,7 @@ from django_recaptcha.widgets import ReCaptchaV3
# wger
from wger.core.models import UserProfile
+from wger.core.models.profile import available_timezone_names
class PasswordInputWithToggle(PasswordInput):
@@ -139,7 +139,7 @@ def timezone_choices():
limits what the dropdown offers, not what the API stores.
"""
groups = defaultdict(list)
- for name in sorted(available_timezones()):
+ for name in sorted(available_timezone_names()):
if '/' not in name or name.startswith('Etc/'):
continue
region, _, city = name.partition('/')
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index 88eb65289..cf14f36bf 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -17,6 +17,7 @@
# Standard Library
import datetime
import decimal
+from functools import lru_cache
from zoneinfo import (
ZoneInfo,
ZoneInfoNotFoundError,
@@ -70,13 +71,21 @@ def birthdate_validator(birthdate):
)
+@lru_cache(maxsize=1)
+def available_timezone_names() -> frozenset[str]:
+ """
+ The IANA names, cached: available_timezones() rescans the tzdata on every call
+ """
+ return frozenset(available_timezones())
+
+
def validate_timezone(value: str) -> None:
"""
Validates that the given string is a real IANA timezone name.
An empty value is allowed and means that no client has reported one.
"""
- if value and value not in available_timezones():
+ if value and value not in available_timezone_names():
raise ValidationError(f'"{value}" is not a valid IANA timezone name')