Browse Source

Merge "Fixing timezone awareness"

tags/9.0.0.0b3^0
Jenkins 3 years ago
parent
commit
66ea48a69f
2 changed files with 31 additions and 0 deletions
  1. 5
    0
      horizon/middleware.py
  2. 26
    0
      horizon/test/tests/middleware.py

+ 5
- 0
horizon/middleware.py View File

@@ -29,6 +29,7 @@ from django.contrib import messages as django_messages
29 29
 from django import http
30 30
 from django import shortcuts
31 31
 from django.utils.encoding import iri_to_uri  # noqa
32
+from django.utils import timezone
32 33
 from django.utils.translation import ugettext_lazy as _
33 34
 
34 35
 from openstack_auth import views as auth_views
@@ -106,6 +107,10 @@ class HorizonMiddleware(object):
106 107
                         }
107 108
                     )
108 109
 
110
+        tz = request.session.get('django_timezone')
111
+        if tz:
112
+            timezone.activate(tz)
113
+
109 114
     def process_exception(self, request, exception):
110 115
         """Catches internal Horizon exception classes such as NotAuthorized,
111 116
         NotFound and Http302 and handles them gracefully.

+ 26
- 0
horizon/test/tests/middleware.py View File

@@ -16,6 +16,7 @@
16 16
 from django.conf import settings
17 17
 
18 18
 from django.http import HttpResponseRedirect  # noqa
19
+from django.utils import timezone
19 20
 
20 21
 from horizon import exceptions
21 22
 from horizon import middleware
@@ -23,6 +24,15 @@ from horizon.test import helpers as test
23 24
 
24 25
 
25 26
 class MiddlewareTests(test.TestCase):
27
+
28
+    def setUp(self):
29
+        self._timezone_backup = timezone.get_current_timezone_name()
30
+        return super(MiddlewareTests, self).setUp()
31
+
32
+    def tearDown(self):
33
+        timezone.activate(self._timezone_backup)
34
+        return super(MiddlewareTests, self).tearDown()
35
+
26 36
     def test_redirect_login_fail_to_login(self):
27 37
         url = settings.LOGIN_URL
28 38
         request = self.factory.post(url)
@@ -49,3 +59,19 @@ class MiddlewareTests(test.TestCase):
49 59
         resp = mw.process_response(request, response)
50 60
         self.assertEqual(200, resp.status_code)
51 61
         self.assertEqual(url, resp['X-Horizon-Location'])
62
+
63
+    def test_timezone_awareness(self):
64
+        url = settings.LOGIN_REDIRECT_URL
65
+        mw = middleware.HorizonMiddleware()
66
+
67
+        request = self.factory.get(url)
68
+        request.session['django_timezone'] = 'America/Chicago'
69
+        mw.process_request(request)
70
+        self.assertEqual(
71
+            timezone.get_current_timezone_name(), 'America/Chicago')
72
+        request.session['django_timezone'] = 'Europe/Paris'
73
+        mw.process_request(request)
74
+        self.assertEqual(timezone.get_current_timezone_name(), 'Europe/Paris')
75
+        request.session['django_timezone'] = 'UTC'
76
+        mw.process_request(request)
77
+        self.assertEqual(timezone.get_current_timezone_name(), 'UTC')

Loading…
Cancel
Save