diff --git a/cms_form/models/cms_form.py b/cms_form/models/cms_form.py index 52334348..52eed5fa 100644 --- a/cms_form/models/cms_form.py +++ b/cms_form/models/cms_form.py @@ -233,3 +233,6 @@ def form_process_POST(self, render_values): def add_status_message(self, msg, **kw): self.env["ir.http"].add_status_message(msg, request=self.o_request, **kw) + + def clear_status_messages(self): + self.env["ir.http"].clear_status_messages() diff --git a/cms_status_message/models/ir_http.py b/cms_status_message/models/ir_http.py index 516415f8..5db5cfde 100644 --- a/cms_status_message/models/ir_http.py +++ b/cms_status_message/models/ir_http.py @@ -105,3 +105,16 @@ def get_status_message(self, session=None): if session: return session.pop("status_message", []) return [] + + @api.model + def clear_status_messages(self, session=None): + """Clear status messages from current session. + + :param session: odoo http session. + By default is taken from the current request. + """ + session = session or http.request.session + if session: + session["status_message"] = [] + if hasattr(session, "touch"): + session.touch() diff --git a/cms_status_message/tests/test_message.py b/cms_status_message/tests/test_message.py index dd7e12a1..7ee65989 100644 --- a/cms_status_message/tests/test_message.py +++ b/cms_status_message/tests/test_message.py @@ -30,6 +30,10 @@ def get_status_message(self, **kw): with self.mock_request(): return self.env["ir.http"].get_status_message(**kw) + def clear_status_messages(self, **kw): + with self.mock_request(): + return self.env["ir.http"].clear_status_messages(**kw) + @contextmanager def mock_request(self): request = MockRequest(self.env) @@ -69,6 +73,14 @@ def test_message_add_message_extra_args(self): self.assertEqual(msg["dismissible"], False) self.assertEqual(msg["msg"], "you cannot remove me!") + def test_message_clear_messages(self): + # Clear all existing messages + self.add_status_message("well done!") + self.add_status_message("once again!") + self.clear_status_messages() + msgs = self.get_status_message() + self.assertEqual(msgs, []) + def _render(self): return self.env["ir.qweb"]._render("cms_status_message.status_message", {})