Ticket #15500: add_support_for_cookie_expiry_setting.patch

File add_support_for_cookie_expiry_setting.patch, 2.4 KB (added by tdignan, 13 years ago)

Add support for setting cookie expiry with a UTC date (patch)

  • django/contrib/sessions/backends/base.py

     
    33import random
    44import sys
    55import time
     6from calendar import timegm
    67from datetime import datetime, timedelta
    78try:
    89    import cPickle as pickle
     
    197198
    198199    _session = property(_get_session)
    199200
     201    def _get_expiry_setting(self):
     202        """
     203        Returns seconds until the cookie expires. Gets this value from either
     204        SESSION_COOKIE_AGE or SESSION_COOKIE_EXPIRY_UTC.
     205        """
     206        # If SESSION_COOKIE_EXPIRY_UTC is set by the user, use it.
     207        # otherwise return the default: SESSION_COOKIE_AGE.
     208        if not hasattr(settings, 'SESSION_COOKIE_EXPIRY_UTC'):
     209            return settings.SESSION_COOKIE_AGE
     210        else:
     211            # Return the difference between the expiration date in UTC and
     212            # the current unix time rounded down to seconds
     213            return timegm(time.strptime(settings.SESSION_COOKIE_EXPIRY_UTC,
     214                "%Y-%m-%d %H:%M:%S")) - int(time.time())
     215   
    200216    def get_expiry_age(self):
    201217        """Get the number of seconds until the session expires."""
    202218        expiry = self.get('_session_expiry')
    203219        if not expiry:   # Checks both None and 0 cases
    204             return settings.SESSION_COOKIE_AGE
     220            return self._get_expiry_setting()
    205221        if not isinstance(expiry, datetime):
    206222            return expiry
    207223        delta = expiry - datetime.now()
     
    213229        if isinstance(expiry, datetime):
    214230            return expiry
    215231        if not expiry:   # Checks both None and 0 cases
    216             expiry = settings.SESSION_COOKIE_AGE
     232            expiry = self._get_expiry_setting()
    217233        return datetime.now() + timedelta(seconds=expiry)
    218234
     235   
    219236    def set_expiry(self, value):
    220237        """
    221238        Sets a custom expiration for the session. ``value`` can be an integer,
  • django/contrib/sessions/backends/cached_db.py

     
    3939        """
    4040        self.clear()
    4141        self.delete(self.session_key)
    42         self.create()
    43  No newline at end of file
     42        self.create()
Back to Top