Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions document_page_approval/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ Contributors

- Bhavesh Heliconia

- `NuoBiT Solutions SL <https://www.nuobit.com>`__:

- Deniz Gallo dgallo@nuobit.com

Maintainers
-----------

Expand Down
16 changes: 16 additions & 0 deletions document_page_approval/models/document_page_history.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
# Copyright 2026 NuoBiT Solutions SL - Deniz Gallo <dgallo@nuobit.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


Expand Down Expand Up @@ -133,6 +134,21 @@ def action_approve(self):

def action_cancel(self):
"""Set a change request as cancelled."""
for rec in self:
if rec.state not in ["draft", "to approve"]:
raise UserError(
self.env._(
"Can't cancel change requests in '%(state)s' state.",
state=rec.state,
)
)
if not (rec.am_i_owner or rec.am_i_approver):
raise UserError(
self.env._(
"You are not authorized to do this.\n"
"Only owners or approvers can cancel Change Requests."
)
)
self.write({"state": "cancelled"})
for rec in self:
rec.message_post(
Expand Down
2 changes: 2 additions & 0 deletions document_page_approval/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
- Fernando La Chica \<<fernando.lachica@guadaltech.es>\>
- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io)
- Bhavesh Heliconia
- [NuoBiT Solutions SL](https://www.nuobit.com):
- Deniz Gallo <dgallo@nuobit.com>
7 changes: 6 additions & 1 deletion document_page_approval/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Document Page Approval</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -460,6 +460,11 @@ <h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
<li>Bhavesh Heliconia</li>
</ul>
</li>
<li><p class="first"><a class="reference external" href="https://www.nuobit.com">NuoBiT Solutions SL</a>:</p>
<ul class="simple">
<li>Deniz Gallo <a class="reference external" href="mailto:dgallo&#64;nuobit.com">dgallo&#64;nuobit.com</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
1 change: 1 addition & 0 deletions document_page_approval/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_document_page_approval
from . import test_document_page_history
228 changes: 228 additions & 0 deletions document_page_approval/tests/test_document_page_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# Copyright 2026 NuoBiT Solutions SL - Deniz Gallo <dgallo@nuobit.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from lxml import etree

from odoo import Command
from odoo.exceptions import UserError
from odoo.tests import new_test_user
from odoo.tools.safe_eval import safe_eval

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


class TestDocumentPageHistory(BaseCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.history_obj = cls.env["document.page.history"]
cls.page_approvers = cls.env["res.groups"].create({"name": "Page approvers"})
cls.owner = new_test_user(
cls.env,
login="test_owner",
groups="document_knowledge.group_document_user,"
"document_page.group_document_editor",
name="Test owner",
)
cls.approver = new_test_user(
cls.env,
login="test_approver",
groups="document_page_approval.group_document_approver_user",
name="Test approver",
)
cls.approver.write({"group_ids": [Command.link(cls.page_approvers.id)]})
cls.other_approver = new_test_user(
cls.env,
login="test_other_approver",
groups="document_page_approval.group_document_approver_user",
name="Test other approver",
)
cls.manager = new_test_user(
cls.env,
login="test_manager",
groups="document_page.group_document_manager",
name="Test manager",
)
category = cls.env["document.page"].create(
{
"name": "This category requires approval",
"type": "category",
"approval_required": True,
"approver_gid": cls.page_approvers.id,
}
)
cls.page = cls.env["document.page"].create(
{"name": "This page requires approval", "parent_id": category.id}
)

def _create_change_request(self, user):
return self.history_obj.with_user(user).create(
{
"page_id": self.page.id,
"summary": "Changed something",
"content": "<p>New content</p>",
}
)

def _with_user(self, record, user):
# a request of that user starts with an empty cache
self.env.invalidate_all()
return record.with_user(user)

def _visible_buttons(self, record, string):
"""Methods of the buttons labelled `string` that the record's user sees."""
arch = record.get_views([(False, "form")])["views"]["form"]["arch"]
values = {
"am_i_owner": record.am_i_owner,
"am_i_approver": record.am_i_approver,
"is_approval_required": record.is_approval_required,
"state": record.state,
}
names = []
for button in etree.fromstring(arch).iter("button"):
if button.get("string") == string and not safe_eval(
button.get("invisible", "False"), values
):
names.append(button.get("name"))
return names

def _click(self, record, string):
names = self._visible_buttons(record, string)
self.assertEqual(len(names), 1, names)
return getattr(record, names[0])()

def test_manager_owner_back_to_draft_pending(self):
"""Ensure a manager sends their own pending request back to draft"""
chreq = self._create_change_request(self.manager)
chreq.action_to_approve()
self._click(chreq, "Back to draft")
self.assertEqual(chreq.state, "draft")
self.assertIn("has been cancelled by Test manager.", chreq.message_ids[0].body)

def test_manager_back_to_draft_pending(self):
"""Ensure a manager sends someone else's pending request back to draft"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
chreq = self._with_user(chreq, self.manager)
self._click(chreq, "Back to draft")
self.assertEqual(chreq.state, "draft")
self.assertIn("has been cancelled by Test manager.", chreq.message_ids[0].body)

def test_other_approver_no_back_to_draft_pending(self):
"""Ensure Back to draft is hidden from who is neither owner nor approver"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
self.assertEqual(chreq.state, "to approve")
chreq = self._with_user(chreq, self.other_approver)
self.assertEqual(self._visible_buttons(chreq, "Back to draft"), [])

def test_manager_back_to_draft_cancelled(self):
"""Ensure Back to draft still reopens a cancelled request"""
chreq = self._create_change_request(self.manager)
chreq.action_to_approve()
chreq.action_cancel()
self._click(chreq, "Back to draft")
self.assertEqual(chreq.state, "draft")

def test_other_approver_no_cancel_and_draft_pending(self):
"""Ensure who is neither owner nor approver cannot cancel and reopen"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
chreq = self._with_user(chreq, self.other_approver)
with self.assertRaises(UserError) as error:
chreq.action_cancel_and_draft()
self.assertEqual(
error.exception.args[0],
"You are not authorized to do this.\n"
"Only owners or approvers can cancel Change Requests.",
)
self.assertEqual(chreq.state, "to approve")

def test_owner_no_cancel_approved(self):
"""Ensure an approved request cannot be cancelled"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
self._with_user(chreq, self.approver).action_approve()
chreq = self._with_user(chreq, self.owner)
with self.assertRaises(UserError) as error:
chreq.action_cancel()
self.assertEqual(
error.exception.args[0],
"Can't cancel change requests in 'approved' state.",
)
self.assertEqual(chreq.state, "approved")

def test_owner_no_cancel_cancelled(self):
"""Ensure a cancelled request cannot be cancelled again"""
chreq = self._create_change_request(self.owner)
chreq.action_cancel()
with self.assertRaises(UserError) as error:
chreq.action_cancel()
self.assertEqual(
error.exception.args[0],
"Can't cancel change requests in 'cancelled' state.",
)
self.assertEqual(chreq.state, "cancelled")

def test_other_approver_no_cancel_pending(self):
"""Ensure who is neither owner nor approver cannot cancel"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
chreq = self._with_user(chreq, self.other_approver)
with self.assertRaises(UserError) as error:
chreq.action_cancel()
self.assertEqual(
error.exception.args[0],
"You are not authorized to do this.\n"
"Only owners or approvers can cancel Change Requests.",
)
self.assertEqual(chreq.state, "to approve")

def test_owner_cancel_draft(self):
"""Ensure the owner cancels their draft request"""
chreq = self._create_change_request(self.owner)
self._click(chreq, "Cancel")
self.assertEqual(chreq.state, "cancelled")
self.assertIn("has been cancelled by Test owner.", chreq.message_ids[0].body)

def test_owner_cancel_pending(self):
"""Ensure the owner cancels their pending request"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
self._click(chreq, "Cancel")
self.assertEqual(chreq.state, "cancelled")
self.assertIn("has been cancelled by Test owner.", chreq.message_ids[0].body)

def test_approver_cancel_pending(self):
"""Ensure an approver cancels a pending request"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
chreq = self._with_user(chreq, self.approver)
self._click(chreq, "Cancel")
self.assertEqual(chreq.state, "cancelled")
self.assertIn("has been cancelled by Test approver.", chreq.message_ids[0].body)

def test_other_approver_no_cancel_button_pending(self):
"""Ensure Cancel is hidden from who is neither owner nor approver"""
chreq = self._create_change_request(self.owner)
chreq.action_to_approve()
self.assertEqual(chreq.state, "to approve")
chreq = self._with_user(chreq, self.other_approver)
self.assertEqual(self._visible_buttons(chreq, "Cancel"), [])

def test_manager_owner_no_cancel_button_approved(self):
"""Ensure Cancel is hidden on an approved request"""
chreq = self._create_change_request(self.manager)
self.assertTrue(chreq.am_i_owner and chreq.am_i_approver)
chreq.action_to_approve()
chreq.action_approve()
self.assertEqual(chreq.state, "approved")
self.assertEqual(self._visible_buttons(chreq, "Cancel"), [])

def test_manager_owner_no_cancel_button_cancelled(self):
"""Ensure Cancel is hidden on a cancelled request"""
chreq = self._create_change_request(self.manager)
self.assertTrue(chreq.am_i_owner and chreq.am_i_approver)
chreq.action_cancel()
self.assertEqual(chreq.state, "cancelled")
self.assertEqual(self._visible_buttons(chreq, "Cancel"), [])
14 changes: 12 additions & 2 deletions document_page_approval/views/document_page_approval.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Copyright 2026 NuoBiT Solutions SL - Deniz Gallo <dgallo@nuobit.com>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<!-- History Form View -->
<record id="wiki_history_form_inherit" model="ir.ui.view">
Expand Down Expand Up @@ -37,14 +39,22 @@
name="action_cancel"
type="object"
string="Cancel"
invisible="(not am_i_owner or not am_i_approver) and (state not in ('draft', 'to approve'))"
invisible="(not am_i_owner and not am_i_approver) or state not in ('draft', 'to approve')"
/>
<!-- reopen if i am owner or approver -->
<button
name="action_draft"
type="object"
string="Back to draft"
invisible="(not am_i_owner or not am_i_approver) and (state not in ('cancelled', 'to approve'))"
invisible="(not am_i_owner and not am_i_approver) or state != 'cancelled'"
groups="document_page.group_document_manager"
/>
<!-- cancel & reopen, if i am owner or approver -->
<button
name="action_cancel_and_draft"
type="object"
string="Back to draft"
invisible="(not am_i_owner and not am_i_approver) or state != 'to approve'"
groups="document_page.group_document_manager"
/>
<field name="am_i_owner" invisible="1" />
Expand Down