我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用django.conf.settings.SESSION_ENGINE。
def logout(self): """ Removes the authenticated user's cookies and session object. Causes the authenticated user to be logged out. """ from django.contrib.auth import get_user, logout request = HttpRequest() engine = import_module(settings.SESSION_ENGINE) if self.session: request.session = self.session request.user = get_user(request) else: request.session = engine.SessionStore() logout(request) self.cookies = SimpleCookie()
def test_log_complete_grammar_unit_test(self): g = Grammar(prods="{'A': ['xA', 'Bz'],'B': ['yB']}", nonTerminals="AB", terminals="xyz", startSymbol="A") g.save() request = HttpRequest() engine = import_module(settings.SESSION_ENGINE) session_key = None request.session = engine.SessionStore(session_key) request.session['gid'] = g.pk request.session['score'] = 5 request.user = User.objects.get(username="test") before_nComplete = g.nComplete stats.log_complete_grammar(request) g = Grammar.objects.get(pk=g.pk) self.assertEqual(before_nComplete + 1, g.nComplete) # this test needs to be tested more thoroughly
def test_session_language(self): page = api.create_page("home", "nav_playground.html", "en", published=True) api.create_title('fr', "home", page) page.publish('fr') page.publish('en') response = self.client.get('/') self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/en/') 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 # ugly and long set of session session = self.client.session session[LANGUAGE_SESSION_KEY] = 'fr' session.save() response = self.client.get('/') self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/fr/') self.client.get('/en/') self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], 'en') response = self.client.get('/') self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/en/')
def _fastlogin(self, **credentials): session = import_module(settings.SESSION_ENGINE).SessionStore() session.save() request = AttributeObject(session=session, META={}) user = authenticate(**credentials) login(request, user) session.save() # We need to "warm up" the webdriver as we can only set cookies on the # current domain self.driver.get(self.live_server_url) # While we don't care about the page fully loading, Django will freak # out if we 'abort' this request, so we wait patiently for it to finish self.wait_page_loaded() self.driver.add_cookie({ 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', 'domain': urlparse(self.live_server_url).hostname }) self.driver.get('{0}/?{1}'.format( self.live_server_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON') )) self.wait_page_loaded()
def get_session_store(session_key=None): engine = import_module(settings.SESSION_ENGINE) # Implement a database session store object that will contain the session key. store = engine.SessionStore(session_key=session_key) if session_key is None: store.save() else: store.load() return store
def setSessionValues(self, **kwargs): settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file' engine = import_module(settings.SESSION_ENGINE) store = engine.SessionStore() for key in kwargs: store[key] = kwargs[key] self.request.session[key] = kwargs[key] store.save() self.session = store self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
def test_log_skip_grammar_unit_test(self): g = Grammar(prods="{'A': ['xA', 'Bz'],'B': ['yB']}", nonTerminals="AB", terminals="xyz", startSymbol="A") g.save() request = HttpRequest() engine = import_module(settings.SESSION_ENGINE) session_key = None request.session = engine.SessionStore(session_key) request.session['gid'] = g.pk request.session['hide_explainer'] = False request.user = User.objects.get(username="test") before_nSkip = g.nSkip stats.log_skip_grammar(request) g = Grammar.objects.get(pk=g.pk) self.assertEqual(before_nSkip, g.nSkip)
def handle(self, **options): engine = import_module(settings.SESSION_ENGINE) try: engine.SessionStore.clear_expired() except NotImplementedError: self.stderr.write("Session engine '%s' doesn't support clearing " "expired sessions.\n" % settings.SESSION_ENGINE)
def __init__(self): engine = import_module(settings.SESSION_ENGINE) self.SessionStore = engine.SessionStore
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) 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 {}
def _login(self, user): from django.contrib.auth import login 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)
def __init__(self, get_response=None): self.get_response = get_response engine = import_module(settings.SESSION_ENGINE) self.SessionStore = engine.SessionStore
def check_session_caching(app_configs, **kwargs): # pragma: no cover errors = [] cached_engines = { 'django.contrib.sessions.backends.cache', 'django.contrib.sessions.backends.cached_db'} if ('locmem' in settings.CACHES['default']['BACKEND'] and settings.SESSION_ENGINE in cached_engines): errors.append( Warning( 'Session caching cannot work with locmem backend', 'User sessions need to be globally shared, use a cache server like Redis.', 'saleor.W001')) return errors
def get_request(*args, **kwargs): request = DummyRequest(*args, **kwargs) engine = import_module(settings.SESSION_ENGINE) request.session = engine.SessionStore(None) return request
def get_request(): request = HttpRequest() engine = import_module(settings.SESSION_ENGINE) request.session = engine.SessionStore(None) return request
def process_request(self, request): engine = import_module(settings.SESSION_ENGINE) session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None) request.session = engine.SessionStore( ip=request.META.get('REMOTE_ADDR', ''), user_agent=request.META.get('HTTP_USER_AGENT', ''), session_key=session_key )
def setUp(self): if django.VERSION < (1, 8,): engine = import_module(settings.SESSION_ENGINE) s = engine.SessionStore() s.save() self.client.cookies[ settings.SESSION_COOKIE_NAME] = s.session_key
def session(self): """ Obtains the current session variables. """ engine = import_module(settings.SESSION_ENGINE) cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) if cookie: return engine.SessionStore(cookie.value) session = engine.SessionStore() session.save() self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key return session
def _login(self, user, backend=None): from django.contrib.auth import login 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, backend) # 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)