def _check_flash(self, response, *expected_messages): """ Check that ``expected_messages`` are defined in the WebFlash cookie. """ assert 'webflash' in response.cookies_set, "Such no WebFlash cookie" flash = url_unquote(response.cookies_set['webflash']) for msg in expected_messages: msg = '"%s"' % msg assert msg in flash, 'Message %s not in flash: %s' % (msg, flash)
def _check_flash(self, response, *expected_messages): """ Check that ``expected_messages`` are defined in the WebFlash cookie. """ cookies = SimpleCookie(response.headers['Set-Cookie']) assert 'webflash' in cookies, "Such no WebFlash cookie" flash = url_unquote(cookies['webflash'].value) for msg in expected_messages: msg = '"%s"' % msg assert msg in flash, 'Message %s not in flash: %s' % (msg, flash)
def pop_payload(self): # First try fetching it from the request req = request._current_obj() payload = req.environ.get('webflash.payload', {}) if not payload: payload = req.cookies.get(self.cookie_name, {}) if payload: payload = json.loads(url_unquote(payload)) if 'webflash.deleted_cookie' not in req.environ: response.delete_cookie(self.cookie_name) req.environ['webflash.delete_cookie'] = True return payload or {}
def pop_payload(self): """Fetch current flash message, status and related information. Fetching flash message deletes the associated cookie. """ # First try fetching it from the request req = request._current_obj() payload = req.environ.get('webflash.payload', {}) if not payload: payload = req.cookies.get(self.cookie_name, {}) if payload: payload = json.loads(url_unquote(payload)) if 'webflash.deleted_cookie' not in req.environ: response.delete_cookie(self.cookie_name) req.environ['webflash.delete_cookie'] = True return payload or {}
def pop_payload(self): """Fetch current flash message, status and related information. Fetching flash message deletes the associated cookie. """ # First try fetching it from the request req = request._current_obj() payload = req.environ.get("webflash.payload", {}) if not payload: payload = req.cookies.get(self.cookie_name, {}) if payload: payload = json.loads(url_unquote(payload)) if "webflash.deleted_cookie" not in req.environ: response.delete_cookie(self.cookie_name) req.environ["webflash.delete_cookie"] = True return payload or {}
def __call__(self, message, status=None, overwrite=False, **extra_payload): if response is None: raise ValueError("Must provide a response object or " "configure a callable that provides one") payload = [] if not overwrite and request: try: # Get payload, if already set before payload = request.environ['webflash.payload'] payload = json.loads(url_unquote(payload)) log.debug("Got payload from environ %d", id(request.environ)) if isinstance(payload, dict): log.debug('Upgrading old-style payload...') payload = [payload] except: # No previous payload set before pass payload.append( dict( # Force the message to be unicode so lazystrings, etc... are coerced message=unicode_text(message), status=status or self.default_status, **extra_payload )) payload = url_quote(json.dumps(payload)) if request: # Save the payload in environ too in case JavaScript is not being # used and the message is being displayed in the same request. request.environ['webflash.payload'] = payload log.debug("Setting payload in environ %d", id(request.environ)) log.debug("Setting payload in cookie") response.set_cookie(self.cookie_name, payload) if len(response.headers['Set-Cookie']) > 4096: raise ValueError('Flash value is too long (cookie would be >4k)')