blob: 8140851eacd51933a8399f8d9aef834daa5048e0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import pytz
from django.utils import timezone
class TimezoneMiddleware(object):
""" Middleware to get user's timezone and activate timezone
if user timezone is not available default value 'Asia/Kolkata'
is activated
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
user = request.user
user_tz = 'Asia/Kolkata'
if hasattr(user, 'profile'):
if user.profile.timezone:
user_tz = user.profile.timezone
timezone.activate(pytz.timezone(user_tz))
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
|