22 lines
888 B
Python
22 lines
888 B
Python
import zoneinfo
|
|
from django.utils import timezone
|
|
from django.shortcuts import render
|
|
|
|
class TimezoneMiddleware:
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
|
|
def __call__(self, request):
|
|
tz = request.COOKIES.get("user_tz")
|
|
if tz:
|
|
if request.user.is_authenticated:
|
|
if not 'user_timezone' in request.user.user_profile.json_data or request.user.user_profile.json_data['user_timezone'] != tz:
|
|
request.user.user_profile.json_data['user_timezone'] = tz
|
|
request.user.user_profile.save(update_fields=['json_data'])
|
|
|
|
msg = f'user={str(request.user.id)} tz={str(tz)}'
|
|
print(msg)
|
|
timezone.activate(zoneinfo.ZoneInfo(tz))
|
|
else:
|
|
timezone.activate(zoneinfo.ZoneInfo("UTC"))
|
|
return self.get_response(request) |