diff --git a/document_page_approval/README.rst b/document_page_approval/README.rst index bad71157a8d..b78aa365109 100644 --- a/document_page_approval/README.rst +++ b/document_page_approval/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ====================== Document Page Approval ====================== @@ -17,7 +13,7 @@ Document Page Approval .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github @@ -106,6 +102,10 @@ Contributors - Bhavesh Heliconia +- `NuoBiT Solutions SL `__: + + - Deniz Gallo dgallo@nuobit.com + Maintainers ----------- diff --git a/document_page_approval/models/document_page_history.py b/document_page_approval/models/document_page_history.py index 62499fe1152..854569042fe 100644 --- a/document_page_approval/models/document_page_history.py +++ b/document_page_approval/models/document_page_history.py @@ -1,4 +1,5 @@ # Copyright (C) 2013 Savoir-faire Linux (). +# Copyright 2026 NuoBiT Solutions SL - Deniz Gallo # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). @@ -130,6 +131,18 @@ 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( + _("Can't cancel change requests in '%s' state.") % rec.state + ) + if not (rec.am_i_owner or rec.am_i_approver): + raise UserError( + _( + "You are not authorized to do this.\r\n" + "Only owners or approvers can cancel Change Requests." + ) + ) self.write({"state": "cancelled"}) for rec in self: rec.message_post( diff --git a/document_page_approval/readme/CONTRIBUTORS.md b/document_page_approval/readme/CONTRIBUTORS.md index 6cc0b9a99cf..30d03d4677c 100644 --- a/document_page_approval/readme/CONTRIBUTORS.md +++ b/document_page_approval/readme/CONTRIBUTORS.md @@ -18,3 +18,5 @@ - Fernando La Chica \<\> - [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) - Bhavesh Heliconia +- [NuoBiT Solutions SL](https://www.nuobit.com): + - Deniz Gallo diff --git a/document_page_approval/static/description/index.html b/document_page_approval/static/description/index.html index 97d65b4bddc..1ac9f391456 100644 --- a/document_page_approval/static/description/index.html +++ b/document_page_approval/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Document Page Approval -
+
+

Document Page Approval

- - -Odoo Community Association - -
-

Document Page Approval

-

Beta License: AGPL-3 OCA/knowledge Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/knowledge Translate me on Weblate Try me on Runboat

This module adds a workflow to approve page modifications and show the approved version by default.

Table of contents

@@ -392,7 +387,7 @@

Document Page Approval

-

Configuration

+

Configuration

To configure this module, you need to:

  1. Set a valid email address on the company settings.
  2. @@ -402,7 +397,7 @@

    Configuration

-

Usage

+

Usage

To use this module, you need to:

  1. Go to Knowledge > Pages
  2. @@ -414,7 +409,7 @@

    Usage

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -422,15 +417,15 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Savoir-faire Linux
-

Contributors

+

Contributors

+
  • NuoBiT Solutions SL:

    + +
  • -

    Maintainers

    +

    Maintainers

    This module is maintained by the OCA.

    Odoo Community Association @@ -476,6 +476,5 @@

    Maintainers

    -
    diff --git a/document_page_approval/tests/__init__.py b/document_page_approval/tests/__init__.py index 6f4fbfc92fc..fd8bd358480 100644 --- a/document_page_approval/tests/__init__.py +++ b/document_page_approval/tests/__init__.py @@ -1 +1,2 @@ from . import test_document_page_approval +from . import test_document_page_history diff --git a/document_page_approval/tests/test_document_page_history.py b/document_page_approval/tests/test_document_page_history.py new file mode 100644 index 00000000000..ec6c7234101 --- /dev/null +++ b/document_page_approval/tests/test_document_page_history.py @@ -0,0 +1,203 @@ +# Copyright 2026 NuoBiT Solutions SL - Deniz Gallo +# 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({"groups_id": [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": "

    New content

    ", + } + ) + + 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.\r\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.\r\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) diff --git a/document_page_approval/views/document_page_approval.xml b/document_page_approval/views/document_page_approval.xml index 87cdf3c4dc4..40887c8709d 100644 --- a/document_page_approval/views/document_page_approval.xml +++ b/document_page_approval/views/document_page_approval.xml @@ -1,4 +1,6 @@ + @@ -44,7 +46,15 @@ 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" + /> + +