Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.utils import timezone
5import pytz
7# Exports
9__all__ = (
10 "TimeZoneMiddleware",
11)
13# Constants
15TIMEZONE_KEY = getattr(settings, "SUPERDJANGO_TIMEZONE_KEY", "django_timezone")
16"""The session key used to identify the user's timezone."""
18# Classes
21class TimeZoneMiddleware:
22 """Set the current time zone; either that of the user or the system default.
24 This is pretty much straight from the example found in Django documentation, but with new-style middleware.
26 See: https://docs.djangoproject.com/en/stable/topics/i18n/timezones/#selecting-the-current-time-zone
28 """
30 def __init__(self, get_response):
31 self.get_response = get_response
33 def __call__(self, request):
34 timezone_name = request.session.get(TIMEZONE_KEY)
36 if timezone_name:
37 timezone.activate(pytz.timezone(timezone_name))
38 elif hasattr(settings, "TIME_ZONE") and settings.USE_TZ:
39 timezone.activate(pytz.timezone(settings.TIME_ZONE))
40 else:
41 timezone.deactivate()
43 return self.get_response(request)