Skip to content

Commit

Permalink
SessionMessageSource implicitly created sessions when the client …
Browse files Browse the repository at this point in the history
…was reading the messages from the source. Changed internal API so reading no longer creates a session when it not yet exists.
  • Loading branch information
Michael Howitz committed Oct 28, 2010
1 parent 20dbdcd commit 2c56337
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
11 changes: 6 additions & 5 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ CHANGES
1.3 (unreleased)
================

- Nothing changed yet.

- ``SessionMessageSource`` implicitly created sessions when the client was
reading the messages from the source. Changed internal API so reading no
longer creates a session when it not yet exists.

1.2 (2010-10-19)
================

* Removed test dependency on zope.app.zcmlfiles.
* Removed test dependency on `zope.app.zcmlfiles`.


1.1 (2010-10-02)
Expand All @@ -23,8 +24,8 @@ CHANGES
1.0 (2007-12-06)
================

* Updated dependency to `zope.session` instead of `zope.app.session` to get rid of
deprecation warnings.
* Updated dependency to `zope.session` instead of `zope.app.session` to get
rid of deprecation warnings.


1.0b2 (2007-09-12)
Expand Down
46 changes: 35 additions & 11 deletions src/z3c/flashmessage/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ListBasedMessageSource(object):
"""An (abstract) base class that stores messages
in a list.
Sub-classes have to define the attribute `_storage`.
Sub-classes have to define the method
`_get_storage(self, for_write=False)`.
"""

Expand All @@ -31,35 +32,58 @@ def send(self, message, type=u"message"):
# message for him. This is allowed by the API for convenience.
message = z3c.flashmessage.message.Message(message, type=type)
message.source = self
self._storage.append(message)
self._get_storage(for_write=True).append(message)

def list(self, type=None):
"""Return all messages of the given type from this source."""
for message in self._storage:
for message in self._get_storage(for_write=False):
if type is None or message.type == type:
yield message

def delete(self, message):
"""Remove the given message from the source."""
self._storage.remove(message)
self._get_storage(for_write=True).remove(message)

def _get_storage(self, for_write=False):
"""Return the storage which must have a list API.
When `for_write` is True the caller want's to write to the storage.
To be implemented in concreate sub classes
"""


class SessionMessageSource(ListBasedMessageSource):
"""Source which stores its data in the session of the user."""

_pkg_id = 'z3c.flashmessage'

@property
def _storage(self):
def _get_storage(self, for_write=False):
request = zope.security.management.getInteraction().participations[0]
session = zope.session.interfaces.ISession(
request)['z3c.flashmessage']
messages = session.setdefault('messages',
persistent.list.PersistentList())
return messages
session = zope.session.interfaces.ISession(request)
if for_write:
# Creating a new session when it does not exist yet.
session_data = session[self._pkg_id]
else:
# Making sure we do *not* create a new session when it not exists:
session_data = session.get(self._pkg_id, {})
return session_data.setdefault('messages',
persistent.list.PersistentList())


class RAMMessageSource(ListBasedMessageSource):
"""Source which stores its data in RAM.
Caution: This source is not able to store messages for individual users.
"""

zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)

def __init__(self):
super(RAMMessageSource, self).__init__()
self._storage = []

def _get_storage(self, for_write=False):
return self._storage

0 comments on commit 2c56337

Please sign in to comment.