Beispiel #1
0
    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 = get_utc_now_with_timezone() + value
        self["_session_expiry"] = value
Beispiel #2
0
 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 = self.settings.SESSION_COOKIE_AGE
     return get_utc_now_with_timezone() + timedelta(seconds=expiry)
Beispiel #3
0
 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 self.settings.SESSION_COOKIE_AGE
     if not isinstance(expiry, datetime):
         return expiry
     delta = expiry - get_utc_now_with_timezone()
     return delta.days * 86400 + delta.seconds
Beispiel #4
0
 def create(self, session):
     """ Populates and persists a new empty session """
     session._session_key = session._get_new_session_key()
     session.modified = True
     session._session_cache = {}
     expiry = self.settings.SESSION_COOKIE_AGE
     expiry_date = get_utc_now_with_timezone() + \
             datetime.timedelta(seconds=expiry)
     session.save(expiry_date=expiry_date)
     return
Beispiel #5
0
 def load(self, session):
     """ Loads the data for the provided session
     If the session has no data, is expired, or
     the decode doesn't match hashes, then the
     session is re-created
     """
     r = RedisStore(settings=self.settings).get_redis_session_connection()
     raw = r.get(session.session_key)
     if raw is None:
         r.delete(session.session_key)
         session.create()
         return {}
     d = pickle.loads(raw)
     expire_date = d.get('expire_date', datetime.datetime(1980, 1, 1))
     if expire_date < get_utc_now_with_timezone():
         session.create()
         return {}
     try:
         decoded = session.decode(d.get('session_data'))
         return decoded
     except SuspiciousOperation:
         session.create()
         return {}