6f7cac7aff3297848728809e4101b403cac94910
[pharos.git] / tools / pharos-dashboard / account / middleware.py
1 from django.utils import timezone
2 from django.utils.deprecation import MiddlewareMixin
3
4 from account.models import UserProfile
5
6
7 class TimezoneMiddleware(MiddlewareMixin):
8     """
9     Activate the timezone from request.user.userprofile if user is authenticated,
10     deactivate the timezone otherwise and use default (UTC)
11     """
12     def process_request(self, request):
13         if request.user.is_authenticated:
14             try:
15                 tz = request.user.userprofile.timezone
16                 timezone.activate(tz)
17             except UserProfile.DoesNotExist:
18                 UserProfile.objects.create(user=request.user)
19                 tz = request.user.userprofile.timezone
20                 timezone.activate(tz)
21         else:
22             timezone.deactivate()