forked from City-of-Helsinki/parkkihubi
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-configure timezones for API and Admin
Use UTC as Django default timezone so that Django Rest Framework will use it for the API endpoints, but override the timezone in Django Admin to still be Europe/Helsinki with a middleware.
- Loading branch information
1 parent
5760537
commit adb4231
Showing
2 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters