diff --git a/attachment_zipped_download/README.rst b/attachment_zipped_download/README.rst new file mode 100644 index 00000000..58bb48c4 --- /dev/null +++ b/attachment_zipped_download/README.rst @@ -0,0 +1,84 @@ +========================== +Attachment Zipped Download +========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fknowledge-lightgray.png?logo=github + :target: https://github.com/OCA/knowledge/tree/14.0/attachment_zipped_download + :alt: OCA/knowledge +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/knowledge-14-0/knowledge-14-0-attachment_zipped_download + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/118/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows downloading multiple attachments as a zip file. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +#. Go to *Settings > Technical > Database Structure > Attachments* and select some files. +#. Go to *Actions > Download* and a zip file containing the selected files will be downloaded. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* César Fernández Domínguez + +* `Tecnativa `_: + + * Víctor Martínez + * Pedro M. Baeza + +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/knowledge `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/attachment_zipped_download/__init__.py b/attachment_zipped_download/__init__.py new file mode 100644 index 00000000..91c5580f --- /dev/null +++ b/attachment_zipped_download/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import models diff --git a/attachment_zipped_download/__manifest__.py b/attachment_zipped_download/__manifest__.py new file mode 100644 index 00000000..97b5f90f --- /dev/null +++ b/attachment_zipped_download/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2022 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Attachment Zipped Download", + "version": "14.0.1.0.0", + "category": "Tools", + "website": "https://github.com/OCA/knowledge", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["base"], + "data": [ + "views/ir_attachment_view.xml", + ], + "installable": True, +} diff --git a/attachment_zipped_download/controllers/__init__.py b/attachment_zipped_download/controllers/__init__.py new file mode 100644 index 00000000..12a7e529 --- /dev/null +++ b/attachment_zipped_download/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/attachment_zipped_download/controllers/main.py b/attachment_zipped_download/controllers/main.py new file mode 100644 index 00000000..f4b5f973 --- /dev/null +++ b/attachment_zipped_download/controllers/main.py @@ -0,0 +1,21 @@ +# Copyright 2019 César Fernández Domínguez +# Copyright 2022 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import _, http +from odoo.http import request + + +class AttachmentZippedDownloadController(http.Controller): + @http.route("/web/attachment/download_zip", type="http", auth="user") + def download_zip(self, ids=None, debug=0): + ids = [] if not ids else ids + if len(ids) == 0: + return + list_ids = map(int, ids.split(",")) + out_file = request.env["ir.attachment"].browse(list_ids)._create_temp_zip() + return http.send_file( + filepath_or_fp=out_file, + mimetype="application/zip", + as_attachment=True, + filename=_("attachments.zip"), + ) diff --git a/attachment_zipped_download/i18n/attachment_zipped_download.pot b/attachment_zipped_download/i18n/attachment_zipped_download.pot new file mode 100644 index 00000000..89025db4 --- /dev/null +++ b/attachment_zipped_download/i18n/attachment_zipped_download.pot @@ -0,0 +1,51 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_zipped_download +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: attachment_zipped_download +#: model:ir.model,name:attachment_zipped_download.model_ir_attachment +msgid "Attachment" +msgstr "" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment__display_name +msgid "Display Name" +msgstr "" + +#. module: attachment_zipped_download +#: model:ir.actions.server,name:attachment_zipped_download.action_attachments_download +msgid "Download" +msgstr "" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment__id +msgid "ID" +msgstr "" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment____last_update +msgid "Last Modified on" +msgstr "" + +#. module: attachment_zipped_download +#: code:addons/attachment_zipped_download/models/ir_attachment.py:0 +#, python-format +msgid "None attachment selected. Only binary attachments allowed." +msgstr "" + +#. module: attachment_zipped_download +#: code:addons/attachment_zipped_download/controllers/main.py:0 +#, python-format +msgid "attachments.zip" +msgstr "" diff --git a/attachment_zipped_download/i18n/es.po b/attachment_zipped_download/i18n/es.po new file mode 100644 index 00000000..bd6ee4c0 --- /dev/null +++ b/attachment_zipped_download/i18n/es.po @@ -0,0 +1,55 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_zipped_download +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2022-03-15 14:04+0000\n" +"PO-Revision-Date: 2022-03-15 15:05+0100\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: \n" +"X-Generator: Poedit 2.3\n" + +#. module: attachment_zipped_download +#: model:ir.model,name:attachment_zipped_download.model_ir_attachment +msgid "Attachment" +msgstr "Adjunto" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment__display_name +msgid "Display Name" +msgstr "Nombre mostrado" + +#. module: attachment_zipped_download +#: model:ir.actions.server,name:attachment_zipped_download.action_attachments_download +msgid "Download" +msgstr "Descargar" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment__id +msgid "ID" +msgstr "ID" + +#. module: attachment_zipped_download +#: model:ir.model.fields,field_description:attachment_zipped_download.field_ir_attachment____last_update +msgid "Last Modified on" +msgstr "Última modificación el" + +#. module: attachment_zipped_download +#: code:addons/attachment_zipped_download/models/ir_attachment.py:0 +#, python-format +msgid "None attachment selected. Only binary attachments allowed." +msgstr "No se seleccionó ningún archivo adjunto. Solo se permiten archivos adjuntos binarios." + +#. module: attachment_zipped_download +#: code:addons/attachment_zipped_download/controllers/main.py:0 +#, python-format +msgid "attachments.zip" +msgstr "adjuntos.zip" diff --git a/attachment_zipped_download/models/__init__.py b/attachment_zipped_download/models/__init__.py new file mode 100644 index 00000000..aaf38a16 --- /dev/null +++ b/attachment_zipped_download/models/__init__.py @@ -0,0 +1 @@ +from . import ir_attachment diff --git a/attachment_zipped_download/models/ir_attachment.py b/attachment_zipped_download/models/ir_attachment.py new file mode 100644 index 00000000..1f58adad --- /dev/null +++ b/attachment_zipped_download/models/ir_attachment.py @@ -0,0 +1,36 @@ +# Copyright 2019 César Fernández Domínguez +# Copyright 2022 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +import zipfile +from io import BytesIO + +from odoo import _, models +from odoo.exceptions import UserError + + +class IrAttachment(models.Model): + _inherit = "ir.attachment" + + def action_attachments_download(self): + items = self.filtered(lambda x: x.type == "binary") + if not items: + raise UserError( + _("None attachment selected. Only binary attachments allowed.") + ) + ids = ",".join(map(str, items.ids)) + return { + "type": "ir.actions.act_url", + "url": "/web/attachment/download_zip?ids=%s" % (ids), + "target": "self", + } + + def _create_temp_zip(self): + zip_buffer = BytesIO() + with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: + for attachment in self: + zip_file.write( + attachment._full_path(attachment.store_fname), attachment.name + ) + zip_buffer.seek(0) + zip_file.close() + return zip_buffer diff --git a/attachment_zipped_download/readme/CONTRIBUTORS.rst b/attachment_zipped_download/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..536c5890 --- /dev/null +++ b/attachment_zipped_download/readme/CONTRIBUTORS.rst @@ -0,0 +1,6 @@ +* César Fernández Domínguez + +* `Tecnativa `_: + + * Víctor Martínez + * Pedro M. Baeza diff --git a/attachment_zipped_download/readme/DESCRIPTION.rst b/attachment_zipped_download/readme/DESCRIPTION.rst new file mode 100644 index 00000000..15f07dbb --- /dev/null +++ b/attachment_zipped_download/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows downloading multiple attachments as a zip file. diff --git a/attachment_zipped_download/readme/USAGE.rst b/attachment_zipped_download/readme/USAGE.rst new file mode 100644 index 00000000..0acc4217 --- /dev/null +++ b/attachment_zipped_download/readme/USAGE.rst @@ -0,0 +1,2 @@ +#. Go to *Settings > Technical > Database Structure > Attachments* and select some files. +#. Go to *Actions > Download* and a zip file containing the selected files will be downloaded. diff --git a/attachment_zipped_download/static/description/icon.png b/attachment_zipped_download/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/attachment_zipped_download/static/description/icon.png differ diff --git a/attachment_zipped_download/static/description/index.html b/attachment_zipped_download/static/description/index.html new file mode 100644 index 00000000..b302dc2a --- /dev/null +++ b/attachment_zipped_download/static/description/index.html @@ -0,0 +1,432 @@ + + + + + + +Attachment Zipped Download + + + +
+

Attachment Zipped Download

+ + +

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

+

This module allows downloading multiple attachments as a zip file.

+

Table of contents

+ +
+

Usage

+
    +
  1. Go to Settings > Technical > Database Structure > Attachments and select some files.
  2. +
  3. Go to Actions > Download and a zip file containing the selected files will be downloaded.
  4. +
+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

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

+

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

+
+
+
+ + diff --git a/attachment_zipped_download/tests/__init__.py b/attachment_zipped_download/tests/__init__.py new file mode 100644 index 00000000..2654e07a --- /dev/null +++ b/attachment_zipped_download/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +from . import test_attachment_zipped_download diff --git a/attachment_zipped_download/tests/test_attachment_zipped_download.py b/attachment_zipped_download/tests/test_attachment_zipped_download.py new file mode 100644 index 00000000..63799c8f --- /dev/null +++ b/attachment_zipped_download/tests/test_attachment_zipped_download.py @@ -0,0 +1,35 @@ +# Copyright 2022 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 + +import odoo.tests + + +class TestAttachmentZippedDownload(odoo.tests.HttpCase): + def setUp(self): + super().setUp() + test_1 = self._create_attachment("test1.txt") + test_2 = self._create_attachment("test2.txt") + self.attachments = test_1 + test_2 + self.user = self.env["res.users"].create( + { + "name": "test-user", + "login": "test-user", + "password": "test-user", + "groups_id": [(6, 0, [self.env.ref("base.group_user").id])], + } + ) + + def _create_attachment(self, name): + return self.env["ir.attachment"].create( + { + "name": name, + "datas": base64.b64encode(b"\xff data"), + } + ) + + def test_action_attachments_download(self): + self.authenticate("test-user", "test-user") + res = self.attachments.action_attachments_download() + response = self.url_open(res["url"], timeout=20) + self.assertEqual(response.status_code, 200) diff --git a/attachment_zipped_download/views/ir_attachment_view.xml b/attachment_zipped_download/views/ir_attachment_view.xml new file mode 100644 index 00000000..4f4e2b16 --- /dev/null +++ b/attachment_zipped_download/views/ir_attachment_view.xml @@ -0,0 +1,13 @@ + + + + Download + + + code + + if records: + action = records.action_attachments_download() + + +