1 from django.utils import timezone
2 from django.utils.deprecation import MiddlewareMixin
4 from account.models import UserProfile
7 class TimezoneMiddleware(MiddlewareMixin):
9 Activate the timezone from request.user.userprofile if user is authenticated,
10 deactivate the timezone otherwise and use default (UTC)
12 def process_request(self, request):
13 if request.user.is_authenticated:
15 tz = request.user.userprofile.timezone
17 except UserProfile.DoesNotExist:
18 UserProfile.objects.create(user=request.user)
19 tz = request.user.userprofile.timezone