From 88db9b2451693ab832a9d8c0abcad0a452b3500d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 6 May 2016 23:45:16 +0530 Subject: display datetime as per user timezone --- yaksh/middleware/user_time_zone.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 yaksh/middleware/user_time_zone.py (limited to 'yaksh/middleware/user_time_zone.py') diff --git a/yaksh/middleware/user_time_zone.py b/yaksh/middleware/user_time_zone.py new file mode 100644 index 0000000..f48bb62 --- /dev/null +++ b/yaksh/middleware/user_time_zone.py @@ -0,0 +1,15 @@ +import pytz + +from django.utils import timezone +from yaksh.models import Profile + +class TimezoneMiddleware(object): + """ Middleware to get user's timezone and activate timezone + if user timezone is not available default value 'UTC' is activated """ + def process_request(self, request): + user = request.user + if hasattr(user, 'profile'): + user_tz = user.profile.timezone + timezone.activate(pytz.timezone(user_tz)) + else: + timezone.activate(pytz.timezone('UTC')) -- cgit