Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: [14.0][ADD] keep latest report record attachment #817

Draft
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Draft
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
97 changes: 97 additions & 0 deletions report_keep_latest_attachment/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/reporting-engine/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 <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_keep_latest_attachment%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Therp BV

Contributors
~~~~~~~~~~~~

* Kevin Kamau <kkahumba@therp.nl>
* Tom Blauwendraat <tblauwendraat@therp.nl>

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 <https://github.com/OCA/reporting-engine/tree/14.0/report_keep_latest_attachment>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions report_keep_latest_attachment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import models
16 changes: 16 additions & 0 deletions report_keep_latest_attachment/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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"],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="report_keep_latest_attachment_demo_document">
<t t-call="web.external_layout">
<div class="page">
<div class="row">
<div class="col-md-12">
<span>
This is a sample report for testing keeping of the
latest PDF Report.
</span>
</div>
</div>
</div>
</t>
</template>
<template id="report_keep_latest_attachment_demo">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t
t-call="report_keep_latest_attachment.report_keep_latest_attachment_demo_document"
t-lang="o.lang"
/>
</t>
</t>
</template>
<record id="action_keep_latest_attachment_report" model="ir.actions.report">
<field name="name">Test PDF Latest Attachment</field>
<field name="model">res.partner</field>
<field name="report_type">qweb-pdf</field>
<field
name="report_name"
>report_keep_latest_attachment.report_keep_latest_attachment_demo</field>
<field name="attachment">'%s.pdf' % object.name or ''</field>
<field name="attachment_use">True</field>
<field name="binding_model_id" ref="base.model_res_partner" />
<field name="binding_type">report</field>
</record>


</odoo>
3 changes: 3 additions & 0 deletions report_keep_latest_attachment/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import ir_actions_report
36 changes: 36 additions & 0 deletions report_keep_latest_attachment/models/ir_actions_report.py
Original file line number Diff line number Diff line change
@@ -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)),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are running into a failure here:

2024-02-17 04:32:39,639 3479 INFO odoo odoo.addons.base.models.ir_actions_report: The PDF report has been generated for model: res.partner, records [41]. 
2024-02-17 04:32:39,641 3479 INFO odoo odoo.addons.base.models.ir_actions_report: The PDF document Test partner.pdf is now saved in the database 
2024-02-17 04:32:39,654 3479 INFO odoo odoo.addons.base.models.ir_actions_report: The PDF report has been generated from attachments. 
2024-02-17 04:32:39,655 3479 ERROR odoo odoo.osv.expression: Non-stored field ir.attachment.res_name cannot be searched. 

("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
)
2 changes: 2 additions & 0 deletions report_keep_latest_attachment/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -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*.
2 changes: 2 additions & 0 deletions report_keep_latest_attachment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Kevin Kamau <kkahumba@therp.nl>
* Tom Blauwendraat <tblauwendraat@therp.nl>
3 changes: 3 additions & 0 deletions report_keep_latest_attachment/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions report_keep_latest_attachment/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading