diff --git a/report_keep_latest_attachment/README.rst b/report_keep_latest_attachment/README.rst new file mode 100644 index 0000000000..335ceb172f --- /dev/null +++ b/report_keep_latest_attachment/README.rst @@ -0,0 +1,97 @@ +============================= +Report Keep Latest Attachment +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8bf0d41eac285579cbab02964f0931e9605a4f01de81c55988c45265aad5ba33 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github + :target: https://github.com/OCA/reporting-engine/tree/14.0/report_keep_latest_attachment + :alt: OCA/reporting-engine +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/reporting-engine-14-0/reporting-engine-14-0-report_keep_latest_attachment + :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/reporting-engine&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module restricts pdf reports to keep only the latest attachment of the +report, by only storing the latest copy of it. Basically it removes duplicates +where applicable. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +1. Go to *Technical -> Actions -> Reports* to the target report record (i.e ir.actions.report) +2. Check the *keep_latest_attachment* option under *Advanced Properties*. + +Usage +===== + +1. Go to *Technical -> Actions -> Reports* to the target report record (i.e ir.actions.report) +2. Check the *keep_latest_attachment* option under *Advanced Properties* +3. Generate attachments for selected report. + +e.g Under *Sales -> Orders -> Quotations* records; send a draft order mail +attachment and check the attachments if duplicate attachment of the report +are saved and if only the latest remained. + + +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 +~~~~~~~ + +* Therp BV + +Contributors +~~~~~~~~~~~~ + +* Kevin Kamau +* Tom Blauwendraat + +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/reporting-engine `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/report_keep_latest_attachment/__init__.py b/report_keep_latest_attachment/__init__.py new file mode 100644 index 0000000000..7d768b54b0 --- /dev/null +++ b/report_keep_latest_attachment/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from . import models diff --git a/report_keep_latest_attachment/__manifest__.py b/report_keep_latest_attachment/__manifest__.py new file mode 100644 index 0000000000..8176c689a3 --- /dev/null +++ b/report_keep_latest_attachment/__manifest__.py @@ -0,0 +1,16 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Report Keep Latest Attachment", + "version": "14.0.1.0.0", + "summary": "Keeps the latest attachment for a give report", + "author": "Therp BV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/reporting-engine", + "license": "LGPL-3", + "category": "Reporting", + "depends": [ + "base", + ], + "data": ["views/ir_actions_report.xml"], + "demo": ["demo/report_keep_latest_attachment_demo.xml"], +} diff --git a/report_keep_latest_attachment/demo/report_keep_latest_attachment_demo.xml b/report_keep_latest_attachment/demo/report_keep_latest_attachment_demo.xml new file mode 100644 index 0000000000..40bc214247 --- /dev/null +++ b/report_keep_latest_attachment/demo/report_keep_latest_attachment_demo.xml @@ -0,0 +1,41 @@ + + + + + + Test PDF Latest Attachment + res.partner + qweb-pdf + report_keep_latest_attachment.report_keep_latest_attachment_demo + '%s.pdf' % object.name or '' + True + + report + + + + diff --git a/report_keep_latest_attachment/models/__init__.py b/report_keep_latest_attachment/models/__init__.py new file mode 100644 index 0000000000..5037b909e1 --- /dev/null +++ b/report_keep_latest_attachment/models/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from . import ir_actions_report diff --git a/report_keep_latest_attachment/models/ir_actions_report.py b/report_keep_latest_attachment/models/ir_actions_report.py new file mode 100644 index 0000000000..bb17e7c1c3 --- /dev/null +++ b/report_keep_latest_attachment/models/ir_actions_report.py @@ -0,0 +1,36 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import fields, models + + +class IrActionsReport(models.Model): + _inherit = "ir.actions.report" + + keep_latest_attachment = fields.Boolean() + + def _post_pdf(self, save_in_attachment, pdf_content=None, res_ids=None): + ir_attachment_obj = self.env["ir.attachment"] + ids = [] + if self.keep_latest_attachment: + if res_ids: + ids = res_ids + if save_in_attachment: + ids = list(save_in_attachment.keys()) + if ids: + record = self.env[self.model_id.model].browse(ids) + domain = [ + ("res_model", "=", record._name), + ("res_id", "=", str(record.id)), + ("res_name", "ilike", "%{}%".format(record.name)), + ("name", "ilike", "%.pdf"), + ] + attachments = ir_attachment_obj.search(domain, order="create_date desc") + + if len(attachments) > 1: + attachments_to_delete = attachments[1:] + if attachments_to_delete: + attachments_to_delete.unlink() + + return super(IrActionsReport, self)._post_pdf( + save_in_attachment, pdf_content, res_ids + ) diff --git a/report_keep_latest_attachment/readme/CONFIGURE.rst b/report_keep_latest_attachment/readme/CONFIGURE.rst new file mode 100644 index 0000000000..c14a60f854 --- /dev/null +++ b/report_keep_latest_attachment/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +1. Go to *Technical -> Actions -> Reports* to the target report record (i.e ir.actions.report) +2. Check the *keep_latest_attachment* option under *Advanced Properties*. diff --git a/report_keep_latest_attachment/readme/CONTRIBUTORS.rst b/report_keep_latest_attachment/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..ae74ad1cfe --- /dev/null +++ b/report_keep_latest_attachment/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Kevin Kamau +* Tom Blauwendraat diff --git a/report_keep_latest_attachment/readme/DESCRIPTION.rst b/report_keep_latest_attachment/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..da610ee737 --- /dev/null +++ b/report_keep_latest_attachment/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module restricts pdf reports to keep only the latest attachment of the +report, by only storing the latest copy of it. Basically it removes duplicates +where applicable. diff --git a/report_keep_latest_attachment/readme/USAGE.rst b/report_keep_latest_attachment/readme/USAGE.rst new file mode 100644 index 0000000000..2ed0c28551 --- /dev/null +++ b/report_keep_latest_attachment/readme/USAGE.rst @@ -0,0 +1,8 @@ +1. Go to *Technical -> Actions -> Reports* to the target report record (i.e ir.actions.report) +2. Check the *keep_latest_attachment* option under *Advanced Properties* +3. Generate attachments for selected report. + +e.g Under *Sales -> Orders -> Quotations* records; send a draft order mail +attachment and check the attachments if duplicate attachment of the report +are saved and if only the latest remained. + diff --git a/report_keep_latest_attachment/static/description/icon.png b/report_keep_latest_attachment/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/report_keep_latest_attachment/static/description/icon.png differ diff --git a/report_keep_latest_attachment/static/description/index.html b/report_keep_latest_attachment/static/description/index.html new file mode 100644 index 0000000000..c1a4d812ad --- /dev/null +++ b/report_keep_latest_attachment/static/description/index.html @@ -0,0 +1,444 @@ + + + + + + +Report Keep Latest Attachment + + + +
+

Report Keep Latest Attachment

+ + +

Beta License: LGPL-3 OCA/reporting-engine Translate me on Weblate Try me on Runboat

+

This module restricts pdf reports to keep only the latest attachment of the +report, by only storing the latest copy of it. Basically it removes duplicates +where applicable.

+

Table of contents

+ +
+

Configuration

+
    +
  1. Go to Technical -> Actions -> Reports to the target report record (i.e ir.actions.report)
  2. +
  3. Check the keep_latest_attachment option under Advanced Properties.
  4. +
+
+
+

Usage

+
    +
  1. Go to Technical -> Actions -> Reports to the target report record (i.e ir.actions.report)
  2. +
  3. Check the keep_latest_attachment option under Advanced Properties
  4. +
  5. Generate attachments for selected report.
  6. +
+

e.g Under Sales -> Orders -> Quotations records; send a draft order mail +attachment and check the attachments if duplicate attachment of the report +are saved and if only the latest remained.

+
+
+

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

+
    +
  • Therp BV
  • +
+
+
+

Contributors

+ +
+
+

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/reporting-engine project on GitHub.

+

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

+
+
+
+ + diff --git a/report_keep_latest_attachment/tests/__init__.py b/report_keep_latest_attachment/tests/__init__.py new file mode 100644 index 0000000000..dddce04f80 --- /dev/null +++ b/report_keep_latest_attachment/tests/__init__.py @@ -0,0 +1,3 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from . import test_report_keep_latest_attachment diff --git a/report_keep_latest_attachment/tests/test_report_keep_latest_attachment.py b/report_keep_latest_attachment/tests/test_report_keep_latest_attachment.py new file mode 100644 index 0000000000..0cc3061bf8 --- /dev/null +++ b/report_keep_latest_attachment/tests/test_report_keep_latest_attachment.py @@ -0,0 +1,59 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo.tests.common import HttpCase + + +class TestReportKeepLatestAttachment(HttpCase): + def setUp(self): + super(TestReportKeepLatestAttachment, self).setUp() + + self.partner = self.env["res.partner"].create({"name": "Test partner"}) + self.report = self.env.ref( + "report_keep_latest_attachment.action_keep_latest_attachment_report" + ).with_context(force_report_rendering=True) + + def test_report_keep_latest_attachment(self): + IrAttachment = self.env["ir.attachment"] + domain = [ + ("res_model", "=", self.partner._name), + ("res_id", "=", str(self.partner.id)), + ("name", "ilike", "%{}%".format(self.partner.name)), + ] + + # First we check if field keep_latest_attachment is set to False + self.assertFalse(self.report.keep_latest_attachment) + + # Attachments should not exist + num_attachments = IrAttachment.search_count(domain) + self.assertFalse(num_attachments) + + # Generate first report attachment + self.report._render_qweb_pdf(self.partner.ids) + num_attachments = IrAttachment.search(domain) + self.assertTrue(num_attachments) + self.assertEqual(len(num_attachments), 1) + + # Duplicate Attachment, If using send_mail; this will be attached if + # report_template field is set on mail_template. + + IrAttachment.create( + { + "name": "%s.pdf" % self.partner.name, + "type": "binary", + "datas": num_attachments.datas, + "res_model": num_attachments.res_model, + "res_id": num_attachments.res_id, + } + ) + num_attachments = IrAttachment.search_count(domain) + self.assertTrue(num_attachments) + self.assertEqual(num_attachments, 2) + + # Now we set report to keep only latest generate attachment report + self.report.keep_latest_attachment = True + self.report._render_qweb_pdf(self.partner.ids) + num_attachments = IrAttachment.search_count(domain) + self.assertTrue(num_attachments) + + # should be only one attachment; the latest attachment + self.assertEqual(num_attachments, 1) diff --git a/report_keep_latest_attachment/views/ir_actions_report.xml b/report_keep_latest_attachment/views/ir_actions_report.xml new file mode 100644 index 0000000000..e6a3e7e0e8 --- /dev/null +++ b/report_keep_latest_attachment/views/ir_actions_report.xml @@ -0,0 +1,16 @@ + + + + ir.actions.report + ir.actions.report + + + + + + + + diff --git a/report_qweb_signer/tests/test_report_qweb_signer.py b/report_qweb_signer/tests/test_report_qweb_signer.py index a7acc58877..d386a3fa0b 100644 --- a/report_qweb_signer/tests/test_report_qweb_signer.py +++ b/report_qweb_signer/tests/test_report_qweb_signer.py @@ -9,7 +9,7 @@ def setUp(self): super(TestReportQwebSigner, self).setUp() self.partner = self.env["res.partner"].create({"name": "Test partner"}) self.report = self.env.ref( - "report_qweb_signer.partner_demo_report" + "report_keep_latest_attachment.report_keep_latest_attachment_demo" ).with_context(force_report_rendering=True) def test_report_qweb_signer(self): diff --git a/setup/report_keep_latest_attachment/odoo/addons/report_keep_latest_attachment b/setup/report_keep_latest_attachment/odoo/addons/report_keep_latest_attachment new file mode 120000 index 0000000000..cb64fa4206 --- /dev/null +++ b/setup/report_keep_latest_attachment/odoo/addons/report_keep_latest_attachment @@ -0,0 +1 @@ +../../../../report_keep_latest_attachment \ No newline at end of file diff --git a/setup/report_keep_latest_attachment/setup.py b/setup/report_keep_latest_attachment/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/report_keep_latest_attachment/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)