Skip to content

Commit

Permalink
cms_status_message: add method to clear status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
JuMiSanAr committed Jan 22, 2024
1 parent 631964e commit 9554255
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cms_form/models/cms_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 238 in cms_form/models/cms_form.py

View check run for this annotation

Codecov / codecov/patch

cms_form/models/cms_form.py#L238

Added line #L238 was not covered by tests
13 changes: 13 additions & 0 deletions cms_status_message/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 120 in cms_status_message/models/ir_http.py

View check run for this annotation

Codecov / codecov/patch

cms_status_message/models/ir_http.py#L120

Added line #L120 was not covered by tests
12 changes: 12 additions & 0 deletions cms_status_message/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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", {})

Expand Down

0 comments on commit 9554255

Please sign in to comment.