Skip to content

Commit 52b1c22

Browse files
committed
[FIX] document_page_approval: Avoid errors when creating a page history and send mail
Example use case: - Modify the Odoo Features category, check Require approval, and set Approver group > Administration / Access Rights - Modify the Marc Demo user and define Document Knowledge > Editor permissions - Go to the Odoo Features category and, taking into account the ID, delete the mail_message record table with the same ID from the database - With the Marc Demo user, go to the Knowledge > Odoo Features menu and enter the Odoo 15.0 Functional Demo page (it is important to follow these specific steps) - Modify the page and save Before this change, an error could occur because the action https://github.com/OCA/knowledge/blob/0881d75a35e7fc2f814110df4878177e805e589b/document_page/views/document_page_category. xml#L83C17-L83C42 defined a default_parent_id=x (page category ID) if there was no mail_message linked to that category ID; now the value of default_parent_id is removed to avoid this specific error. TT61290
1 parent 0881d75 commit 52b1c22

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

document_page_approval/models/document_page_history.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from odoo import fields, models
66
from odoo.exceptions import UserError
7+
from odoo.tools.misc import clean_context
78
from odoo.tools.translate import _
89

910

@@ -80,7 +81,10 @@ def action_to_approve(self):
8081
[("groups_id", "in", guids), ("groups_id", "in", approver_gid.id)]
8182
)
8283
rec.message_subscribe(partner_ids=users.mapped("partner_id").ids)
83-
rec.message_post_with_source(template)
84+
# pylint: disable=W8121
85+
rec.with_context(
86+
clean_context(self.env.context)
87+
).message_post_with_source(template)
8488
else:
8589
# auto-approve if approval is not required
8690
rec.action_approve()

document_page_approval/tests/test_document_page_approval.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from odoo import Command
22
from odoo.exceptions import UserError
33
from odoo.tests import new_test_user
4+
from odoo.tools import mute_logger
45

56
from odoo.addons.base.tests.common import BaseCommon
67

@@ -53,6 +54,7 @@ def test_approval_required(self):
5354
self.assertTrue(page.has_changes_pending_approval)
5455
self.assertEqual(len(page.history_ids), 0)
5556

57+
@mute_logger("odoo.models.unlink")
5658
def test_change_request_approve(self):
5759
"""Test that an approver can approve a change request."""
5860
page = self.page2
@@ -72,6 +74,11 @@ def test_change_request_approve(self):
7274
self.assertEqual(chreq.state, "approved")
7375
self.assertEqual(chreq.content, page.content)
7476

77+
# Remove the linked mail.message and define a specific context to simulate that
78+
# when accessing from the category smart button, there is no error when creating
79+
# the history and sending the email
80+
self.env["mail.message"].browse(page.parent_id.id).unlink()
81+
page = page.with_context(default_parent_id=page.parent_id.id)
7582
# Create new change request
7683
page.write({"content": "<p>New content</p>"})
7784
page.invalidate_model() # Recompute fields
@@ -90,6 +97,7 @@ def test_change_request_auto_approve(self):
9097
page.write({"content": "<p>New content</p>"})
9198
self.assertEqual(page.content, "<p>New content</p>")
9299

100+
@mute_logger("odoo.models.unlink")
93101
def test_change_request_from_scratch(self):
94102
"""Test a full change request lifecycle from draft to approval."""
95103
page = self.page2
@@ -164,6 +172,7 @@ def test_can_user_approve_this_page(self):
164172
self.page2.with_user(self.user2).can_user_approve_this_page(self.user2)
165173
)
166174

175+
@mute_logger("odoo.models.unlink")
167176
def test_pending_approval_detection(self):
168177
"""Ensure the system detects pending approval changes"""
169178
# Reset page2 by removing previous history

0 commit comments

Comments
 (0)