Skip to content

Commit

Permalink
[ADD] ir_attachment_download (#740)
Browse files Browse the repository at this point in the history
* [ADD] ir_attachment_download

* [FIX] ir_attachment_download: test

* [FIX] ir_attachment_download_test: test pep8

* [IMP] ir_attachment_download: enable downloading + fix tests
  • Loading branch information
mikelarre authored and oihane committed Aug 7, 2019
1 parent 4cce121 commit 9ae344f
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ir_attachment_download/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

====================
Attachments download
====================

Creates action to download selected attachments


Credits
=======

Contributors
------------
* Mikel Arregi <[email protected]>
* Ana Juaristi <[email protected]>
2 changes: 2 additions & 0 deletions ir_attachment_download/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
19 changes: 19 additions & 0 deletions ir_attachment_download/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2019 Mikel Arregi Etxaniz - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
{
"name": "Attachments download",
"version": "12.0.1.0.0",
"license": "AGPL-3",
"depends": [
"base",
"web",
],
"author": "AvanzOSC",
"website": "http://www.avanzosc.es",
"category": "Tools",
"data": [
"wizard/attach_download_action_view.xml",
"views/ir_model_view.xml",
],
"installable": True,
}
118 changes: 118 additions & 0 deletions ir_attachment_download/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * ir_attachment_download
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-07-29 10:26+0000\n"
"PO-Revision-Date: 2019-07-29 10:26+0000\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: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__name
msgid "Action Name"
msgstr "Nombre de la acción"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__attach_fields
msgid "Attach Fields"
msgstr "Campos"

#. module: ir_attachment_download
#: model:ir.model,name:ir_attachment_download.model_ir_attachment
msgid "Attachment"
msgstr "Adjunto"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__binary_fields
msgid "Binary Fields"
msgstr "Campos binarios"

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Cancel"
msgstr "Cancelar"

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Confirm"
msgstr "Confirmar"

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Create Action server"
msgstr "Crear acción de servidor"

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.ir_model_action_creation_view
msgid "Create Attach Downloader"
msgstr "Crear descargador de adjuntos"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__create_uid
msgid "Created by"
msgstr "Creado por"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__create_date
msgid "Created on"
msgstr "Creado el"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__display_name
msgid "Display Name"
msgstr "Nombre mostrado"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__id
msgid "ID"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action____last_update
msgid "Last Modified on"
msgstr "Última modificación en"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__write_uid
msgid "Last Updated by"
msgstr "Última actualización por"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__write_date
msgid "Last Updated on"
msgstr "Última actualización el"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__model_id
msgid "Model"
msgstr "Modelo"

#. module: ir_attachment_download
#: code:addons/ir_attachment_download/models/ir_attachment.py:59
#, python-format
msgid "No files to download"
msgstr "No hay archivos para descargar"

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__select_field
msgid "Select Field"
msgstr "Selecciona Campo"

#. module: ir_attachment_download
#: model:ir.model,name:ir_attachment_download.model_attach_download_action
msgid "attach.download.action"
msgstr "Asistente para crear acción de descarga"

#. module: ir_attachment_download
#: model:ir.actions.act_window,name:ir_attachment_download.action_attach_download_action
msgid "create attach download action"
msgstr "crear acción para descargar adjuntos"

118 changes: 118 additions & 0 deletions ir_attachment_download/i18n/ir_attachment_download.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * ir_attachment_download
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-07-29 10:26+0000\n"
"PO-Revision-Date: 2019-07-29 10:26+0000\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: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__name
msgid "Action Name"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__attach_fields
msgid "Attach Fields"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model,name:ir_attachment_download.model_ir_attachment
msgid "Attachment"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__binary_fields
msgid "Binary Fields"
msgstr ""

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Cancel"
msgstr ""

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Confirm"
msgstr ""

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.attach_download_action
msgid "Create Action server"
msgstr ""

#. module: ir_attachment_download
#: model_terms:ir.ui.view,arch_db:ir_attachment_download.ir_model_action_creation_view
msgid "Create Attach Downloader"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__create_uid
msgid "Created by"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__create_date
msgid "Created on"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__display_name
msgid "Display Name"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__id
msgid "ID"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action____last_update
msgid "Last Modified on"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__write_uid
msgid "Last Updated by"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__write_date
msgid "Last Updated on"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__model_id
msgid "Model"
msgstr ""

#. module: ir_attachment_download
#: code:addons/ir_attachment_download/models/ir_attachment.py:59
#, python-format
msgid "No files to download"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model.fields,field_description:ir_attachment_download.field_attach_download_action__select_field
msgid "Select Field"
msgstr ""

#. module: ir_attachment_download
#: model:ir.model,name:ir_attachment_download.model_attach_download_action
msgid "attach.download.action"
msgstr ""

#. module: ir_attachment_download
#: model:ir.actions.act_window,name:ir_attachment_download.action_attach_download_action
msgid "create attach download action"
msgstr ""

1 change: 1 addition & 0 deletions ir_attachment_download/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_attachment
59 changes: 59 additions & 0 deletions ir_attachment_download/models/ir_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2019 Mikel Arregi Etxaniz - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import _, api, exceptions, fields, models
from io import BytesIO
import zipfile
import base64


class IrAttachment(models.Model):
_inherit = "ir.attachment"

def _generate_zip(self, files):
mem_zip = BytesIO()
with zipfile.ZipFile(mem_zip, mode="w",
compression=zipfile.ZIP_DEFLATED) as zf:
for f in files:
zf.writestr(f[0], f[1])
zip_file = mem_zip.getvalue()
mem_zip.close()
return zip_file

@api.model
def _generate_zip_from_attachments(self, records,
att_fields=None):
files = []
if att_fields:
att_fields = self.env["ir.model.fields"].browse(att_fields)
for attach in records:
for field in att_fields:
if attach[field.name]:
file = base64.b64decode(attach[field.name])
files.append((attach.name, file))
else:
for attach in self.env['ir.attachment'].search(
[('res_model', '=', str(records._name)),
('res_id', 'in', records.ids)]):
file = base64.b64decode(attach.datas)
files.append((attach.name, file))
if files:
zip_file = self._generate_zip(files)
model = [("model", "=", self._context.get("active_model"))]
file_name = "%s/%s.zip" % (
(self.env["ir.model"].search(model).name or ""),
fields.Datetime.now())
attach_id = self.create({
'datas': base64.b64encode(zip_file),
'name': file_name,
'datas_fname': file_name,
})
file_url = "web/content/?model=ir.attachment&id=" + str(
attach_id.id) + "&filename_field=datas_fname&field=datas" \
"&download=true&filename=" + file_name,
return {
'type': 'ir.actions.act_url',
'url': file_url,
'target': 'new'
}
else:
raise exceptions.Warning(_("No files to download"))
1 change: 1 addition & 0 deletions ir_attachment_download/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_ir_attachment
37 changes: 37 additions & 0 deletions ir_attachment_download/tests/test_ir_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019 Mikel Arregi Etxaniz - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import odoo.tests.common as common
import base64


@common.at_install(False)
@common.post_install(True)
class IrAttachment(common.SavepointCase):

@classmethod
def setUpClass(cls):
super(IrAttachment, cls).setUpClass()
cls.wiz_obj = cls.env['attach.download.action']
cls.attachment = cls.env['ir.attachment'].create({
'name': 'Attach1',
'datas': base64.b64encode(b"avanzosc")
})
cls.property_field = cls.env['ir.model.fields'].search([
('ttype', '=', 'binary')], limit=1)
cls.property = cls.env['ir.property'].create({
'name': 'Property to test',
'value_binary': cls.attachment.datas,
'fields_id': cls.property_field.id,
})

def test_attachment_downloader(self):
wiz = self.wiz_obj.create({
'name': 'Download',
'model_id': self.env['ir.model'].search([
('model', '=', 'ir.property')], limit=1).id,
})
wiz.create_action_server()
action = self.env['ir.actions.server'].search(
[('name', '=', 'Download')])
self.assertTrue(action)
15 changes: 15 additions & 0 deletions ir_attachment_download/views/ir_model_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record model="ir.ui.view" id="ir_model_action_creation_view">
<field name="model">ir.model</field>
<field name="inherit_id" ref="base.view_model_form" />
<field name="arch" type="xml">
<xpath expr="//sheet" position="before">
<header>
<button name="%(action_attach_download_action)d"
string="Create Attach Downloader" type="action" class="oe_highlight" />
</header>
</xpath>
</field>
</record>
</odoo>
Loading

0 comments on commit 9ae344f

Please sign in to comment.