Hide keyboard shortcuts

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 

2 

3from django.conf import settings 

4from django.utils import timezone 

5import pytz 

6 

7# Exports 

8 

9__all__ = ( 

10 "TimeZoneMiddleware", 

11) 

12 

13# Constants 

14 

15TIMEZONE_KEY = getattr(settings, "SUPERDJANGO_TIMEZONE_KEY", "django_timezone") 

16"""The session key used to identify the user's timezone.""" 

17 

18# Classes 

19 

20 

21class TimeZoneMiddleware: 

22 """Set the current time zone; either that of the user or the system default. 

23 

24 This is pretty much straight from the example found in Django documentation, but with new-style middleware. 

25 

26 See: https://docs.djangoproject.com/en/stable/topics/i18n/timezones/#selecting-the-current-time-zone 

27 

28 """ 

29 

30 def __init__(self, get_response): 

31 self.get_response = get_response 

32 

33 def __call__(self, request): 

34 timezone_name = request.session.get(TIMEZONE_KEY) 

35 

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() 

42 

43 return self.get_response(request)