|
| 1 | +Python code |
| 2 | +----------- |
| 3 | + |
| 4 | +Set a message: |
| 5 | + |
| 6 | +.. code:: python |
| 7 | +
|
| 8 | + msg = _('My important message.') |
| 9 | + if request.website: |
| 10 | + request.website.add_status_message(msg) |
| 11 | +
|
| 12 | +By default the message type is ``info``. The title (the label at the |
| 13 | +beginning of the message) matches the message type: |
| 14 | + |
| 15 | +- 'info': 'Info' |
| 16 | +- 'success': 'Success' |
| 17 | +- 'danger': 'Error' |
| 18 | +- 'warning': 'Warning' |
| 19 | + |
| 20 | +You can change message parameters: |
| 21 | + |
| 22 | +.. code:: python |
| 23 | +
|
| 24 | + msg = _('Watch out!') |
| 25 | + if request.website: |
| 26 | + request.website.add_status_message(msg, type_='warning', title='Oh no') |
| 27 | +
|
| 28 | +Messages will be displayed like this: |
| 29 | + |
| 30 | +.. image:: ./images/preview.png |
| 31 | + |
| 32 | +Autodismiss |
| 33 | +----------- |
| 34 | + |
| 35 | +By default messages will be auto-dismissed after 8 seconds. |
| 36 | +You can turn this off by setting an ir.config_param like:: |
| 37 | + |
| 38 | + cms_status_message.autodismiss = 0 |
| 39 | + |
| 40 | +You can customize the timeout by setting the key:: |
| 41 | + |
| 42 | + cms_status_message.autodismiss_timeout = 3000 # milliseconds |
| 43 | + |
| 44 | + |
| 45 | +You can also customize this on demand when you create the message: |
| 46 | + |
| 47 | + |
| 48 | +.. code:: python |
| 49 | +
|
| 50 | + msg = _('I will disappear more slowly') |
| 51 | + options = {'autodismissTimeout': 10000} |
| 52 | + if request.website: |
| 53 | + request.website.add_status_message(msg, dismiss_options=options) |
| 54 | +
|
| 55 | +
|
| 56 | +Javascript code |
| 57 | +--------------- |
| 58 | + |
| 59 | +Dependencies: |
| 60 | + |
| 61 | +.. code:: javascript |
| 62 | +
|
| 63 | +
|
| 64 | + var msg_tool = require('cms_status_message.tool'); |
| 65 | + var core = require('web.core'); |
| 66 | + var _t = core._t; |
| 67 | +
|
| 68 | +Inject a custom message on the fly: |
| 69 | + |
| 70 | +.. code:: javascript |
| 71 | +
|
| 72 | + msg = { |
| 73 | + 'msg': _t('Item unpublished.'), |
| 74 | + 'title': _t('Warning'), |
| 75 | + 'type': 'warning' |
| 76 | + } |
| 77 | + msg_tool.render_messages(msg).then(function(html) { |
| 78 | + // wipe existing |
| 79 | + $('.status_message').remove(); |
| 80 | + // inject new |
| 81 | + $(html).hide().prependTo('#wrap').fadeIn('slow'); |
| 82 | + }); |
| 83 | +
|
| 84 | +
|
| 85 | +Add a status message to the session, useful if you want to show the |
| 86 | +message only after a redirect: |
| 87 | + |
| 88 | +.. code:: javascript |
| 89 | +
|
| 90 | + var msg = _t('Contratulations! You made it!.'); |
| 91 | + var options = {'title': _('My title'), 'dismissible': false}; |
| 92 | + msg_tool.add_message(msg, options); |
| 93 | +
|
| 94 | +Customize appereance |
| 95 | +-------------------- |
| 96 | + |
| 97 | +By default the alert box is added on top of ``<main />`` content. If you |
| 98 | +want to customize this behavior just override or disable |
| 99 | +``cms_status_message.add_status_message`` template. |
| 100 | + |
| 101 | + |
| 102 | +Test your theme look and feel |
| 103 | +----------------------------- |
| 104 | + |
| 105 | +Go to `/cms/status-message/display-test` to see how messages will look like |
| 106 | +when your theme is applied. |
0 commit comments