[IMP] attachment_zipped_download: provide ir.attachment.action_download mixin

This helps to download multiple attachments from any models.
This commit is contained in:
Pierre Verkest
2023-04-28 16:13:20 +02:00
parent 1ab97f3e3f
commit 420ae570e6
14 changed files with 391 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
# Copyright 2023 Foodles (https://www.foodles.com/)
# @author Pierre Verkest <pierreverkest84@gmail.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import _, models
class IrAttachmentActionDownloadMixin(models.AbstractModel):
_name = "ir.attachment.action_download"
_description = """
Mixin to help download attachments linked to record(s).
"""
def _get_downloadable_attachments(self):
"""Give a chance to easily overwrite this method
on sub modules to limit restict attachement able to downlaods
In some case we probably want the user download some specific
document that are probably related to the current model
By default return all attachment link the the record.
"""
return self.env["ir.attachment"].search(
[("res_model", "=", self._name), ("res_id", "in", self.ids)]
)
def action_download_attachments(self):
"""Return action to:
* emit a warning message if no attachment found
* download a file if only 1 file found
* zip and download the list of attachment returns by `_get_downloadable_attachments`
"""
attachments = self._get_downloadable_attachments()
if not attachments:
title = _("No attachment!")
message = _("There is no document found to download.")
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"type": "warning",
"title": title,
"message": message,
"sticky": True,
},
}
if len(attachments) == 1:
return {
"target": "self",
"type": "ir.actions.act_url",
"url": "/web/content/%s?download=1" % attachments.id,
}
else:
return attachments.action_attachments_download()