本文整理汇总了Python中django.contrib.sessions.backends.db.SessionStore方法的典型用法代码示例。如果您正苦于以下问题:Python db.SessionStore方法的具体用法?Python db.SessionStore怎么用?Python db.SessionStore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.sessions.backends.db
的用法示例。
在下文中一共展示了db.SessionStore方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def check(cls, **kwargs):
"""
We use .session_key and CSRF_COOKIE above: check that they will be there, and fail fast if not.
"""
errors = super().check(**kwargs)
# Ensure we are using the database session store. (Other SessionStores may also have .session_key?)
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore
if not issubclass(store, DatabaseSessionStore):
errors.append(Error(
"Quiz logging uses request.session.session_key, which likely implies "
"SESSION_ENGINE = 'django.contrib.sessions.backends.db' in settings."
))
if 'django.middleware.csrf.CsrfViewMiddleware' not in settings.MIDDLEWARE:
errors.append(Error(
"CsrfViewMiddleware is not enabled in settings: quiz logging uses CSRF_COOKIE and will fail without "
"CSRF checking enabled. Also it should be enabled in general."
))
return errors
示例2: test_get_ip_reverse_proxy
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_get_ip_reverse_proxy(self):
""" Tests if can handle a long user agent
"""
request_factory = RequestFactory()
request = request_factory.get(ADMIN_LOGIN_URL)
request.user = AnonymousUser()
request.session = SessionStore()
request.META["HTTP_X_FORWARDED_FOR"] = "192.168.24.24"
self.assertEqual(utils.get_ip(request), "192.168.24.24")
request_factory = RequestFactory()
request = request_factory.get(ADMIN_LOGIN_URL)
request.user = AnonymousUser()
request.session = SessionStore()
request.META["REMOTE_ADDR"] = "24.24.24.24"
self.assertEqual(utils.get_ip(request), "24.24.24.24")
示例3: test_login_non_blocked_for_non_standard_login_views_different_msg
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_login_non_blocked_for_non_standard_login_views_different_msg(self):
"""
Check that a view wich returns the expected status code but not the
expected message is not causing the IP to be locked out.
"""
@watch_login(status_code=401, msg="Invalid credentials")
def fake_api_401_login_view_without_msg(request):
""" Fake the api login with 401 """
return HttpResponse("Ups, wrong credentials", status=401)
request_factory = RequestFactory()
request = request_factory.post("api/login")
request.user = AnonymousUser()
request.session = SessionStore()
request.META["HTTP_X_FORWARDED_FOR"] = "192.168.24.24"
for _ in range(4):
fake_api_401_login_view_without_msg(request)
data_out = utils.get_blocked_ips()
self.assertEqual(data_out, [])
示例4: create_users
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [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
示例5: test_ticket_12163
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_ticket_12163(self):
# Test for #12163 - Pickling error saving session with unsaved model
# instances.
SESSION_KEY = '2b1189a188b44ad18c35e1baac6ceead'
item = Item()
item._deferred = False
s = SessionStore(SESSION_KEY)
s.clear()
s["item"] = item
s.save(must_create=True)
s = SessionStore(SESSION_KEY)
s.modified = True
s.save()
i2 = s["item"]
self.assertFalse(i2._deferred)
示例6: rf
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def rf():
"""A RequestFactory instance."""
from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.storage.fallback import FallbackStorage
from django.contrib.sessions.backends.db import SessionStore
from django.test.client import RequestFactory as BaseRequestFactory
class RequestFactory(BaseRequestFactory):
def request(self, user=None, **request):
request = super().request(**request)
request.user = AnonymousUser()
request.session = SessionStore()
request._messages = FallbackStorage(request)
return request
return RequestFactory()
示例7: test_session_update_error_redirect
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_session_update_error_redirect(self):
path = '/foo/'
request = RequestFactory().get(path)
response = HttpResponse()
middleware = SessionMiddleware()
request.session = DatabaseSession()
request.session.save(must_create=True)
request.session.delete()
msg = (
"The request's session was deleted before the request completed. "
"The user may have logged out in a concurrent request, for example."
)
with self.assertRaisesMessage(SuspiciousOperation, msg):
# Handle the response through the middleware. It will try to save
# the deleted session which will cause an UpdateError that's caught
# and raised as a SuspiciousOperation.
middleware.process_response(request, response)
示例8: for_sessionstore
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def for_sessionstore(cls, sessionstore, save_new=True):
'Retrieve or create a SessionInfo for this SessionStore.'
assert isinstance(sessionstore, SessionStore)
try:
si = cls.objects.get(session__session_key=sessionstore.session_key)
except (SessionInfo.DoesNotExist):
si = SessionInfo(session=Session.objects.get(session_key=sessionstore.session_key))
if save_new:
si.save()
return si
示例9: test_get_ip
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_get_ip(self):
""" Tests if can handle a long user agent
"""
request_factory = RequestFactory()
request = request_factory.get(ADMIN_LOGIN_URL)
request.user = AnonymousUser()
request.session = SessionStore()
self.assertEqual(utils.get_ip(request), "127.0.0.1")
示例10: test_login_blocked_for_non_standard_login_views_without_msg
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_login_blocked_for_non_standard_login_views_without_msg(self):
"""
Check that a view wich returns the expected status code is causing
the user to be locked out when we do not expect a specific message
to be returned.
"""
@watch_login(status_code=401)
def fake_api_401_login_view_without_msg(request):
""" Fake the api login with 401 """
return HttpResponse(status=401)
request_factory = RequestFactory()
request = request_factory.post("api/login")
request.user = AnonymousUser()
request.session = SessionStore()
request.META["HTTP_X_FORWARDED_FOR"] = "192.168.24.24"
for _ in range(3):
fake_api_401_login_view_without_msg(request)
data_out = utils.get_blocked_ips()
self.assertEqual(data_out, [])
fake_api_401_login_view_without_msg(request)
data_out = utils.get_blocked_ips()
self.assertEqual(data_out, ["192.168.24.24"])
示例11: test_login_blocked_for_non_standard_login_views_with_msg
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_login_blocked_for_non_standard_login_views_with_msg(self):
"""
Check that a view wich returns the expected status code and the
expected message is causing the IP to be locked out.
"""
@watch_login(status_code=401, msg="Invalid credentials")
def fake_api_401_login_view_without_msg(request):
""" Fake the api login with 401 """
return HttpResponse("Sorry, Invalid credentials", status=401)
request_factory = RequestFactory()
request = request_factory.post("api/login")
request.user = AnonymousUser()
request.session = SessionStore()
request.META["HTTP_X_FORWARDED_FOR"] = "192.168.24.24"
for _ in range(3):
fake_api_401_login_view_without_msg(request)
data_out = utils.get_blocked_ips()
self.assertEqual(data_out, [])
fake_api_401_login_view_without_msg(request)
data_out = utils.get_blocked_ips()
self.assertEqual(data_out, ["192.168.24.24"])
示例12: request
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def request(self, **request):
request["user"] = None
request = super(RequestFactory, self).request(**request)
request.user = AnonymousUser()
request.session = SessionStore()
request._messages = FallbackStorage(request)
return request
示例13: request
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def request(self, user=None, **request):
request = super(RequestFactory, self).request(**request)
request.user = AnonymousUser()
request.session = SessionStore()
request._messages = FallbackStorage(request)
return request
示例14: logged_in_request
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def logged_in_request() -> HttpRequest:
request = HttpRequest()
request.session = SessionStore()
username = "user1"
password = "bar"
User.objects.create_user(username=username, password=password)
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
request.method = 'GET'
request.user = user
return request
示例15: test_not_exist_view
# 需要导入模块: from django.contrib.sessions.backends import db [as 别名]
# 或者: from django.contrib.sessions.backends.db import SessionStore [as 别名]
def test_not_exist_view(self):
from django.contrib.messages.storage import default_storage
request = HttpRequest()
request.session = SessionStore('asd')
request._messages = default_storage(request)
response1 = self.admin.easy_list_view(request, 'not')
response2 = self.admin.easy_object_view(request, 1, 'not')
self.assertEqual(response1.status_code, 302)
self.assertEqual(response2.status_code, 302)
self.assertEqual(len(request._messages._queued_messages), 2)