Commit 8d4f79a7 authored by Jacob Kaplan-Moss's avatar Jacob Kaplan-Moss
Browse files

Fixed #2548: added get/set_expiry methods to session objects. Thanks, Amit...

Fixed #2548: added get/set_expiry methods to session objects. Thanks, Amit Upadhyay and SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7586 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 50de1334
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ answer newbie questions, and generally made Django that much better:
    Makoto Tsuyuki <mtsuyuki@gmail.com>
    tt@gurgle.no
    David Tulig <david.tulig@gmail.com>
    Amit Upadhyay
    Amit Upadhyay <http://www.amitu.com/blog/>
    Geert Vanderkelen
    I.S. van Oostveen <v.oostveen@idca.nl>
    viestards.lists@gmail.com
+1 −1
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ SESSION_COOKIE_DOMAIN = None # A string like ".lawren
SESSION_COOKIE_SECURE = False                           # Whether the session cookie should be secure (https:// only).
SESSION_COOKIE_PATH = '/'                               # The path of the session cookie.
SESSION_SAVE_EVERY_REQUEST = False                      # Whether to save the session data on every request.
SESSION_EXPIRE_AT_BROWSER_CLOSE = False                 # Whether sessions expire when a user closes his browser.
SESSION_EXPIRE_AT_BROWSER_CLOSE = False                 # Whether a user's session cookie expires when they close their browser.
SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # The module to store session data
SESSION_FILE_PATH = None                                # Directory to store session files if using the file session module. If None, the backend will use a sensible default.

+57 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import os
import random
import sys
import time
from datetime import datetime, timedelta
from django.conf import settings
from django.core.exceptions import SuspiciousOperation

@@ -128,6 +129,62 @@ class SessionBase(object):

    _session = property(_get_session)

    def get_expiry_age(self):
        """Get the number of seconds until the session expires."""
        expiry = self.get('_session_expiry')
        if not expiry:   # Checks both None and 0 cases
            return settings.SESSION_COOKIE_AGE
        if not isinstance(expiry, datetime):
            return expiry
        delta = expiry - datetime.now()
        return delta.days * 86400 + delta.seconds

    def get_expiry_date(self):
        """Get session the expiry date (as a datetime object)."""
        expiry = self.get('_session_expiry')
        if isinstance(expiry, datetime):
            return expiry
        if not expiry:   # Checks both None and 0 cases
            expiry = settings.SESSION_COOKIE_AGE
        return datetime.now() + timedelta(seconds=expiry)

    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``.

        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
        browser close.

        If ``value`` is a ``datetime`` or ``timedelta`` object, the session
        will expire at that specific future time.

        If ``value`` is ``None``, the session uses the global session expiry
        policy.
        """
        if value is None:
            # Remove any custom expiration for this session.
            try:
                del self['_session_expiry']
            except KeyError:
                pass
            return
        if isinstance(value, timedelta):
            value = datetime.now() + value
        self['_session_expiry'] = value

    def get_expire_at_browser_close(self):
        """
        Returns ``True`` if the session is set to expire when the browser
        closes, and ``False`` if there's an expiry date. Use
        ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
        date/age, if there is one.
        """
        if self.get('_session_expiry') is None:
            return settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
        return self.get('_session_expiry') == 0

    # Methods that child classes must implement.

    def exists(self, session_key):
+4 −4
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ class SessionStore(SessionBase):
        return session_data or {}

    def save(self):
        self._cache.set(self.session_key, self._session, settings.SESSION_COOKIE_AGE)
        self._cache.set(self.session_key, self._session, self.get_expiry_age())

    def exists(self, session_key):
        if self._cache.get(session_key):
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class SessionStore(SessionBase):
        Session.objects.create(
            session_key = self.session_key,
            session_data = self.encode(self._session),
            expire_date = datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)
            expire_date = self.get_expiry_date()
        )

    def delete(self, session_key):
Loading