def _start_response(status, response_headers, exc_info=None): # Iterate the new flash messages in the WSGI, setting a 'flash' # cookie for each one. flash = environ[ENVIRON_KEY] cookies_mgr = cookies.get_cookies(environ) for i, flash in enumerate(flash.flashes): cookies_mgr.set_cookie(('%s.%d'% (COOKIE_NAME, i), flash)) # Call wrapped app's start_response. return start_response(status, response_headers, exc_info)
def set_authenticated_username(request, username): """ Automatically log in. """ # Find the reqoze.who plugin that will remember the identity. rememberer = request.environ['repoze.who.plugins']['auth_tkt'] # Build an identity. repoze.who seems to only *need* the username but there # are other bits that can go in the cookie. identity = {'repoze.who.userid': username} # Set the cookies that the remember says to set. cookie_mgr = cookies.get_cookies(request.environ) # XXX IIdentifier.remember() sometimes returns None (if there's no changes) # so we can't blindly iterate the returned value. headers = rememberer.remember(request.environ, identity) if headers: for _, cookie_header in headers: cookie_mgr.set_cookie(cookie_header)
def get_messages(self): """ Retrieve flash messages found in the request's cookies, returning them as a list of (type, message) tuples and deleting the cookies. """ messages = [] cookies_mgr = cookies.get_cookies(self.request.environ) for i in itertools.count(): cookie_name = '%s.%d'% (COOKIE_NAME, i) # Try to find the next message. Leave the loop if it does not exist. message = self.request.cookies.get(cookie_name) if not message: break # Remove the cookie, presumably it will be displayed shortly. cookies_mgr.delete_cookie(cookie_name) # Parse and yield the message. try: type, message = message.split(':', 1) except ValueError: # Skip an unparseable cookie value. pass else: messages.append((type or None, message)) return messages