From 5db4d602152c14bb4b25011d9a881f7fa1639152 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Mon, 23 Jun 2008 05:08:07 +0000 Subject: [PATCH] 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 --- django/contrib/sessions/backends/base.py | 11 ++++++----- django/contrib/sessions/backends/cache.py | 3 ++- django/contrib/sessions/backends/db.py | 6 ++++-- django/contrib/sessions/backends/file.py | 16 ++++++++++------ django/contrib/sessions/middleware.py | 2 +- django/contrib/sessions/models.py | 9 +++++++-- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index 1f9c20871b..b40fba6e6e 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -5,14 +5,15 @@ import random import sys import time from datetime import datetime, timedelta -from django.conf import settings -from django.core.exceptions import SuspiciousOperation - try: import cPickle as pickle except ImportError: import pickle +from django.conf import settings +from django.core.exceptions import SuspiciousOperation + + class SessionBase(object): """ Base class for all Session classes. @@ -169,8 +170,8 @@ class SessionBase(object): def set_expiry(self, value): """ - Sets a custom expiration for the session. ``value`` can be an integer, a - Python ``datetime`` or ``timedelta`` object or ``None``. + Sets a custom expiration for the session. ``value`` can be an integer, + a Python ``datetime`` or ``timedelta`` object or ``None``. 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 diff --git a/django/contrib/sessions/backends/cache.py b/django/contrib/sessions/backends/cache.py index 7626163a13..7e171e8309 100644 --- a/django/contrib/sessions/backends/cache.py +++ b/django/contrib/sessions/backends/cache.py @@ -2,6 +2,7 @@ from django.conf import settings from django.contrib.sessions.backends.base import SessionBase from django.core.cache import cache + class SessionStore(SessionBase): """ A cache-based session store. @@ -23,4 +24,4 @@ class SessionStore(SessionBase): return False def delete(self, session_key): - self._cache.delete(session_key) \ No newline at end of file + self._cache.delete(session_key) diff --git a/django/contrib/sessions/backends/db.py b/django/contrib/sessions/backends/db.py index b1c1097865..67132c7560 100644 --- a/django/contrib/sessions/backends/db.py +++ b/django/contrib/sessions/backends/db.py @@ -1,12 +1,14 @@ +import datetime + from django.conf import settings from django.contrib.sessions.models import Session from django.contrib.sessions.backends.base import SessionBase from django.core.exceptions import SuspiciousOperation -import datetime + class SessionStore(SessionBase): """ - Implements database session store + Implements database session store. """ def __init__(self, session_key=None): super(SessionStore, self).__init__(session_key) diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py index d65c81c101..49a7045244 100644 --- a/django/contrib/sessions/backends/file.py +++ b/django/contrib/sessions/backends/file.py @@ -1,9 +1,11 @@ import os import tempfile + from django.conf import settings from django.contrib.sessions.backends.base import SessionBase from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured + class SessionStore(SessionBase): """ Implements a file based session store. @@ -15,10 +17,10 @@ class SessionStore(SessionBase): # Make sure the storage path is valid. if not os.path.isdir(self.storage_path): - raise ImproperlyConfigured("The session storage path %r doesn't exist. "\ - "Please set your SESSION_FILE_PATH setting "\ - "to an existing directory in which Django "\ - "can store session data." % self.storage_path) + raise ImproperlyConfigured( + "The session storage path %r doesn't exist. Please set your" + " SESSION_FILE_PATH setting to an existing directory in which" + " Django can store session data." % self.storage_path) self.file_prefix = settings.SESSION_COOKIE_NAME super(SessionStore, self).__init__(session_key) @@ -31,9 +33,11 @@ class SessionStore(SessionBase): session_key = self.session_key # 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: - 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) diff --git a/django/contrib/sessions/middleware.py b/django/contrib/sessions/middleware.py index a7b376dde0..238d6095e5 100644 --- a/django/contrib/sessions/middleware.py +++ b/django/contrib/sessions/middleware.py @@ -7,6 +7,7 @@ from django.utils.http import cookie_date TEST_COOKIE_NAME = 'testcookie' TEST_COOKIE_VALUE = 'worked' + class SessionMiddleware(object): def process_request(self, request): @@ -40,5 +41,4 @@ class SessionMiddleware(object): expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None) - return response diff --git a/django/contrib/sessions/models.py b/django/contrib/sessions/models.py index 4769e63b1b..70fce3b226 100644 --- a/django/contrib/sessions/models.py +++ b/django/contrib/sessions/models.py @@ -6,9 +6,12 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ from django.conf import settings + class SessionManager(models.Manager): 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_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest() return base64.encodestring(pickled + pickled_md5) @@ -21,6 +24,7 @@ class SessionManager(models.Manager): s.delete() # Clear sessions with no data. return s + class Session(models.Model): """ 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 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')) expire_date = models.DateTimeField(_('expire date')) objects = SessionManager()