Several Django styling fixes in the `contrib.sessions` app.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7725 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1fc0077a95
commit
5db4d60215
|
@ -5,14 +5,15 @@ import random
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from django.conf import settings
|
|
||||||
from django.core.exceptions import SuspiciousOperation
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import cPickle as pickle
|
import cPickle as pickle
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import SuspiciousOperation
|
||||||
|
|
||||||
|
|
||||||
class SessionBase(object):
|
class SessionBase(object):
|
||||||
"""
|
"""
|
||||||
Base class for all Session classes.
|
Base class for all Session classes.
|
||||||
|
@ -169,8 +170,8 @@ class SessionBase(object):
|
||||||
|
|
||||||
def set_expiry(self, value):
|
def set_expiry(self, value):
|
||||||
"""
|
"""
|
||||||
Sets a custom expiration for the session. ``value`` can be an integer, a
|
Sets a custom expiration for the session. ``value`` can be an integer,
|
||||||
Python ``datetime`` or ``timedelta`` object or ``None``.
|
a Python ``datetime`` or ``timedelta`` object or ``None``.
|
||||||
|
|
||||||
If ``value`` is an integer, the session will expire after that many
|
If ``value`` is an integer, the session will expire after that many
|
||||||
seconds of inactivity. If set to ``0`` then the session will expire on
|
seconds of inactivity. If set to ``0`` then the session will expire on
|
||||||
|
|
|
@ -2,6 +2,7 @@ from django.conf import settings
|
||||||
from django.contrib.sessions.backends.base import SessionBase
|
from django.contrib.sessions.backends.base import SessionBase
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
|
||||||
|
|
||||||
class SessionStore(SessionBase):
|
class SessionStore(SessionBase):
|
||||||
"""
|
"""
|
||||||
A cache-based session store.
|
A cache-based session store.
|
||||||
|
@ -23,4 +24,4 @@ class SessionStore(SessionBase):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def delete(self, session_key):
|
def delete(self, session_key):
|
||||||
self._cache.delete(session_key)
|
self._cache.delete(session_key)
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.sessions.models import Session
|
from django.contrib.sessions.models import Session
|
||||||
from django.contrib.sessions.backends.base import SessionBase
|
from django.contrib.sessions.backends.base import SessionBase
|
||||||
from django.core.exceptions import SuspiciousOperation
|
from django.core.exceptions import SuspiciousOperation
|
||||||
import datetime
|
|
||||||
|
|
||||||
class SessionStore(SessionBase):
|
class SessionStore(SessionBase):
|
||||||
"""
|
"""
|
||||||
Implements database session store
|
Implements database session store.
|
||||||
"""
|
"""
|
||||||
def __init__(self, session_key=None):
|
def __init__(self, session_key=None):
|
||||||
super(SessionStore, self).__init__(session_key)
|
super(SessionStore, self).__init__(session_key)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.sessions.backends.base import SessionBase
|
from django.contrib.sessions.backends.base import SessionBase
|
||||||
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
|
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
|
||||||
|
|
||||||
|
|
||||||
class SessionStore(SessionBase):
|
class SessionStore(SessionBase):
|
||||||
"""
|
"""
|
||||||
Implements a file based session store.
|
Implements a file based session store.
|
||||||
|
@ -15,10 +17,10 @@ class SessionStore(SessionBase):
|
||||||
|
|
||||||
# Make sure the storage path is valid.
|
# Make sure the storage path is valid.
|
||||||
if not os.path.isdir(self.storage_path):
|
if not os.path.isdir(self.storage_path):
|
||||||
raise ImproperlyConfigured("The session storage path %r doesn't exist. "\
|
raise ImproperlyConfigured(
|
||||||
"Please set your SESSION_FILE_PATH setting "\
|
"The session storage path %r doesn't exist. Please set your"
|
||||||
"to an existing directory in which Django "\
|
" SESSION_FILE_PATH setting to an existing directory in which"
|
||||||
"can store session data." % self.storage_path)
|
" Django can store session data." % self.storage_path)
|
||||||
|
|
||||||
self.file_prefix = settings.SESSION_COOKIE_NAME
|
self.file_prefix = settings.SESSION_COOKIE_NAME
|
||||||
super(SessionStore, self).__init__(session_key)
|
super(SessionStore, self).__init__(session_key)
|
||||||
|
@ -31,9 +33,11 @@ class SessionStore(SessionBase):
|
||||||
session_key = self.session_key
|
session_key = self.session_key
|
||||||
|
|
||||||
# Make sure we're not vulnerable to directory traversal. Session keys
|
# Make sure we're not vulnerable to directory traversal. Session keys
|
||||||
# should always be md5s, so they should never contain directory components.
|
# should always be md5s, so they should never contain directory
|
||||||
|
# components.
|
||||||
if os.path.sep in session_key:
|
if os.path.sep in session_key:
|
||||||
raise SuspiciousOperation("Invalid characters (directory components) in session key")
|
raise SuspiciousOperation(
|
||||||
|
"Invalid characters (directory components) in session key")
|
||||||
|
|
||||||
return os.path.join(self.storage_path, self.file_prefix + session_key)
|
return os.path.join(self.storage_path, self.file_prefix + session_key)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ from django.utils.http import cookie_date
|
||||||
TEST_COOKIE_NAME = 'testcookie'
|
TEST_COOKIE_NAME = 'testcookie'
|
||||||
TEST_COOKIE_VALUE = 'worked'
|
TEST_COOKIE_VALUE = 'worked'
|
||||||
|
|
||||||
|
|
||||||
class SessionMiddleware(object):
|
class SessionMiddleware(object):
|
||||||
|
|
||||||
def process_request(self, request):
|
def process_request(self, request):
|
||||||
|
@ -40,5 +41,4 @@ class SessionMiddleware(object):
|
||||||
expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
|
expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
|
||||||
path=settings.SESSION_COOKIE_PATH,
|
path=settings.SESSION_COOKIE_PATH,
|
||||||
secure=settings.SESSION_COOKIE_SECURE or None)
|
secure=settings.SESSION_COOKIE_SECURE or None)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -6,9 +6,12 @@ from django.db import models
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
class SessionManager(models.Manager):
|
class SessionManager(models.Manager):
|
||||||
def encode(self, session_dict):
|
def encode(self, session_dict):
|
||||||
"Returns the given session dictionary pickled and encoded as a string."
|
"""
|
||||||
|
Returns the given session dictionary pickled and encoded as a string.
|
||||||
|
"""
|
||||||
pickled = pickle.dumps(session_dict)
|
pickled = pickle.dumps(session_dict)
|
||||||
pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()
|
pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()
|
||||||
return base64.encodestring(pickled + pickled_md5)
|
return base64.encodestring(pickled + pickled_md5)
|
||||||
|
@ -21,6 +24,7 @@ class SessionManager(models.Manager):
|
||||||
s.delete() # Clear sessions with no data.
|
s.delete() # Clear sessions with no data.
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
class Session(models.Model):
|
class Session(models.Model):
|
||||||
"""
|
"""
|
||||||
Django provides full support for anonymous sessions. The session
|
Django provides full support for anonymous sessions. The session
|
||||||
|
@ -38,7 +42,8 @@ class Session(models.Model):
|
||||||
the sessions documentation that is shipped with Django (also available
|
the sessions documentation that is shipped with Django (also available
|
||||||
on the Django website).
|
on the Django website).
|
||||||
"""
|
"""
|
||||||
session_key = models.CharField(_('session key'), max_length=40, primary_key=True)
|
session_key = models.CharField(_('session key'), max_length=40,
|
||||||
|
primary_key=True)
|
||||||
session_data = models.TextField(_('session data'))
|
session_data = models.TextField(_('session data'))
|
||||||
expire_date = models.DateTimeField(_('expire date'))
|
expire_date = models.DateTimeField(_('expire date'))
|
||||||
objects = SessionManager()
|
objects = SessionManager()
|
||||||
|
|
Loading…
Reference in New Issue