Upgraded django.contrib.sessions to be compatible with time zone support.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17121 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-11-20 10:33:44 +00:00
parent 1240c8332d
commit 4ac594f8a5
3 changed files with 18 additions and 9 deletions

View File

@ -12,6 +12,7 @@ except ImportError:
from django.conf import settings from django.conf import settings
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.utils.crypto import constant_time_compare, salted_hmac 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. # Use the system (hardware-based) random number generator if it exists.
if hasattr(random, 'SystemRandom'): if hasattr(random, 'SystemRandom'):
@ -188,7 +189,7 @@ class SessionBase(object):
return settings.SESSION_COOKIE_AGE return settings.SESSION_COOKIE_AGE
if not isinstance(expiry, datetime): if not isinstance(expiry, datetime):
return expiry return expiry
delta = expiry - datetime.now() delta = expiry - timezone.now()
return delta.days * 86400 + delta.seconds return delta.days * 86400 + delta.seconds
def get_expiry_date(self): def get_expiry_date(self):
@ -198,7 +199,7 @@ class SessionBase(object):
return expiry return expiry
if not expiry: # Checks both None and 0 cases if not expiry: # Checks both None and 0 cases
expiry = settings.SESSION_COOKIE_AGE expiry = settings.SESSION_COOKIE_AGE
return datetime.now() + timedelta(seconds=expiry) return timezone.now() + timedelta(seconds=expiry)
def set_expiry(self, value): def set_expiry(self, value):
""" """
@ -223,7 +224,7 @@ class SessionBase(object):
pass pass
return return
if isinstance(value, timedelta): if isinstance(value, timedelta):
value = datetime.now() + value value = timezone.now() + value
self['_session_expiry'] = value self['_session_expiry'] = value
def get_expire_at_browser_close(self): def get_expire_at_browser_close(self):

View File

@ -1,8 +1,8 @@
import datetime
from django.contrib.sessions.backends.base import SessionBase, CreateError from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError, transaction, router from django.db import IntegrityError, transaction, router
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils import timezone
class SessionStore(SessionBase): class SessionStore(SessionBase):
@ -16,7 +16,7 @@ class SessionStore(SessionBase):
try: try:
s = Session.objects.get( s = Session.objects.get(
session_key = self.session_key, session_key = self.session_key,
expire_date__gt=datetime.datetime.now() expire_date__gt=timezone.now()
) )
return self.decode(force_unicode(s.session_data)) return self.decode(force_unicode(s.session_data))
except (Session.DoesNotExist, SuspiciousOperation): except (Session.DoesNotExist, SuspiciousOperation):

View File

@ -16,6 +16,7 @@ from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
from django.http import HttpResponse from django.http import HttpResponse
from django.test import TestCase, RequestFactory from django.test import TestCase, RequestFactory
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils import timezone
from django.utils import unittest from django.utils import unittest
@ -187,7 +188,7 @@ class SessionTestsMixin(object):
def test_custom_expiry_seconds(self): def test_custom_expiry_seconds(self):
# Using seconds # Using seconds
self.session.set_expiry(10) 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)) self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age() age = self.session.get_expiry_age()
@ -196,7 +197,7 @@ class SessionTestsMixin(object):
def test_custom_expiry_timedelta(self): def test_custom_expiry_timedelta(self):
# Using timedelta # Using timedelta
self.session.set_expiry(timedelta(seconds=10)) 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)) self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age() age = self.session.get_expiry_age()
@ -204,8 +205,8 @@ class SessionTestsMixin(object):
def test_custom_expiry_datetime(self): def test_custom_expiry_datetime(self):
# Using fixed datetime # Using fixed datetime
self.session.set_expiry(datetime.now() + timedelta(seconds=10)) self.session.set_expiry(timezone.now() + 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)) self.assertTrue(delta.seconds in (9, 10))
age = self.session.get_expiry_age() age = self.session.get_expiry_age()
@ -279,10 +280,17 @@ class DatabaseSessionTests(SessionTestsMixin, TestCase):
self.assertEqual(self.session['y'], 2) self.assertEqual(self.session['y'], 2)
DatabaseSessionWithTimeZoneTests = override_settings(USE_TZ=True)(DatabaseSessionTests)
class CacheDBSessionTests(SessionTestsMixin, TestCase): class CacheDBSessionTests(SessionTestsMixin, TestCase):
backend = CacheDBSession 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 # Don't need DB flushing for these tests, so can use unittest.TestCase as base class
class FileSessionTests(SessionTestsMixin, unittest.TestCase): class FileSessionTests(SessionTestsMixin, unittest.TestCase):