本文整理匯總了Python中django.contrib.auth.SESSION_KEY屬性的典型用法代碼示例。如果您正苦於以下問題:Python auth.SESSION_KEY屬性的具體用法?Python auth.SESSION_KEY怎麽用?Python auth.SESSION_KEY使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.contrib.auth
的用法示例。
在下文中一共展示了auth.SESSION_KEY屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_load_modified
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_load_modified(store, django_user_model):
django_user_model.objects.create_user(username="test_user")
store[auth.SESSION_KEY] = 1
store.save()
store2 = SessionStore(session_key=store.session_key, user_agent="TestUA/1.1-changed", ip="8.8.8.8")
store2.load()
assert store2.get(USER_AGENT_SESSION_KEY) == "TestUA/1.1"
assert store2.get(IP_SESSION_KEY) == "127.0.0.1"
assert store2.get(auth.SESSION_KEY) == 1
assert store2.modified is True
store2.save()
assert store2.get(USER_AGENT_SESSION_KEY) == "TestUA/1.1-changed"
assert store2.get(IP_SESSION_KEY) == "8.8.8.8"
示例2: get_cached_user
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def get_cached_user(request):
if not hasattr(request, '_cached_user'):
try:
key = CACHE_KEY.format(request.session[SESSION_KEY])
cache = caches['default']
user = cache.get(key)
except KeyError:
user = AnonymousUser()
if user is None:
user = get_user(request)
cache = caches['default']
# 8 hours
cache.set(key, user, 28800)
logger.debug('No User Cache. Setting now: {0}, {1}'.format(key, user.username))
request._cached_user = user
return request._cached_user
示例3: create_users
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def create_users(self):
for username in self.get_my_usernames():
user = User.objects.create(username=username)
user.set_password('p4ssw0rd')
user.save()
profile = user.get_profile()
profile.has_seen_sheet_page = True
profile.save()
# create sessions we can use for login too
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
self.session_keys[username] = session.session_key
示例4: test_login_with_u2f
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_login_with_u2f(self):
code = self.enable_backupcode()
r = self.client.post(self.login_url, {
'username': 'test',
'password': 'asdfasdf',
})
self.assertNotIn(SESSION_KEY, self.client.session)
self.assertIn(reverse('u2f:verify-second-factor'), r['location'])
verify_key_response = self.client.get(r['location'])
self.assertContains(verify_key_response, 'Django administration')
r = self.client.post(r['location'], {
'code': code,
'type': 'backup',
})
self.assertEqual(str(self.client.session[SESSION_KEY]), str(self.user.id))
self.assertTrue(r['location'].endswith(self.admin_url))
示例5: test_login_while_already_logged_in
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_login_while_already_logged_in(self):
User.objects.create_superuser(
username='test2',
email='test2@example.com',
password='asdfasdf',
)
r = self.client.post(self.login_url, {
'username': 'test2',
'password': 'asdfasdf',
'next': '/next/'
})
assert r.status_code == 302
code = self.enable_backupcode()
r = self.login()
self.assertIn(reverse('u2f:verify-second-factor'), r['location'])
r = self.client.post(r['location'], {
'code': code,
'type': 'backup',
})
self.assertEqual(str(self.client.session[SESSION_KEY]), str(self.user.id))
self.assertTrue(r['location'].endswith('/next/'))
self.assertFalse(self.user.backup_codes.filter(code=code).exists())
示例6: test_token_cant_be_used_twice
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_token_cant_be_used_twice(self):
key = self.enable_totp()
r = self.login()
token = oath.totp(key, timezone.now()),
r = self.client.post(r['location'], {
'token': token,
'type': 'totp',
})
self.assertEqual(str(self.client.session[SESSION_KEY]), str(self.user.id))
self.client.logout()
r = self.login()
r = self.client.post(r['location'], {
'token': token,
'type': 'totp',
})
self.assertContains(r, TOTPForm.INVALID_ERROR_MESSAGE)
示例7: getUserFromSessionId
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def getUserFromSessionId(self, session_id):
"""Return the user from `session_id`."""
session_engine = self.factory.getSessionEngine()
session_wrapper = session_engine.SessionStore(session_id)
user_id = session_wrapper.get(SESSION_KEY)
backend = session_wrapper.get(BACKEND_SESSION_KEY)
if backend is None:
return None
auth_backend = load_backend(backend)
if user_id is not None and auth_backend is not None:
user = auth_backend.get_user(user_id)
# Get the user again prefetching the SSHKey for the user. This is
# done so a query is not made for each action that is possible on
# a node in the node listing.
return (
User.objects.filter(id=user.id)
.prefetch_related("sshkey_set")
.first()
)
else:
return None
示例8: test_auth_session_key
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_auth_session_key(store):
assert auth.SESSION_KEY not in store
assert store.modified is False
assert store.accessed is True
store.get(auth.SESSION_KEY)
assert store.modified is False
store[auth.SESSION_KEY] = 1
assert store.modified is True
示例9: test_save
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_save(store, django_user_model):
django_user_model.objects.create_user(username="test_user")
store[auth.SESSION_KEY] = 1
store.save()
session = Session.objects.get(pk=store.session_key)
assert session.user_agent == "TestUA/1.1"
assert session.ip == "127.0.0.1"
assert session.user_id == 1
assert now() - timedelta(seconds=5) <= session.updated_at <= now()
示例10: test_load_unmodified
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_load_unmodified(store, django_user_model):
django_user_model.objects.create_user(username="test_user")
store[auth.SESSION_KEY] = 1
store.save()
store2 = SessionStore(session_key=store.session_key, user_agent="TestUA/1.1", ip="127.0.0.1")
store2.load()
assert store2.get(USER_AGENT_SESSION_KEY) == "TestUA/1.1"
assert store2.get(IP_SESSION_KEY) == "127.0.0.1"
assert store2.get(auth.SESSION_KEY) == 1
assert store2.modified is False
示例11: test_clear
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_clear(store):
"""
Clearing the session should clear all non-browser information
"""
store[auth.SESSION_KEY] = 1
store.clear()
store.save()
session = Session.objects.get(pk=store.session_key)
assert session.user_id is None
示例12: test_get_decoded
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def test_get_decoded(django_user_model):
django_user_model.objects.create_user(username="test_user")
store = SessionStore(user_agent="TestUA/1.1", ip="127.0.0.1")
store[auth.SESSION_KEY] = 1
store["foo"] = "bar"
store.save()
session = Session.objects.get(pk=store.session_key)
assert session.get_decoded() == {
"foo": "bar",
auth.SESSION_KEY: 1,
IP_SESSION_KEY: "127.0.0.1",
USER_AGENT_SESSION_KEY: "TestUA/1.1",
}
示例13: create_model_instance
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def create_model_instance(self, data):
# Store User, User Agent, and IP in Session (in DB).
return self.model(
session_key=self._get_or_create_session_key(),
session_data=self.encode(data),
expire_date=self.get_expiry_date(),
user_id=self.get(auth.SESSION_KEY),
user_agent=self.user_agent,
ip=self.ip,
)
示例14: get_session_dict_user
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def get_session_dict_user(session_dict: Mapping[str, int]) -> Optional[int]:
# Compare django.contrib.auth._get_user_session_key
try:
return get_user_model()._meta.pk.to_python(session_dict[SESSION_KEY])
except KeyError:
return None
示例15: __setitem__
# 需要導入模塊: from django.contrib import auth [as 別名]
# 或者: from django.contrib.auth import SESSION_KEY [as 別名]
def __setitem__(self, key, value):
if key == auth.SESSION_KEY:
self.user_id = value
super(SessionStore, self).__setitem__(key, value)