本文整理匯總了Python中django.conf.settings.SESSION_COOKIE_AGE屬性的典型用法代碼示例。如果您正苦於以下問題:Python settings.SESSION_COOKIE_AGE屬性的具體用法?Python settings.SESSION_COOKIE_AGE怎麽用?Python settings.SESSION_COOKIE_AGE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.conf.settings
的用法示例。
在下文中一共展示了settings.SESSION_COOKIE_AGE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_expiry_age
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def get_expiry_age(self, **kwargs):
"""Get the number of seconds until the session expires.
Optionally, this function accepts `modification` and `expiry` keyword
arguments specifying the modification and expiry of the session.
"""
try:
modification = kwargs['modification']
except KeyError:
modification = timezone.now()
# Make the difference between "expiry=None passed in kwargs" and
# "expiry not passed in kwargs", in order to guarantee not to trigger
# self.load() when expiry is provided.
try:
expiry = kwargs['expiry']
except KeyError:
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 - modification
return delta.days * 86400 + delta.seconds
示例2: get_expiry_date
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def get_expiry_date(self, **kwargs):
"""Get session the expiry date (as a datetime object).
Optionally, this function accepts `modification` and `expiry` keyword
arguments specifying the modification and expiry of the session.
"""
try:
modification = kwargs['modification']
except KeyError:
modification = timezone.now()
# Same comment as in get_expiry_age
try:
expiry = kwargs['expiry']
except KeyError:
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 modification + timedelta(seconds=expiry)
示例3: load
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def load(self):
"""
Load the data from the key itself instead of fetching from some
external data store. Opposite of _get_session_key(), raise BadSignature
if signature fails.
"""
try:
return signing.loads(
self.session_key,
serializer=self.serializer,
# This doesn't handle non-default expiry dates, see #19201
max_age=settings.SESSION_COOKIE_AGE,
salt='django.contrib.sessions.backends.signed_cookies',
)
except Exception:
# BadSignature, ValueError, or unpickling exceptions. If any of
# these happen, reset the session.
self.create()
return {}
示例4: process_response
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def process_response(self, request, response):
"""
If the current session is fresh (was just created by the default
session middleware, setting its expiry to `SESSION_COOKIE_AGE`)
or the session is configured to keep alive, processes all rules
to identify what `expiry` should be set.
"""
if not (hasattr(request, 'user') and hasattr(request, 'session')):
return response
key = get_settings_key(request.user)
fresh_session = (
request.session.get_expiry_age() == settings.SESSION_COOKIE_AGE
)
keep_alive = getattr(
settings, 'EXPIRY_{}_KEEP_ALIVE'.format(key), False
)
if fresh_session or keep_alive:
process_rules(request=request, user=request.user)
return response
示例5: get_expiry_date
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def get_expiry_date(self, **kwargs):
"""Get session the expiry date (as a datetime object).
Optionally, this function accepts `modification` and `expiry` keyword
arguments specifying the modification and expiry of the session.
"""
try:
modification = kwargs['modification']
except KeyError:
modification = timezone.now()
# Same comment as in get_expiry_age
try:
expiry = kwargs['expiry']
except KeyError:
expiry = self.get('_session_expiry')
if isinstance(expiry, datetime):
return expiry
expiry = expiry or settings.SESSION_COOKIE_AGE # Checks both None and 0 cases
return modification + timedelta(seconds=expiry)
示例6: load
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def load(self):
"""
We load the data from the key itself instead of fetching from
some external data store. Opposite of _get_session_key(),
raises BadSignature if signature fails.
"""
try:
return signing.loads(self.session_key,
serializer=self.serializer,
# This doesn't handle non-default expiry dates, see #19201
max_age=settings.SESSION_COOKIE_AGE,
salt='django.contrib.sessions.backends.signed_cookies')
except Exception:
# BadSignature, ValueError, or unpickling exceptions. If any of
# these happen, reset the session.
self.create()
return {}
示例7: load
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def load(self):
"""
We load the data from the key itself instead of fetching from
some external data store. Opposite of _get_session_key(),
raises BadSignature if signature fails.
"""
try:
return signing.loads(
self.session_key,
serializer=self.serializer,
# This doesn't handle non-default expiry dates, see #19201
max_age=settings.SESSION_COOKIE_AGE,
salt='django.contrib.sessions.backends.signed_cookies',
)
except Exception:
# BadSignature, ValueError, or unpickling exceptions. If any of
# these happen, reset the session.
self.create()
return {}
示例8: synchronize_all_feeds
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def synchronize_all_feeds():
"""Synchronize feeds every 30 minutes.
To avoid a spike of load, the synchronization is spread over the whole
period.
Feeds that have their sync explicitly disabled or that have no active
subscribers are not synchronized.
"""
current_date = now()
inactive_user_threshold = current_date - (timedelta(seconds=settings.SESSION_COOKIE_AGE) * 2)
feeds_to_sync = models.Feed.objects.filter(
is_sync_enabled=True,
subscribers__user__is_active=True,
subscribers__user__last_login__gte=inactive_user_threshold
)
ats = list()
for i in range(0, 29):
ats.append(current_date + timedelta(minutes=i))
batch = Batch()
for feed_id in feeds_to_sync.values_list('id', flat=True):
batch.schedule_at('synchronize_feed', random.choice(ats), feed_id)
tasks.schedule_batch(batch)
示例9: save
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def save(self):
'''Saves the session data to the datastore.'''
time_till_expire = timedelta(seconds=settings.SESSION_COOKIE_AGE)
expire_date = datetime.now() + time_till_expire
if self._datastore_session:
self._datastore_session.session_data = self.encode(self._session)
else:
self._datastore_session = Session(session_key=self.session_key, session_data=self.encode(self._session), expire_date=expire_date)
self._datastore_session.put()
示例10: load
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def load(self):
"""
We load the data from the key itself instead of fetching from
some external data store. Opposite of _get_session_key(),
raises BadSignature if signature fails.
"""
try:
return signing.loads(self.session_key,
serializer=self.serializer,
# This doesn't handle non-default expiry dates, see #19201
max_age=settings.SESSION_COOKIE_AGE,
salt='django.contrib.sessions.backends.signed_cookies')
except (signing.BadSignature, ValueError):
self.create()
return {}
示例11: _expiry_date
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
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
示例12: test_default_expiry
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def test_default_expiry(self):
# A normal session has a max age equal to settings
self.assertEqual(
self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE
)
# So does a custom session with an idle expiration time of 0 (but it'll
# expire at browser close)
self.session.set_expiry(0)
self.assertEqual(
self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE
)
示例13: test_custom_expiry_reset
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def test_custom_expiry_reset(self):
self.session.set_expiry(None)
self.session.set_expiry(10)
self.session.set_expiry(None)
self.assertEqual(
self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE
)
示例14: _expiry_date
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def _expiry_date(self, session_data):
"""
Return the expiry time of the file storing the session's content.
"""
return session_data.get('_session_expiry') or (
self._last_modification() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)
)
示例15: process_request
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_AGE [as 別名]
def process_request(self, request):
if not hasattr(request, "session") or request.session.is_empty():
return
init_time = request.session.setdefault(SESSION_TIMEOUT_KEY, time.time())
expire_seconds = getattr(
settings, "SESSION_EXPIRE_SECONDS", settings.SESSION_COOKIE_AGE
)
session_is_expired = time.time() - init_time > expire_seconds
if session_is_expired:
request.session.flush()
redirect_url = getattr(settings, "SESSION_TIMEOUT_REDIRECT", None)
if redirect_url:
return redirect(redirect_url)
else:
return redirect_to_login(next=request.path)
expire_since_last_activity = getattr(
settings, "SESSION_EXPIRE_AFTER_LAST_ACTIVITY", False
)
grace_period = getattr(
settings, "SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD", 1
)
if expire_since_last_activity and time.time() - init_time > grace_period:
request.session[SESSION_TIMEOUT_KEY] = time.time()