From 4a14cb98cb8e73d1c1eab1c3097eda2b9b15b722 Mon Sep 17 00:00:00 2001 From: Anmol Garg Date: Fri, 4 Sep 2026 12:34:55 +0530 Subject: [PATCH 1/2] [ADD] brand_qweb_pdf_watermark Added Fixes fixed test cases and pre commit [REF] brand_qweb_pdf_watermark: inherit _get_watermark hook directly [REF] brand_qweb_pdf_watermark: address review feedback --- brand_qweb_pdf_watermark/README.rst | 89 ++++ brand_qweb_pdf_watermark/__init__.py | 1 + brand_qweb_pdf_watermark/__manifest__.py | 20 + brand_qweb_pdf_watermark/models/__init__.py | 2 + brand_qweb_pdf_watermark/models/report.py | 35 ++ brand_qweb_pdf_watermark/models/res_brand.py | 13 + brand_qweb_pdf_watermark/pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 1 + .../readme/DESCRIPTION.md | 3 + brand_qweb_pdf_watermark/readme/USAGE.md | 7 + .../static/description/index.html | 437 ++++++++++++++++++ brand_qweb_pdf_watermark/tests/__init__.py | 4 + .../tests/test_brand_qweb_pdf_watermark.py | 226 +++++++++ .../views/res_brand_views.xml | 17 + 14 files changed, 858 insertions(+) create mode 100644 brand_qweb_pdf_watermark/README.rst create mode 100644 brand_qweb_pdf_watermark/__init__.py create mode 100644 brand_qweb_pdf_watermark/__manifest__.py create mode 100644 brand_qweb_pdf_watermark/models/__init__.py create mode 100644 brand_qweb_pdf_watermark/models/report.py create mode 100644 brand_qweb_pdf_watermark/models/res_brand.py create mode 100644 brand_qweb_pdf_watermark/pyproject.toml create mode 100644 brand_qweb_pdf_watermark/readme/CONTRIBUTORS.md create mode 100644 brand_qweb_pdf_watermark/readme/DESCRIPTION.md create mode 100644 brand_qweb_pdf_watermark/readme/USAGE.md create mode 100644 brand_qweb_pdf_watermark/static/description/index.html create mode 100644 brand_qweb_pdf_watermark/tests/__init__.py create mode 100644 brand_qweb_pdf_watermark/tests/test_brand_qweb_pdf_watermark.py create mode 100644 brand_qweb_pdf_watermark/views/res_brand_views.xml diff --git a/brand_qweb_pdf_watermark/README.rst b/brand_qweb_pdf_watermark/README.rst new file mode 100644 index 000000000..e1f1feb9e --- /dev/null +++ b/brand_qweb_pdf_watermark/README.rst @@ -0,0 +1,89 @@ +======================== +Brand QWeb PDF Watermark +======================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:6438f9efa9332e9b70beac999d1a0f63146d0673ebed65d8cfdef1e7f963bfb5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/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%2Fbrand-lightgray.png?logo=github + :target: https://github.com/OCA/brand/tree/18.0/brand_qweb_pdf_watermark + :alt: OCA/brand +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/brand-18-0/brand-18-0-brand_qweb_pdf_watermark + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/brand&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows you to define a PDF or image watermark on each Brand, +which will be automatically applied to QWeb PDF reports printed for +documents belonging to that brand. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module: + +1. Go to *Settings > Users & Companies > Brands*. +2. Open or create a brand. +3. In the *PDF Watermark* field, upload a PDF file. +4. When printing any QWeb PDF report for documents associated with this + brand, the watermark will be applied to the output PDF. + +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* CIT-Services + +Contributors +------------ + +- `CIT-Services `__ + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/brand `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/brand_qweb_pdf_watermark/__init__.py b/brand_qweb_pdf_watermark/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/brand_qweb_pdf_watermark/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/brand_qweb_pdf_watermark/__manifest__.py b/brand_qweb_pdf_watermark/__manifest__.py new file mode 100644 index 000000000..d4cf96fbf --- /dev/null +++ b/brand_qweb_pdf_watermark/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2026 CIT-Services +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Brand QWeb PDF Watermark", + "summary": "Add watermark to Brand reports", + "version": "18.0.1.0.0", + "category": "Technical", + "website": "https://github.com/OCA/brand", + "author": "CIT-Services, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "brand", + "report_qweb_pdf_watermark", + ], + "data": [ + "views/res_brand_views.xml", + ], + "installable": True, +} diff --git a/brand_qweb_pdf_watermark/models/__init__.py b/brand_qweb_pdf_watermark/models/__init__.py new file mode 100644 index 000000000..c398997a9 --- /dev/null +++ b/brand_qweb_pdf_watermark/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_brand +from . import report diff --git a/brand_qweb_pdf_watermark/models/report.py b/brand_qweb_pdf_watermark/models/report.py new file mode 100644 index 000000000..bdfea1f57 --- /dev/null +++ b/brand_qweb_pdf_watermark/models/report.py @@ -0,0 +1,35 @@ +# Copyright 2026 CIT-Services +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 + +from odoo import _, models +from odoo.exceptions import UserError + + +class Report(models.Model): + _inherit = "ir.actions.report" + + def _get_watermark(self, report_ref, docids=False): + report_sudo = super()._get_report(report_ref) + if "brand_id" in self.env[report_sudo.model]._fields: + docs = self.env[report_sudo.model].browse(docids) + brands = docs.mapped("brand_id") + if len(brands) > 1: + brand_names = ", ".join(brands.mapped("name")) + raise UserError( + _( + "Cannot print documents belonging to " + "different brands (%s) together." + ) + % brand_names + ) + if brands and any(not doc.brand_id for doc in docs): + raise UserError( + _("Some of the documents do not have a brand linked to it.") + ) + brand = docs and docs[0].brand_id + if brand and brand.pdf_watermark: + return base64.b64decode(brand.pdf_watermark) + + return super()._get_watermark(report_ref, docids=docids) diff --git a/brand_qweb_pdf_watermark/models/res_brand.py b/brand_qweb_pdf_watermark/models/res_brand.py new file mode 100644 index 000000000..cf0edecdd --- /dev/null +++ b/brand_qweb_pdf_watermark/models/res_brand.py @@ -0,0 +1,13 @@ +# Copyright 2026 CIT-Services +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResBrand(models.Model): + _inherit = "res.brand" + + pdf_watermark = fields.Binary( + string="Watermark", + help="Upload a PDF or image file to use as a watermark for this brand.", + ) diff --git a/brand_qweb_pdf_watermark/pyproject.toml b/brand_qweb_pdf_watermark/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/brand_qweb_pdf_watermark/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/brand_qweb_pdf_watermark/readme/CONTRIBUTORS.md b/brand_qweb_pdf_watermark/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..8481c3f7b --- /dev/null +++ b/brand_qweb_pdf_watermark/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- [CIT-Services](https://cit-services.eu) diff --git a/brand_qweb_pdf_watermark/readme/DESCRIPTION.md b/brand_qweb_pdf_watermark/readme/DESCRIPTION.md new file mode 100644 index 000000000..637ce544c --- /dev/null +++ b/brand_qweb_pdf_watermark/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module allows you to define a PDF or image watermark on each Brand, which +will be automatically applied to QWeb PDF reports printed for documents +belonging to that brand. diff --git a/brand_qweb_pdf_watermark/readme/USAGE.md b/brand_qweb_pdf_watermark/readme/USAGE.md new file mode 100644 index 000000000..b12a05df6 --- /dev/null +++ b/brand_qweb_pdf_watermark/readme/USAGE.md @@ -0,0 +1,7 @@ +To use this module: + +1. Go to *Settings > Users & Companies > Brands*. +2. Open or create a brand. +3. In the *PDF Watermark* field, upload a PDF file. +4. When printing any QWeb PDF report for documents associated with this brand, + the watermark will be applied to the output PDF. diff --git a/brand_qweb_pdf_watermark/static/description/index.html b/brand_qweb_pdf_watermark/static/description/index.html new file mode 100644 index 000000000..7d7993fec --- /dev/null +++ b/brand_qweb_pdf_watermark/static/description/index.html @@ -0,0 +1,437 @@ + + + + + +Brand QWeb PDF Watermark + + + +
+

Brand QWeb PDF Watermark

+ + +

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

+

This module allows you to define a PDF or image watermark on each Brand, +which will be automatically applied to QWeb PDF reports printed for +documents belonging to that brand.

+

Table of contents

+ +
+

Usage

+

To use this module:

+
    +
  1. Go to Settings > Users & Companies > Brands.
  2. +
  3. Open or create a brand.
  4. +
  5. In the PDF Watermark field, upload a PDF file.
  6. +
  7. When printing any QWeb PDF report for documents associated with this +brand, the watermark will be applied to the output PDF.
  8. +
+
+
+

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 +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • CIT-Services
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/brand project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/brand_qweb_pdf_watermark/tests/__init__.py b/brand_qweb_pdf_watermark/tests/__init__.py new file mode 100644 index 000000000..79387b753 --- /dev/null +++ b/brand_qweb_pdf_watermark/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2026 CIT-Services +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_brand_qweb_pdf_watermark diff --git a/brand_qweb_pdf_watermark/tests/test_brand_qweb_pdf_watermark.py b/brand_qweb_pdf_watermark/tests/test_brand_qweb_pdf_watermark.py new file mode 100644 index 000000000..7d3124b14 --- /dev/null +++ b/brand_qweb_pdf_watermark/tests/test_brand_qweb_pdf_watermark.py @@ -0,0 +1,226 @@ +# Copyright 2026 CIT-Services +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 +from unittest.mock import MagicMock, patch + +from odoo.exceptions import UserError +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged("post_install", "-at_install") +class TestBrandQwebPdfWatermark(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env["res.company"]._fields["brand_id"] = MagicMock() + cls.addClassCleanup(cls.env["res.company"]._fields.pop, "brand_id", None) + + cls.raw_watermark_1 = b"%PDF-1.4 Mock Watermark 1 Content" + cls.raw_watermark_2 = b"%PDF-1.4 Mock Watermark 2 Content" + cls.b64_watermark_1 = base64.b64encode(cls.raw_watermark_1) + cls.b64_watermark_2 = base64.b64encode(cls.raw_watermark_2) + + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.brand_with_watermark = cls.env["res.brand"].create( + { + "name": "Brand Watermark 1", + "pdf_watermark": cls.b64_watermark_1, + } + ) + cls.brand_with_watermark_2 = cls.env["res.brand"].create( + { + "name": "Brand Watermark 2", + "pdf_watermark": cls.b64_watermark_2, + } + ) + cls.brand_no_watermark = cls.env["res.brand"].create( + { + "name": "Brand No Watermark", + } + ) + cls.report = cls.env["ir.actions.report"].create( + { + "name": "Test Action Report", + "model": "res.company", + "report_name": "test_action_report", + } + ) + + def _create_mock_docs(self, doc_list): + mock_docs = MagicMock() + mock_docs.__iter__.return_value = doc_list + mock_docs.__getitem__.side_effect = doc_list.__getitem__ + mock_docs.__bool__.return_value = bool(doc_list) + mock_docs.__len__.return_value = len(doc_list) + brands = self.env["res.brand"] + for d in doc_list: + brand_id = getattr(d, "brand_id", False) + if brand_id: + brands |= brand_id + mock_docs.mapped.return_value = brands + return mock_docs + + def test_watermark_field_on_brand(self): + """Test that pdf_watermark is correctly stored and retrieved on res.brand.""" + self.assertEqual(self.brand_with_watermark.pdf_watermark, self.b64_watermark_1) + self.assertFalse(self.brand_no_watermark.pdf_watermark) + + def test_get_watermark_no_docids(self): + """When docids is False or empty, fallback cleanly to None.""" + watermark = self.env["ir.actions.report"]._get_watermark( + self.report, docids=False + ) + self.assertIsNone(watermark) + + def test_get_watermark_model_without_brand_id(self): + """When model has no brand_id in _fields, fallback cleanly to None.""" + report_no_brand = self.env["ir.actions.report"].create( + { + "name": "Report Without Brand Model", + "model": "res.users", + "report_name": "test_report_without_brand_model", + } + ) + watermark = self.env["ir.actions.report"]._get_watermark( + report_no_brand, docids=[self.env.user.id] + ) + self.assertIsNone(watermark) + + def test_get_watermark_single_brand_with_watermark(self): + """When documents belong to a single brand, return decoded watermark.""" + mock_doc = MagicMock() + mock_doc.brand_id = self.brand_with_watermark + mock_docs = self._create_mock_docs([mock_doc]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + self.report, docids=[1] + ) + self.assertEqual(watermark, self.raw_watermark_1) + + def test_get_watermark_multiple_docs_same_brand(self): + """When multiple docs belong to the same brand, return decoded watermark.""" + mock_doc1 = MagicMock() + mock_doc1.brand_id = self.brand_with_watermark + mock_doc2 = MagicMock() + mock_doc2.brand_id = self.brand_with_watermark + mock_docs = self._create_mock_docs([mock_doc1, mock_doc2]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + self.report, docids=[1, 2] + ) + self.assertEqual(watermark, self.raw_watermark_1) + + def test_get_watermark_single_brand_without_watermark(self): + """When document brand has no watermark, fallback cleanly to None.""" + mock_doc = MagicMock() + mock_doc.brand_id = self.brand_no_watermark + mock_docs = self._create_mock_docs([mock_doc]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + self.report, docids=[1] + ) + self.assertIsNone(watermark) + + def test_get_watermark_doc_without_brand(self): + """When document has False brand_id, fallback cleanly to None.""" + mock_doc = MagicMock() + mock_doc.brand_id = False + mock_docs = self._create_mock_docs([mock_doc]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + self.report, docids=[1] + ) + self.assertIsNone(watermark) + + def test_get_watermark_multiple_brands_raises_user_error(self): + """When documents belong to different brands, raise UserError.""" + mock_doc1 = MagicMock() + mock_doc1.brand_id = self.brand_with_watermark + mock_doc2 = MagicMock() + mock_doc2.brand_id = self.brand_with_watermark_2 + mock_docs = self._create_mock_docs([mock_doc1, mock_doc2]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + with self.assertRaises(UserError) as cm: + self.env["ir.actions.report"]._get_watermark(self.report, docids=[1, 2]) + self.assertIn( + "Cannot print documents belonging to different brands", + str(cm.exception), + ) + + def test_get_watermark_mixed_brand_and_no_brand_raises_user_error(self): + """When documents have a brand and another has no brand, raise UserError.""" + mock_doc1 = MagicMock() + mock_doc1.brand_id = self.brand_with_watermark + mock_doc2 = MagicMock() + mock_doc2.brand_id = False + mock_docs = self._create_mock_docs([mock_doc1, mock_doc2]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + with self.assertRaises(UserError) as cm: + self.env["ir.actions.report"]._get_watermark(self.report, docids=[1, 2]) + self.assertIn( + "Some of the documents do not have a brand", str(cm.exception) + ) + + def test_get_watermark_brand_takes_precedence_over_report_watermark(self): + """When report has its own watermark, brand watermark takes precedence.""" + report_with_watermark = self.env["ir.actions.report"].create( + { + "name": "Report With Own Watermark", + "model": "res.company", + "report_name": "report_with_own_watermark", + "pdf_watermark": self.b64_watermark_2, + } + ) + mock_doc = MagicMock() + mock_doc.brand_id = self.brand_with_watermark + mock_docs = self._create_mock_docs([mock_doc]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + report_with_watermark, docids=[1] + ) + self.assertEqual(watermark, self.raw_watermark_1) + + def test_get_watermark_fallback_to_report_watermark(self): + """When doc brand has no watermark, fallback to report's own watermark.""" + report_with_watermark = self.env["ir.actions.report"].create( + { + "name": "Report With Own Watermark 2", + "model": "res.company", + "report_name": "report_with_own_watermark_2", + "pdf_watermark": self.b64_watermark_2, + } + ) + mock_doc = MagicMock() + mock_doc.brand_id = self.brand_no_watermark + mock_docs = self._create_mock_docs([mock_doc]) + + with patch.object( + type(self.env["res.company"]), "browse", return_value=mock_docs + ): + watermark = self.env["ir.actions.report"]._get_watermark( + report_with_watermark, docids=[1] + ) + self.assertEqual(watermark, self.raw_watermark_2) diff --git a/brand_qweb_pdf_watermark/views/res_brand_views.xml b/brand_qweb_pdf_watermark/views/res_brand_views.xml new file mode 100644 index 000000000..d12529de8 --- /dev/null +++ b/brand_qweb_pdf_watermark/views/res_brand_views.xml @@ -0,0 +1,17 @@ + + + + res.brand.view.form.inherit.watermark + res.brand + + + + + + + + From ac68545f212676bdf6dbec8edadac3a551048729 Mon Sep 17 00:00:00 2001 From: Anmol Garg Date: Mon, 26 Oct 2026 12:37:37 +0530 Subject: [PATCH 2/2] [DON'T MERGE] test-requirements.txt --- test-requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test-requirements.txt diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 000000000..a1aee3328 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +odoo-addon-report_qweb_pdf_watermark @ git+https://github.com/OCA/reporting-engine.git@refs/pull/1195/head#subdirectory=report_qweb_pdf_watermark