diff --git a/parkkihubi/middleware.py b/parkkihubi/middleware.py new file mode 100644 index 00000000..6faa2744 --- /dev/null +++ b/parkkihubi/middleware.py @@ -0,0 +1,31 @@ +import contextlib +from functools import lru_cache + +from django.conf import settings +from django.urls import reverse +from django.utils import timezone + +# No-op context manager +# +# Usable like the one coming in Python 3.7, see +# https://github.com/python/cpython/commit/0784a2e5b174d2dbf7b144d480559e650c5cf64c +nullcontext = contextlib.ExitStack + + +class AdminTimezoneMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + tz_overrider = nullcontext() + if request.path.startswith(get_admin_url_path_prefix()): + admin_tz = getattr(settings, 'ADMIN_TIME_ZONE', None) + if admin_tz: + tz_overrider = timezone.override(admin_tz) + with tz_overrider: + return self.get_response(request) + + +@lru_cache() +def get_admin_url_path_prefix(): + return reverse('admin:index') diff --git a/parkkihubi/settings.py b/parkkihubi/settings.py index f8f98c22..365f874a 100644 --- a/parkkihubi/settings.py +++ b/parkkihubi/settings.py @@ -87,10 +87,11 @@ ############## # Middleware # ############## -MIDDLEWARE_CLASSES = [ +MIDDLEWARE = [ 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', + 'parkkihubi.middleware.AdminTimezoneMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', @@ -133,7 +134,8 @@ # Languages & Localization # ############################ LANGUAGE_CODE = 'en' -TIME_ZONE = 'Europe/Helsinki' +TIME_ZONE = 'UTC' +ADMIN_TIME_ZONE = 'Europe/Helsinki' USE_I18N = True USE_L10N = True USE_TZ = True