--- django/contrib/sessions/backends/file.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) --- a/django/contrib/sessions/backends/file.py +++ b/django/contrib/sessions/backends/file.py @@ -6,13 +6,14 @@ import shutil import tempfile from django.conf import settings -from django.contrib.sessions.backends.base import SessionBase, CreateError, VALID_KEY_CHARS -from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured +from django.contrib.sessions.backends.base import ( + VALID_KEY_CHARS, CreateError, SessionBase, +) +from django.contrib.sessions.exceptions import InvalidSessionKey +from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation from django.utils import timezone from django.utils.encoding import force_text -from django.contrib.sessions.exceptions import InvalidSessionKey - class SessionStore(SessionBase): """ @@ -70,6 +71,15 @@ class SessionStore(SessionBase): modification = datetime.datetime.fromtimestamp(modification) return modification + def _expiry_date(self, session_data): + """ + Return the expiry time of the file storing the session's content. + """ + expiry = session_data.get('_session_expiry') + if not expiry: + expiry = self._last_modification() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE) + return expiry + def load(self): session_data = {} try: @@ -88,15 +98,13 @@ class SessionStore(SessionBase): self.create() # Remove expired sessions. - expiry_age = self.get_expiry_age( - modification=self._last_modification(), - expiry=session_data.get('_session_expiry')) - if expiry_age < 0: + expiry_age = self.get_expiry_age(expiry=self._expiry_date(session_data)) + if expiry_age <= 0: session_data = {} self.delete() self.create() except (IOError, SuspiciousOperation): - self.create() + self._session_key = None return session_data def create(self): @@ -107,10 +115,11 @@ class SessionStore(SessionBase): except CreateError: continue self.modified = True - self._session_cache = {} return def save(self, must_create=False): + if self.session_key is None: + return self.create() # Get the session data now, before we start messing # with the file it is stored within. session_data = self._get_session(no_load=must_create)