From 620aac50414bd7a8202235dedfae2213eb38c3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Wed, 13 Oct 2021 17:27:50 +0200 Subject: [PATCH] [IMP] document_url: Visual and functional improvements in UI. Define the add attachment and add URL buttons on the same line. Allow downloading of url type attachments by clicking on the icon or the name. Define the name of the url type attachments as links (allows to open it in a new tab). Hide the download button for url attachments. Force to set mimetype to "application/link" for url attachments. Displays a link icon for url attachments. TT30263 --- document_url/__init__.py | 1 + .../migrations/14.0.1.0.0/post-migration.py | 16 ++++++++++ document_url/models/__init__.py | 3 ++ document_url/models/ir_attachment.py | 13 +++++++++ document_url/static/src/js/url.js | 9 ++++++ .../static/src/scss/document_url.scss | 15 ++++++++++ document_url/static/src/xml/url.xml | 29 +++++++++++++++++++ document_url/tests/test_document_url.py | 2 ++ document_url/view/document_url_view.xml | 4 +++ 9 files changed, 92 insertions(+) create mode 100644 document_url/migrations/14.0.1.0.0/post-migration.py create mode 100644 document_url/models/__init__.py create mode 100644 document_url/models/ir_attachment.py create mode 100644 document_url/static/src/scss/document_url.scss diff --git a/document_url/__init__.py b/document_url/__init__.py index 5f3c3bc9..fb6a3f22 100644 --- a/document_url/__init__.py +++ b/document_url/__init__.py @@ -1,4 +1,5 @@ # Copyright 2014 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com) # Copyright 2014 Tecnativa - Pedro M. Baeza # Copyright 2016 ACSONE SA/NV () +from . import models from . import wizard diff --git a/document_url/migrations/14.0.1.0.0/post-migration.py b/document_url/migrations/14.0.1.0.0/post-migration.py new file mode 100644 index 00000000..58a70619 --- /dev/null +++ b/document_url/migrations/14.0.1.0.0/post-migration.py @@ -0,0 +1,16 @@ +# Copyright 2021 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.logged_query( + env.cr, + """ + UPDATE ir_attachment + SET mimetype = 'application/link' + WHERE type = 'link' + """, + ) diff --git a/document_url/models/__init__.py b/document_url/models/__init__.py new file mode 100644 index 00000000..aee1fe67 --- /dev/null +++ b/document_url/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import ir_attachment diff --git a/document_url/models/ir_attachment.py b/document_url/models/ir_attachment.py new file mode 100644 index 00000000..0413ae1c --- /dev/null +++ b/document_url/models/ir_attachment.py @@ -0,0 +1,13 @@ +# Copyright 2021 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class IrAttachment(models.Model): + _inherit = "ir.attachment" + + def _compute_mimetype(self, values): + if values.get("url"): + return "application/link" + return super()._compute_mimetype(values) diff --git a/document_url/static/src/js/url.js b/document_url/static/src/js/url.js index a4654c82..2cc2b33a 100644 --- a/document_url/static/src/js/url.js +++ b/document_url/static/src/js/url.js @@ -10,6 +10,7 @@ odoo.define("document_url", function (require) { "use strict"; const AttachmentBox = require("mail/static/src/components/attachment_box/attachment_box.js"); + const Attachment = require("mail/static/src/components/attachment/attachment.js"); const {patch} = require("web.utils"); patch(AttachmentBox, "document_url", { @@ -30,4 +31,12 @@ odoo.define("document_url", function (require) { this.trigger("reload"); }, }); + patch(Attachment, "document_url", { + _onClickImage(ev) { + if (!this.attachment.isViewable) { + this._onClickDownload(ev); + } + this._super.apply(this, arguments); + }, + }); }); diff --git a/document_url/static/src/scss/document_url.scss b/document_url/static/src/scss/document_url.scss new file mode 100644 index 00000000..3b8b69a5 --- /dev/null +++ b/document_url/static/src/scss/document_url.scss @@ -0,0 +1,15 @@ +.o_AttachmentBox_content { + text-align: center; + display: block; +} +.o_AttachmentList_partialListNonImages { + .o_Attachment_image { + cursor: pointer; + } + .o_Attachment_filename a { + color: #4c4c4c; + } +} +.o_Attachment_url_icon { + margin: 4px 2px 0px 4px; +} diff --git a/document_url/static/src/xml/url.xml b/document_url/static/src/xml/url.xml index b8ec9c48..6a139db5 100644 --- a/document_url/static/src/xml/url.xml +++ b/document_url/static/src/xml/url.xml @@ -15,4 +15,33 @@ + + + + + + + + attachment.mimetype != 'application/link' + + + attachment.mimetype != 'application/link' + + + + + + + diff --git a/document_url/tests/test_document_url.py b/document_url/tests/test_document_url.py index 348b8a0f..45ee2ed0 100644 --- a/document_url/tests/test_document_url.py +++ b/document_url/tests/test_document_url.py @@ -27,3 +27,5 @@ class TestDocumentUrl(common.TransactionCase): ] attachment_added_count = self.env["ir.attachment"].search_count(domain) self.assertEqual(attachment_added_count, 1) + attachment = self.env["ir.attachment"].search(domain) + self.assertEqual(attachment.mimetype, "application/link") diff --git a/document_url/view/document_url_view.xml b/document_url/view/document_url_view.xml index cd96777c..1403b168 100644 --- a/document_url/view/document_url_view.xml +++ b/document_url/view/document_url_view.xml @@ -7,6 +7,10 @@ >