2021-06-18 21:12:56 +00:00
|
|
|
""" Makes the app aware of the users timezone """
|
2021-03-29 12:20:35 +00:00
|
|
|
import pytz
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
|
|
|
class TimezoneMiddleware:
|
2021-06-18 21:12:56 +00:00
|
|
|
"""Determine the timezone based on the request"""
|
|
|
|
|
2021-03-29 12:20:35 +00:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
timezone.activate(pytz.timezone(request.user.preferred_timezone))
|
|
|
|
else:
|
|
|
|
timezone.activate(pytz.utc)
|
|
|
|
response = self.get_response(request)
|
|
|
|
timezone.deactivate()
|
|
|
|
return response
|