def test_restore_with_invalid_values(self): """FlashScope: Should not restore the flash using a dict with invalid values. """ data = {_SESSION_KEY: {}, _USED_KEY: None} self.assertRaises(ValueError, lambda: FlashScope(data)) data = {_SESSION_KEY: None, _USED_KEY: {}} self.assertRaises(ValueError, lambda: FlashScope(data))
def setUp(self): """Create a request with an used message inside the flash. """ self.request = HttpRequest() self.request.flash = self.flash = FlashScope() self.flash['message'] = 'Message' self.flash.update()
def test_empty_to_dict(self): """FlashScope: Should export the flash data to a dict even if it's empty. """ self.flash = FlashScope() expected_data = {_SESSION_KEY: {}, _USED_KEY: {}} data = self.flash.to_dict() self.assertEqual(expected_data, data)
def _get_flash_from_storage(request): """Gets the flash from the storage, adds it to the given request and returns it. A new :class:`FlashScope` is used if the storage is empty. """ flash = storage.get(request) or FlashScope() setattr(request, CONTEXT_VAR, flash) return flash
def setUp(self): """Creates a cookie-based flash storage for testing. """ self.request = HttpRequest() self.response = HttpResponse('') self.flash = FlashScope() self.storage = cookie.FlashStorageClass()
def setUp(self): """Creates a JSON-based codec and a sample flash. """ self.expected = '{"_session": {"info": "Info"}, ' \ '"_used": {"info": null}}' self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update()
def get_context_from_session(request): """Gets the :class:`FlashScope` object from the user's session and :meth:`update` it. If this object couldn't be found, this method returns a brand new :class:`FlashScope` object. """ context = FlashScope() if hasattr(request, 'session') and _SESSION_KEY in request.session: context = request.session[_SESSION_KEY] context.update() return context
def setUp(self): """Creates a codec and a sample flash. """ self.codec = json_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = 'eyJfc2Vzc2lvbiI6IHsiaW5mbyI6ICJJbmZvIn0sICJfdXNlZCI6' \ 'IHsiaW5mbyI6IG51bGx9fWZk\nNDViYTljMmU3MWJlZjBjYjcxOW' \ 'EwYjdlYzJlZjUx\n'
def setUp(self): """Creates a JSON\zlib-based codec and a sample flash. """ self.expected = 'x\x9c\xabV\x8a/N-.\xce\xcc\xcfS\xb2R\xa8V\xca\xccK' \ '\xcb\x072\x94<At\xad\x8e\x82R|iqj\n\xb2T^iNNm-\x00' \ '\xf5\xa2\x12\x03' self.codec = json_zlib_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update()
def get_context_from_request(request): """Gets the :class:`FlashScope` object from the *request* and returns it. If it couldn't be found, the method returns a brand new :class:`FlashScope` object. """ context = FlashScope() if hasattr(request, CONTEXT_VAR): context = getattr(request, CONTEXT_VAR) if not isinstance(context, FlashScope): raise TypeError('Invalid Flash scope object: %s' % \ repr(context)) return context
def setUp(self): """Creates a Pickle-based codec and a sample flash. """ self.codec = pickle_impl.CodecClass() self.flash = FlashScope() self.flash['info'] = 'Info' self.flash.update() self.expected = '\x80\x02cdjangoflash.models\nFlashScope\nq\x01)\x81q' \ '\x02}q\x03(U\x03nowq\x04cdjangoflash.models\n_Immedi' \ 'ateFlashScopeAdapter\nq\x05)\x81q\x06}q\x07U\x08dele' \ 'gateq\x08h\x02sbU\x08_sessionq\t}q\nU\x04infoq\x0bU' \ '\x04Infoq\x0csU\x05_usedq\r}q\x0eh\x0bNsub.'
def test_restore_immutability(self): """FlashScope: Should restore the flash using a shallow copy of a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual('Info', self.flash['info']) del data[_SESSION_KEY]['info'] self.assertTrue('info' in self.flash)
def test_restore(self): """FlashScope: Should restore the flash using a dict. """ data = { _SESSION_KEY: { 'info': 'Info', 'error': 'Error' }, _USED_KEY: { 'error': None } } self.flash = FlashScope(data) self.assertEqual(2, len(self.flash)) self.assertEqual('Info', self.flash['info']) self.assertEqual('Error', self.flash['error']) self.flash.update() self.assertEqual('Info', self.flash['info']) self.assertFalse('error' in self.flash)
def flash(request): """This context processor gets the :class:`FlashScope` object from the current *request* and adds it to the template context: .. code-block:: html+django <html> <head></head> <body> request.flash['message'] = {{ flash.message }} </body> </html> """ flash_scope = None try: flash_scope = getattr(request, CONTEXT_VAR) if not isinstance(flash_scope, FlashScope): raise SuspiciousOperation('Invalid flash: %s' % repr(flash_scope)) except AttributeError: # Exposes an empty flash when none is available flash_scope = FlashScope() return {CONTEXT_VAR: flash_scope}
def setUp(self): self.request = HttpRequest() self.scope = FlashScope(); setattr(self.request, CONTEXT_VAR, self.scope);
def setUp(self): """Create a FlashScope object to be used by the test methods. """ self.flash = FlashScope()
def test_restore_with_invalid_type(self): """FlashScope: Should not restore the flash using an invalid object. """ self.assertRaises(TypeError, lambda: FlashScope('invalid_data'))
def decode(self, encoded_flash): """Restores the *flash* from the given JSON string. """ return FlashScope(json.loads(encoded_flash))