我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.conf.settings.SESSION_COOKIE_DOMAIN。
def set_cookie(response, key, value, expires=None): """ ??????????????? ??????? ??? ????????? ???. ???????? expires ????? ???? None (??? ????????? ???? ?? ????? ??????); ??? ??????, ???????????? ???-?? ???? ?? ?? ?????????. """ if isinstance(expires, int): expires = datetime.utcnow() + timedelta(days=expires) response.set_cookie( key, value, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None )
def get(self, request, *args, **kwargs): url = request.GET.get('next', request.META.get('HTTP_REFERER', '/')) url = url if is_safe_url(url, host=request.get_host()) else '/' resp = HttpResponseRedirect(url) locale = request.GET.get('locale') if locale in [lc for lc, ll in settings.LANGUAGES]: if request.user.is_authenticated: request.user.locale = locale request.user.save() max_age = 10 * 365 * 24 * 60 * 60 resp.set_cookie(settings.LANGUAGE_COOKIE_NAME, locale, max_age=max_age, expires=(datetime.utcnow() + timedelta(seconds=max_age)).strftime( '%a, %d-%b-%Y %H:%M:%S GMT'), domain=settings.SESSION_COOKIE_DOMAIN) with override(locale): messages.success(request, phrases.cfp.locale_change_success) return resp
def subscribe_to_sales_list(request): def set_cookie(response, key, value, days_expire=7): max_age = days_expire * 24 * 60 * 60 if days_expire is None: max_age = 365 * 24 * 60 * 60 # one year expires_raw = datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age) expires = expires_raw.strftime("%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie(key, value, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None) data = request.data name = data.get("name") company = data.get("company") email = data.get("email") mailchimp.subscribe_to_sales_lead(name, company, email) network_response = Vestivise.network_response("success") set_cookie(network_response, 'demo_access', True) return network_response # TEST VIEWS
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie or delete the session cookie if the session has been emptied. """ try: accessed = request.session.accessed modified = request.session.modified empty = request.session.is_empty() except AttributeError: pass else: # First check if we need to delete this cookie. # The session should be deleted only if the session is entirely empty if settings.SESSION_COOKIE_NAME in request.COOKIES and empty: response.delete_cookie(settings.SESSION_COOKIE_NAME, domain=settings.SESSION_COOKIE_DOMAIN) else: if accessed: patch_vary_headers(response, ('Cookie',)) if (modified or settings.SESSION_SAVE_EVERY_REQUEST) and not empty: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: request.session.save() response.set_cookie(settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
def _update_cookie(self, encoded_data, response): """ Either sets the cookie with the encoded data if there is any data to store, or deletes the cookie. """ if encoded_data: response.set_cookie(self.cookie_name, encoded_data, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) else: response.delete_cookie(self.cookie_name, domain=settings.SESSION_COOKIE_DOMAIN)
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 _update_cookie(self, encoded_data, response): """ Either sets the cookie with the encoded data if there is any data to store, or deletes the cookie. """ if encoded_data: response.set_cookie( self.cookie_name, encoded_data, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None, ) else: response.delete_cookie(self.cookie_name, domain=settings.SESSION_COOKIE_DOMAIN)
def logout(request): auth.logout(request) response = HttpResponseRedirect('/') response.delete_cookie('sso_token', domain=settings.SESSION_COOKIE_DOMAIN) return response #@login_required
def login(request): title = '????' user = request.user passwd_url = settings.PASSWD_URL show_captcha = settings.SHOW_CAPTCHA back = request.GET.get('back') if not back: back = '/' if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') code = request.POST.get('code') if show_captcha: if not Captcha(request).check(code): result = '?????' return render_to_response('sso/login.html',locals()) user = auth.authenticate(username=username,password=password) if user is not None: auth.login(request,user) token_confirm = Token(settings.SECRET_KEY) token_key = '%s' % username token = token_confirm.generate_validate_token(token_key) #redirect_uri = '%s?token=%s' % (back, token) #return HttpResponseRedirect(redirect_uri) response = HttpResponseRedirect(back) response.set_cookie('sso_token', token, settings.COOKIE_EXPIRES,domain=settings.SESSION_COOKIE_DOMAIN) return response else: result = '????????' return render_to_response('sso/login.html',locals())
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. """ user = authenticate(**credentials) if user and user.is_active: # Create a fake request to store login details. request = HttpRequest() if self.session: request.session = self.session else: request.session = SessionStore('Python/2.7', '127.0.0.1') 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
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie. """ try: accessed = request.session.accessed modified = request.session.modified except AttributeError: pass else: if accessed: patch_vary_headers(response, ('Cookie',)) if modified or settings.SESSION_SAVE_EVERY_REQUEST: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: request.session.save() response.set_cookie( settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
def process_request(request): request.js_storage = JS_STORAGE.copy() request.js_storage.update( cookie_domain=settings.SESSION_COOKIE_DOMAIN, )
def delete_cookie(response, key): """ ??????????????? ??????? ??? ???????? ???. """ response.delete_cookie( key, domain=settings.SESSION_COOKIE_DOMAIN, )
def get_cookie_domain(request): if '.' not in request.host: # As per spec, browsers do not accept cookie domains without dots in it, # e.g. "localhost", see http://curl.haxx.se/rfc/cookie_spec.html return None default_domain, default_port = split_domain_port(settings.SITE_NETLOC) if request.host == default_domain: # We are on our main domain, set the cookie domain the user has chosen return settings.SESSION_COOKIE_DOMAIN else: # We are on an organizer's custom domain, set no cookie domain, as we do not want # the cookies to be present on any other domain. Setting an explicit value can be # dangerous, see http://erik.io/blog/2014/03/04/definitive-guide-to-cookie-domains/ return None
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie or delete the session cookie if the session has been emptied. """ try: accessed = request.session.accessed modified = request.session.modified empty = request.session.is_empty() except AttributeError: pass else: # First check if we need to delete this cookie. # The session should be deleted only if the session is entirely empty if settings.SESSION_COOKIE_NAME in request.COOKIES and empty: response.delete_cookie(settings.SESSION_COOKIE_NAME, domain=settings.SESSION_COOKIE_DOMAIN) else: if accessed: patch_vary_headers(response, ('Cookie',)) if modified or settings.SESSION_SAVE_EVERY_REQUEST: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: request.session.save() response.set_cookie(settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie or delete the session cookie if the session has been emptied. """ try: accessed = request.session.accessed modified = request.session.modified empty = request.session.is_empty() except AttributeError: pass else: # First check if we need to delete this cookie. # The session should be deleted only if the session is entirely empty if settings.SESSION_COOKIE_NAME in request.COOKIES and empty: response.delete_cookie(settings.SESSION_COOKIE_NAME) else: if accessed: patch_vary_headers(response, ('Cookie',)) if modified or settings.SESSION_SAVE_EVERY_REQUEST: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: request.session.save() response.set_cookie(settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
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)
def process_response(self, request, response): """ If request.session was modified, or if the configuration is to save the session every time, save the changes and set a session cookie. """ try: accessed = request.session.accessed modified = request.session.modified except AttributeError: pass else: if accessed: patch_vary_headers(response, ('Cookie',)) if modified or settings.SESSION_SAVE_EVERY_REQUEST: if request.session.get_expire_at_browser_close(): max_age = None expires = None else: max_age = request.session.get_expiry_age() expires_time = time.time() + max_age expires = cookie_date(expires_time) # Save the session data and refresh the client cookie. # Skip session save for 500 responses, refs #3881. if response.status_code != 500: request.session.save() response.set_cookie(settings.SESSION_COOKIE_NAME, request.session.session_key, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, path=settings.SESSION_COOKIE_PATH, secure=settings.SESSION_COOKIE_SECURE or None, httponly=settings.SESSION_COOKIE_HTTPONLY or None) return response
def _update_cookie(self, encoded_data, response): """ Either sets the cookie with the encoded data if there is any data to store, or deletes the cookie. """ if encoded_data: response.set_cookie(self.cookie_name, encoded_data, domain=settings.SESSION_COOKIE_DOMAIN) else: response.delete_cookie(self.cookie_name, domain=settings.SESSION_COOKIE_DOMAIN)
def process_response(self, request, response): id = request.session.get('referrer') if not id: return response max_age = 365 * 24 * 60 * 60 expires = (datetime.utcnow() + timedelta(seconds=max_age)) expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('referrer', id, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None) return response
def process_response(self, request, response): name = request.session.get('campaign') if not name: return response max_age = 365 * 24 * 60 * 60 expires = (datetime.utcnow() + timedelta(seconds=max_age)) expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('campaign', name, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None) return response
def save_session(self): self.session.save() cookie_data = { 'max-age': None, 'path': '/', 'domain': settings.SESSION_COOKIE_DOMAIN, 'secure': settings.SESSION_COOKIE_SECURE or None, 'expires': None, } session_cookie = settings.SESSION_COOKIE_NAME self.client.cookies[session_cookie] = self.session.session_key self.client.cookies[session_cookie].update(cookie_data)