z3c.flashmessage


Namez3c.flashmessage JSON
Version 3.0 PyPI version JSON
download
home_pagehttps://github.com/zopefoundation/z3c.flashmessage
SummaryA package for sending `flash messages` to users.
upload_time2023-02-08 15:27:57
maintainer
docs_urlNone
authorJasper Op de Coul, Christian Theune
requires_python>=3.7
licenseZPL 2.1
keywords zope3 message zope session
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. contents::



==============
Flash messages
==============

Components to display small messages to users.


Sending a message to the current user
=====================================

To send a message to the current user, you can use the session-based message
source. Let's set one up:

>>> from z3c.flashmessage.sources import SessionMessageSource
>>> from __future__ import unicode_literals
>>> source = SessionMessageSource()

>>> source.send('The world will come to an end in 40 seconds!')

The source allows to list all current messages:

>>> m = list(source.list())
>>> m
[<z3c.flashmessage.message.Message object at 0x...>]
>>> m[0].message
'The world will come to an end in 40 seconds!'
>>> str(m[0].type)
'message'

Receiving messages
==================

The standard message that is generated removes itself from the source when it
is received. The receiver will call `prepare()` on the message before it is
handed out to the code that receives it:

>>> m[0].prepare(source)
>>> list(source.list())
[]

There also is another default message that does not delete itself when being
read:

>>> from z3c.flashmessage.message import PersistentMessage
>>> source.send(PersistentMessage('I will stay forever!'))
>>> m = list(source.list())[0]
>>> m.message
'I will stay forever!'
>>> m.prepare(source)
>>> list(source.list())
[<z3c.flashmessage.message.PersistentMessage object at 0x...>]

Global receiver
===============

There is a global receiver that queries all message sources that are set up as
utilities. Let's set up a session message source as a utility:

>>> from zope.component import provideUtility
>>> provideUtility(source)
>>> source.send('Test!')

>>> from z3c.flashmessage.sources import RAMMessageSource
>>> source2 = RAMMessageSource()
>>> provideUtility(source2, name='other')
>>> source2.send('Test 2!')
>>> source2.send('Test 3!')

>>> from z3c.flashmessage.receiver import GlobalMessageReceiver
>>> receiver = GlobalMessageReceiver()
>>> m = list(receiver.receive())
>>> len(m)
4
>>> m[0].message
'I will stay forever!'
>>> m[1].message
'Test!'
>>> m[2].message
'Test 2!'
>>> m[3].message
'Test 3!'

After the receiver handed out the messages, they are gone from the
sources, because the receiver notifies the messages that they were
read:

>>> len(list(receiver.receive()))
1


Filtering message types
=======================

When listing messages from a message source, we can restrict which messages we
see. If we don't give a type, then all messages are returned. The default type
of a message is `message`:

>>> source3 = RAMMessageSource()
>>> source3.send('Test 2!')
>>> list(source3.list())
[<z3c.flashmessage.message.Message object at 0x...>]
>>> list(source3.list('message'))
[<z3c.flashmessage.message.Message object at 0x...>]
>>> list(source3.list('somethingelse'))
[]


Performance and Scalability Issues
==================================

By default, messages are stored persistently in the ZODB using
zope.session.  This can be a significant scalability problem; see
design.txt in zope.session for more information.  You should think
twice before using flashmessages for unauthenticated users, as this
can easily lead to unnecessary database growth on anonymous page
views, and conflict errors under heavy load.

One solution is to configure your system to store flashmessages in
RAM. You would do this by configuring a utility providing
``z3c.flashmessage.interfaces.IMessageSource`` with the factory set to
``z3c.flashmessage.sources.RAMMessageSource``, and a specific name if
your application expects one.

RAM storage is much faster and removes the persistence issues
described above, but there are two new problems.  First, be aware that
if your server process restarts for any reason, all unread
flashmessages will be lost.  Second, if you cluster your application
servers using e.g. ZEO, you must also ensure that your load-balancer
supports session affinity (so a specific client always hits the same
back end server).  This somewhat reduces the performance benefits of
clustering.


=======
CHANGES
=======

3.0 (2023-02-08)
================

- Drop support for Python 2.7, 3.4, 3.5, 3.6.

- Add support for Python 3.8, 3.9, 3.10, 3.11.

- Ensure all objects have consistent resolution orders.


2.1 (2018-11-12)
================

- Claim support for Python 3.6, 3.7, PyPy and PyPy3.

- Drop support for Python 3.3.

- Drop support for ``python setup.py test``.


2.0 (2016-08-08)
================

- Standardize namespace ``__init__``.

- Claim compatibility for Python 3.3, 3.4, and 3.5.

1.3 (2010-10-28)
================

- ``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`.


1.1 (2010-10-02)
================

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


1.0 (2007-12-06)
================

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


1.0b2 (2007-09-12)
==================

* Bugfix: When there was more than one message in a source not all messages
  would be returned by the receiver.

1.0b1 (2007-08-22)
==================

* Initial public release.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zopefoundation/z3c.flashmessage",
    "name": "z3c.flashmessage",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "zope3 message zope session",
    "author": "Jasper Op de Coul, Christian Theune",
    "author_email": "jasper@infrae.com, mail@gocept.com",
    "download_url": "https://files.pythonhosted.org/packages/0d/22/5a1797084a777b1afde17a88f861f2f552f0ecb655344147c1f3a7979723/z3c.flashmessage-3.0.tar.gz",
    "platform": null,
    "description": ".. contents::\n\n\n\n==============\nFlash messages\n==============\n\nComponents to display small messages to users.\n\n\nSending a message to the current user\n=====================================\n\nTo send a message to the current user, you can use the session-based message\nsource. Let's set one up:\n\n>>> from z3c.flashmessage.sources import SessionMessageSource\n>>> from __future__ import unicode_literals\n>>> source = SessionMessageSource()\n\n>>> source.send('The world will come to an end in 40 seconds!')\n\nThe source allows to list all current messages:\n\n>>> m = list(source.list())\n>>> m\n[<z3c.flashmessage.message.Message object at 0x...>]\n>>> m[0].message\n'The world will come to an end in 40 seconds!'\n>>> str(m[0].type)\n'message'\n\nReceiving messages\n==================\n\nThe standard message that is generated removes itself from the source when it\nis received. The receiver will call `prepare()` on the message before it is\nhanded out to the code that receives it:\n\n>>> m[0].prepare(source)\n>>> list(source.list())\n[]\n\nThere also is another default message that does not delete itself when being\nread:\n\n>>> from z3c.flashmessage.message import PersistentMessage\n>>> source.send(PersistentMessage('I will stay forever!'))\n>>> m = list(source.list())[0]\n>>> m.message\n'I will stay forever!'\n>>> m.prepare(source)\n>>> list(source.list())\n[<z3c.flashmessage.message.PersistentMessage object at 0x...>]\n\nGlobal receiver\n===============\n\nThere is a global receiver that queries all message sources that are set up as\nutilities. Let's set up a session message source as a utility:\n\n>>> from zope.component import provideUtility\n>>> provideUtility(source)\n>>> source.send('Test!')\n\n>>> from z3c.flashmessage.sources import RAMMessageSource\n>>> source2 = RAMMessageSource()\n>>> provideUtility(source2, name='other')\n>>> source2.send('Test 2!')\n>>> source2.send('Test 3!')\n\n>>> from z3c.flashmessage.receiver import GlobalMessageReceiver\n>>> receiver = GlobalMessageReceiver()\n>>> m = list(receiver.receive())\n>>> len(m)\n4\n>>> m[0].message\n'I will stay forever!'\n>>> m[1].message\n'Test!'\n>>> m[2].message\n'Test 2!'\n>>> m[3].message\n'Test 3!'\n\nAfter the receiver handed out the messages, they are gone from the\nsources, because the receiver notifies the messages that they were\nread:\n\n>>> len(list(receiver.receive()))\n1\n\n\nFiltering message types\n=======================\n\nWhen listing messages from a message source, we can restrict which messages we\nsee. If we don't give a type, then all messages are returned. The default type\nof a message is `message`:\n\n>>> source3 = RAMMessageSource()\n>>> source3.send('Test 2!')\n>>> list(source3.list())\n[<z3c.flashmessage.message.Message object at 0x...>]\n>>> list(source3.list('message'))\n[<z3c.flashmessage.message.Message object at 0x...>]\n>>> list(source3.list('somethingelse'))\n[]\n\n\nPerformance and Scalability Issues\n==================================\n\nBy default, messages are stored persistently in the ZODB using\nzope.session.  This can be a significant scalability problem; see\ndesign.txt in zope.session for more information.  You should think\ntwice before using flashmessages for unauthenticated users, as this\ncan easily lead to unnecessary database growth on anonymous page\nviews, and conflict errors under heavy load.\n\nOne solution is to configure your system to store flashmessages in\nRAM. You would do this by configuring a utility providing\n``z3c.flashmessage.interfaces.IMessageSource`` with the factory set to\n``z3c.flashmessage.sources.RAMMessageSource``, and a specific name if\nyour application expects one.\n\nRAM storage is much faster and removes the persistence issues\ndescribed above, but there are two new problems.  First, be aware that\nif your server process restarts for any reason, all unread\nflashmessages will be lost.  Second, if you cluster your application\nservers using e.g. ZEO, you must also ensure that your load-balancer\nsupports session affinity (so a specific client always hits the same\nback end server).  This somewhat reduces the performance benefits of\nclustering.\n\n\n=======\nCHANGES\n=======\n\n3.0 (2023-02-08)\n================\n\n- Drop support for Python 2.7, 3.4, 3.5, 3.6.\n\n- Add support for Python 3.8, 3.9, 3.10, 3.11.\n\n- Ensure all objects have consistent resolution orders.\n\n\n2.1 (2018-11-12)\n================\n\n- Claim support for Python 3.6, 3.7, PyPy and PyPy3.\n\n- Drop support for Python 3.3.\n\n- Drop support for ``python setup.py test``.\n\n\n2.0 (2016-08-08)\n================\n\n- Standardize namespace ``__init__``.\n\n- Claim compatibility for Python 3.3, 3.4, and 3.5.\n\n1.3 (2010-10-28)\n================\n\n- ``SessionMessageSource`` implicitly created sessions when the client was\n  reading the messages from the source. Changed internal API so reading no\n  longer creates a session when it not yet exists.\n\n1.2 (2010-10-19)\n================\n\n* Removed test dependency on `zope.app.zcmlfiles`.\n\n\n1.1 (2010-10-02)\n================\n\n* Removed test dependency on `zope.app.testing`.\n\n\n1.0 (2007-12-06)\n================\n\n* Updated dependency to `zope.session` instead of `zope.app.session` to get\n  rid of deprecation warnings.\n\n\n1.0b2 (2007-09-12)\n==================\n\n* Bugfix: When there was more than one message in a source not all messages\n  would be returned by the receiver.\n\n1.0b1 (2007-08-22)\n==================\n\n* Initial public release.\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "A package for sending `flash messages` to users.",
    "version": "3.0",
    "split_keywords": [
        "zope3",
        "message",
        "zope",
        "session"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ae768170e548a887ceae3db29b4254a6f3f722e12def5b25a9f9f07ab0629912",
                "md5": "79402f35455cece772b8db92829dba31",
                "sha256": "2e68faf87bbe3011bba79dd75a1b37ee309558a917ba7002569f2c6d9604fef6"
            },
            "downloads": -1,
            "filename": "z3c.flashmessage-3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "79402f35455cece772b8db92829dba31",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 12450,
            "upload_time": "2023-02-08T15:27:55",
            "upload_time_iso_8601": "2023-02-08T15:27:55.967260Z",
            "url": "https://files.pythonhosted.org/packages/ae/76/8170e548a887ceae3db29b4254a6f3f722e12def5b25a9f9f07ab0629912/z3c.flashmessage-3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0d225a1797084a777b1afde17a88f861f2f552f0ecb655344147c1f3a7979723",
                "md5": "39e47eed4ebecf64c8c85610e58599fd",
                "sha256": "d512630c5bc7e3bfb11fa58018de21f7fa33195d650247dd0b533c5b4c9b7d96"
            },
            "downloads": -1,
            "filename": "z3c.flashmessage-3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "39e47eed4ebecf64c8c85610e58599fd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 11828,
            "upload_time": "2023-02-08T15:27:57",
            "upload_time_iso_8601": "2023-02-08T15:27:57.566723Z",
            "url": "https://files.pythonhosted.org/packages/0d/22/5a1797084a777b1afde17a88f861f2f552f0ecb655344147c1f3a7979723/z3c.flashmessage-3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-08 15:27:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "zopefoundation",
    "github_project": "z3c.flashmessage",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "z3c.flashmessage"
}
        
Elapsed time: 0.05132s