Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Upgraded django.contrib.sessions to be compatible with time zone supp…

…ort.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17121 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4ac594f8a55c94f6192fb33bb8f7b8a8ca671360 1 parent 1240c83
@aaugustin aaugustin authored
View
7 django/contrib/sessions/backends/base.py
@@ -12,6 +12,7 @@
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.utils.crypto import constant_time_compare, salted_hmac
+from django.utils import timezone
# Use the system (hardware-based) random number generator if it exists.
if hasattr(random, 'SystemRandom'):
@@ -188,7 +189,7 @@ def get_expiry_age(self):
return settings.SESSION_COOKIE_AGE
if not isinstance(expiry, datetime):
return expiry
- delta = expiry - datetime.now()
+ delta = expiry - timezone.now()
return delta.days * 86400 + delta.seconds
def get_expiry_date(self):
@@ -198,7 +199,7 @@ def get_expiry_date(self):
return expiry
if not expiry: # Checks both None and 0 cases
expiry = settings.SESSION_COOKIE_AGE
- return datetime.now() + timedelta(seconds=expiry)
+ return timezone.now() + timedelta(seconds=expiry)
def set_expiry(self, value):
"""
@@ -223,7 +224,7 @@ def set_expiry(self, value):
pass
return
if isinstance(value, timedelta):
- value = datetime.now() + value
+ value = timezone.now() + value
self['_session_expiry'] = value
def get_expire_at_browser_close(self):
View
4 django/contrib/sessions/backends/db.py
@@ -1,8 +1,8 @@
-import datetime
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError, transaction, router
from django.utils.encoding import force_unicode
+from django.utils import timezone
class SessionStore(SessionBase):
@@ -16,7 +16,7 @@ def load(self):
try:
s = Session.objects.get(
session_key = self.session_key,
- expire_date__gt=datetime.datetime.now()
+ expire_date__gt=timezone.now()
)
return self.decode(force_unicode(s.session_data))
except (Session.DoesNotExist, SuspiciousOperation):
View
16 django/contrib/sessions/tests.py
@@ -16,6 +16,7 @@
from django.http import HttpResponse
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
+from django.utils import timezone
from django.utils import unittest
@@ -187,7 +188,7 @@ def test_default_expiry(self):
def test_custom_expiry_seconds(self):
# Using seconds
self.session.set_expiry(10)
- delta = self.session.get_expiry_date() - datetime.now()
+ delta = self.session.get_expiry_date() - timezone.now()
self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age()
@@ -196,7 +197,7 @@ def test_custom_expiry_seconds(self):
def test_custom_expiry_timedelta(self):
# Using timedelta
self.session.set_expiry(timedelta(seconds=10))
- delta = self.session.get_expiry_date() - datetime.now()
+ delta = self.session.get_expiry_date() - timezone.now()
self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age()
@@ -204,8 +205,8 @@ def test_custom_expiry_timedelta(self):
def test_custom_expiry_datetime(self):
# Using fixed datetime
- self.session.set_expiry(datetime.now() + timedelta(seconds=10))
- delta = self.session.get_expiry_date() - datetime.now()
+ self.session.set_expiry(timezone.now() + timedelta(seconds=10))
+ delta = self.session.get_expiry_date() - timezone.now()
self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age()
@@ -279,10 +280,17 @@ def test_sessionmanager_save(self):
self.assertEqual(self.session['y'], 2)
+DatabaseSessionWithTimeZoneTests = override_settings(USE_TZ=True)(DatabaseSessionTests)
+
+
class CacheDBSessionTests(SessionTestsMixin, TestCase):
backend = CacheDBSession
+
+CacheDBSessionWithTimeZoneTests = override_settings(USE_TZ=True)(CacheDBSessionTests)
+
+
# Don't need DB flushing for these tests, so can use unittest.TestCase as base class
class FileSessionTests(SessionTestsMixin, unittest.TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.