本文整理匯總了Python中django.conf.settings.SESSION_COOKIE_NAME屬性的典型用法代碼示例。如果您正苦於以下問題:Python settings.SESSION_COOKIE_NAME屬性的具體用法?Python settings.SESSION_COOKIE_NAME怎麽用?Python settings.SESSION_COOKIE_NAME使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.conf.settings
的用法示例。
在下文中一共展示了settings.SESSION_COOKIE_NAME屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_modify_session
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def test_modify_session(client, logged_in):
if logged_in:
user = User.objects.create_superuser("user", "", "secret")
client.force_login(user)
else:
user = None
client.get("/read_session/", HTTP_USER_AGENT="TestUA/1.1")
client.get("/modify_session/", HTTP_USER_AGENT="TestUA/1.1")
data = json.loads(client.get("/read_session/", HTTP_USER_AGENT="TestUA/1.1").content.decode("UTF-8"))
assert data["FOO"] == "BAR"
assert data[USER_AGENT_SESSION_KEY] == "TestUA/1.1"
if user:
assert str(data[SESSION_KEY]) == str(user.id)
assert settings.SESSION_COOKIE_NAME in client.cookies
session = Session.objects.get(pk=client.cookies[settings.SESSION_COOKIE_NAME].value)
assert session.user_agent == "TestUA/1.1"
assert session.ip == "127.0.0.1"
assert session.user == user
示例2: __init__
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def __init__(self, *args, **kwargs):
self.protected_cookies = get_config_setting(
"SESSION_COOKIE_SAMESITE_KEYS", set()
)
if not isinstance(self.protected_cookies, (list, set, tuple)):
raise ValueError(
"SESSION_COOKIE_SAMESITE_KEYS should be a list, set or tuple."
)
self.protected_cookies = set(self.protected_cookies)
self.protected_cookies |= {
settings.SESSION_COOKIE_NAME,
settings.CSRF_COOKIE_NAME,
}
samesite_flag = get_config_setting("SESSION_COOKIE_SAMESITE", "")
self.samesite_flag = (
str(samesite_flag).capitalize() if samesite_flag is not None else ""
)
self.samesite_force_all = get_config_setting(
"SESSION_COOKIE_SAMESITE_FORCE_ALL"
)
return super(CookiesSameSite, self).__init__(*args, **kwargs)
示例3: __call__
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
from django.conf import settings
from polaris import settings as polaris_settings
if (
settings.SESSION_COOKIE_NAME in response.cookies
and not polaris_settings.LOCAL_MODE
):
response.cookies[settings.SESSION_COOKIE_NAME]["samesite"] = "None"
response.cookies[settings.SESSION_COOKIE_NAME]["secure"] = True
return response
示例4: login
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def login(
self, username=None, password=USER_PASSWORD, manually=False
):
if username is None:
username = self.get_my_username()
if manually:
self.get_element('id=id_username').clear()
self.get_element('id=id_password').clear()
self.get_element('id=id_username').send_keys(username)
self.get_element('id=id_password').send_keys(password)
self.click_link('id_login')
return
session_key = self.session_keys[username]
## to set a cookie we need to first visit the domain.
## 404 pages load the quickest!
self.browser.get(urljoin(Url.ROOT, "/404_no_such_url/"))
self.browser.add_cookie(dict(
name=settings.SESSION_COOKIE_NAME,
value=session_key,
path='/',
))
self.go_to_url(Url.ROOT)
示例5: establish_session
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def establish_session(original_function):
'''
Simulate establishing a session.
Adapted from https://code.djangoproject.com/ticket/10899 and
http://blog.joshcrompton.com/2012/09/how-to-use-sessions-in-django-unit-tests.html
FIXME this needs its own tests
'''
def new_function(self, *args, **kwargs):
from importlib import import_module
from django.conf import settings
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save() # we need to make load() work, or the cookie is worthless
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
self.session_key = store.session_key
original_function(self, *args, **kwargs)
return new_function
示例6: dispatch
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
messages.success(self.request, _('You have been successfully logged out.'))
# By default, logging out will generate a fresh sessionid cookie. We want to use the
# absence of sessionid as an indication that front-end pages are being viewed by a
# non-logged-in user and are therefore cacheable, so we forcibly delete the cookie here.
response.delete_cookie(
settings.SESSION_COOKIE_NAME,
domain=settings.SESSION_COOKIE_DOMAIN,
path=settings.SESSION_COOKIE_PATH
)
# HACK: pretend that the session hasn't been modified, so that SessionMiddleware
# won't override the above and write a new cookie.
self.request.session.modified = False
return response
示例7: login
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def login(self, superuser=False):
""" Craft cookie to fake a login. """
assert auth, "auth.py could not be imported from acceptance_tests"
# First visit a guaranteed 404, just to get selenium in the right domain (it doesn't like us setting cookies
# for places we aren't in, even if we specify a domain).
self.driver.get('http://localhost:19150/not-a-place')
self.driver.add_cookie({
'name': settings.SESSION_COOKIE_NAME,
'value': auth.SESSION_SUPER_KEY if superuser else auth.SESSION_KEY,
'secure': False,
'path': '/',
})
self.driver.refresh()
示例8: _get_session
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def _get_session(self):
session_cookie = self._driver.get_cookie(settings.SESSION_COOKIE_NAME)
if session_cookie is None:
# Create new
session = get_session_store()
cookie_data = {'name': settings.SESSION_COOKIE_NAME,
'value': session.session_key,
'path': '/',
'secure': False,
}
if self._driver.name == 'phantomjs':
# Not sure why this is needed, but it seems to do the trick
cookie_data['domain'] = '.localhost'
self._add_cookie(cookie_data)
else:
session = get_session_store(session_key=session_cookie['value'])
return session
示例9: test_httponly_session_cookie
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def test_httponly_session_cookie(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Simulate a request the modifies the session
middleware.process_request(request)
request.session['hello'] = 'world'
# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertIs(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], True)
self.assertIn(
cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例10: test_no_httponly_session_cookie
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def test_no_httponly_session_cookie(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Simulate a request the modifies the session
middleware.process_request(request)
request.session['hello'] = 'world'
# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertEqual(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], '')
self.assertNotIn(
cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例11: test_session_delete_on_end
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def test_session_delete_on_end(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Before deleting, there has to be an existing cookie
request.COOKIES[settings.SESSION_COOKIE_NAME] = 'abc'
# Simulate a request that ends the session
middleware.process_request(request)
request.session.flush()
# Handle the response through the middleware
response = middleware.process_response(request, response)
# The cookie was deleted, not recreated.
# A deleted cookie header looks like:
# Set-Cookie: sessionid=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/
self.assertEqual(
'Set-Cookie: {}=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; '
'Max-Age=0; Path=/'.format(
settings.SESSION_COOKIE_NAME,
),
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例12: login_user
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def login_user(self, userid):
"""
Login as specified user, does not depend on auth backend (hopefully)
This is based on Client.login() with a small hack that does not
require the call to authenticate()
"""
if not 'django.contrib.sessions' in settings.INSTALLED_APPS:
raise AssertionError("Unable to login without django.contrib.sessions in INSTALLED_APPS")
try:
user = User.objects.get(username=userid)
except User.DoesNotExist:
user = User(username=userid, password='')
user.save()
user.backend = "%s.%s" % ("django.contrib.auth.backends",
"ModelBackend")
engine = import_module(settings.SESSION_ENGINE)
# Create a fake request to store login details.
request = HttpRequest()
#if self.session:
# request.session = self.session
#else:
request.session = engine.SessionStore()
login(request, user)
# Set the cookie to represent the session.
session_cookie = settings.SESSION_COOKIE_NAME
self.cookies[session_cookie] = request.session.session_key
cookie_data = {
'max-age': None,
'path': '/',
'domain': settings.SESSION_COOKIE_DOMAIN,
'secure': settings.SESSION_COOKIE_SECURE or None,
'expires': None,
}
self.cookies[session_cookie].update(cookie_data)
# Save the session values.
request.session.save()
示例13: setUp
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def setUp(self):
classify = Classification.objects.create(c_name='test')
self.art = Article.objects.create(caption='article',
sub_caption='sub_article',
classification=classify,
content='article test',
publish=True)
settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save()
self.session = store
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
示例14: _session
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def _session(self):
"""
Obtains the current session variables.
"""
if apps.is_installed('django.contrib.sessions'):
engine = import_module(settings.SESSION_ENGINE)
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
if cookie:
return engine.SessionStore(cookie.value)
else:
s = engine.SessionStore()
s.save()
self.cookies[settings.SESSION_COOKIE_NAME] = s.session_key
return s
return {}
示例15: login
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SESSION_COOKIE_NAME [as 別名]
def login(self, **credentials):
"""
Sets the Factory to appear as if it has successfully logged into a site.
Returns True if login is possible; False if the provided credentials
are incorrect, or the user is inactive, or if the sessions framework is
not available.
"""
from django.contrib.auth import authenticate, login
user = authenticate(**credentials)
if (user and user.is_active and
apps.is_installed('django.contrib.sessions')):
engine = import_module(settings.SESSION_ENGINE)
# Create a fake request to store login details.
request = HttpRequest()
if self.session:
request.session = self.session
else:
request.session = engine.SessionStore()
login(request, user)
# Save the session values.
request.session.save()
# Set the cookie to represent the session.
session_cookie = settings.SESSION_COOKIE_NAME
self.cookies[session_cookie] = request.session.session_key
cookie_data = {
'max-age': None,
'path': '/',
'domain': settings.SESSION_COOKIE_DOMAIN,
'secure': settings.SESSION_COOKIE_SECURE or None,
'expires': None,
}
self.cookies[session_cookie].update(cookie_data)
return True
else:
return False