diff --git a/mail_preview_base/README.rst b/mail_preview_base/README.rst new file mode 100644 index 0000000000..3cf1aef1b2 --- /dev/null +++ b/mail_preview_base/README.rst @@ -0,0 +1,101 @@ +============ +Mail Preview +============ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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-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%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/15.0/mail_preview_base + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-15-0/social-15-0-mail_preview_base + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/205/15.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module has two goals: + +* Define a unique system for inheritance that will allow to introduce more + types for the preview. +* Define a new widget that will allow to show the preview. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Creating a new preview file +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You need to add the configuration in three places: + +* Function `_checkAttachment` and `_hasPreview` from `mail.DocumentViewer` on + Javascript +* Qweb template `DocumentViewer.Content` + +To use this module, you need to: + +#. Go to Settings > Technical > Attachments +#. Create attachment with image or pdf format +#. Show preview icon in "File content" + +As an example, you can check `mail_preview_audio`. + +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 +~~~~~~~ + +* Creu Blanca + +Contributors +~~~~~~~~~~~~ + +* Enric Tobella + +* `Tecnativa `_: + + * Víctor Martínez + +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/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_preview_base/__init__.py b/mail_preview_base/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mail_preview_base/__manifest__.py b/mail_preview_base/__manifest__.py new file mode 100644 index 0000000000..fc411eccbf --- /dev/null +++ b/mail_preview_base/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2020 Creu Blanca +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Mail Preview", + "summary": """ + Base to add more previewing options""", + "version": "16.0.1.0.0", + "license": "LGPL-3", + "author": "Creu Blanca,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/social", + "depends": ["mail"], + "data": [ + "views/ir_attachment_view.xml", + ], + "assets": { + "web.assets_backend": [ + "mail_preview_base/static/src/js/preview.js", + "mail_preview_base/static/src/scss/preview.scss", + ], + "web.assets_qweb": ["mail_preview_base/static/src/xml/preview.xml"], + }, +} diff --git a/mail_preview_base/i18n/mail_preview_base.pot b/mail_preview_base/i18n/mail_preview_base.pot new file mode 100644 index 0000000000..b9cdfcc4cc --- /dev/null +++ b/mail_preview_base/i18n/mail_preview_base.pot @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_preview_base +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.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: mail_preview_base +#. openerp-web +#: code:addons/mail_preview_base/static/src/xml/preview.xml:0 +#: code:addons/mail_preview_base/static/src/xml/preview.xml:0 +#, python-format +msgid "#{widget.activeAttachment.source_url}" +msgstr "" + +#. module: mail_preview_base +#. openerp-web +#: code:addons/mail_preview_base/static/src/xml/preview.xml:0 +#, python-format +msgid "" +"#{widget.activeAttachment.source_url}?unique=1&signature=#{widget.activeAttachment.checksum}" +msgstr "" + +#. module: mail_preview_base +#. openerp-web +#: code:addons/mail_preview_base/static/src/xml/preview.xml:0 +#, python-format +msgid "" +"/web/static/lib/pdfjs/web/viewer.html?file=#{widget.activeAttachment.source_url}" +msgstr "" diff --git a/mail_preview_base/readme/CONTRIBUTORS.rst b/mail_preview_base/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..bc379ab591 --- /dev/null +++ b/mail_preview_base/readme/CONTRIBUTORS.rst @@ -0,0 +1,5 @@ +* Enric Tobella + +* `Tecnativa `_: + + * Víctor Martínez diff --git a/mail_preview_base/readme/DESCRIPTION.rst b/mail_preview_base/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..c353c3f62c --- /dev/null +++ b/mail_preview_base/readme/DESCRIPTION.rst @@ -0,0 +1,5 @@ +This module has two goals: + +* Define a unique system for inheritance that will allow to introduce more + types for the preview. +* Define a new widget that will allow to show the preview. diff --git a/mail_preview_base/readme/USAGE.rst b/mail_preview_base/readme/USAGE.rst new file mode 100644 index 0000000000..ea4857ef98 --- /dev/null +++ b/mail_preview_base/readme/USAGE.rst @@ -0,0 +1,16 @@ +Creating a new preview file +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You need to add the configuration in three places: + +* Function `_checkAttachment` and `_hasPreview` from `mail.DocumentViewer` on + Javascript +* Qweb template `DocumentViewer.Content` + +To use this module, you need to: + +#. Go to Settings > Technical > Attachments +#. Create attachment with image or pdf format +#. Show preview icon in "File content" + +As an example, you can check `mail_preview_audio`. diff --git a/mail_preview_base/static/description/icon.png b/mail_preview_base/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/mail_preview_base/static/description/icon.png differ diff --git a/mail_preview_base/static/description/index.html b/mail_preview_base/static/description/index.html new file mode 100644 index 0000000000..a4290702a6 --- /dev/null +++ b/mail_preview_base/static/description/index.html @@ -0,0 +1,451 @@ + + + + + + +Mail Preview + + + +
+

Mail Preview

+ + +

Beta License: LGPL-3 OCA/social Translate me on Weblate Try me on Runbot

+

This module has two goals:

+
    +
  • Define a unique system for inheritance that will allow to introduce more +types for the preview.
  • +
  • Define a new widget that will allow to show the preview.
  • +
+

Table of contents

+ +
+

Usage

+
+

Creating a new preview file

+

You need to add the configuration in three places:

+
    +
  • Function _checkAttachment and _hasPreview from mail.DocumentViewer on +Javascript
  • +
  • Qweb template DocumentViewer.Content
  • +
+

To use this module, you need to:

+
    +
  1. Go to Settings > Technical > Attachments
  2. +
  3. Create attachment with image or pdf format
  4. +
  5. Show preview icon in “File content”
  6. +
+

As an example, you can check mail_preview_audio.

+
+
+
+

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

+
    +
  • Creu Blanca
  • +
+
+
+

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

+

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

+
+
+
+ + diff --git a/mail_preview_base/static/src/js/preview.js b/mail_preview_base/static/src/js/preview.js new file mode 100644 index 0000000000..3bc8028567 --- /dev/null +++ b/mail_preview_base/static/src/js/preview.js @@ -0,0 +1,152 @@ +/** ******************************************************************************** + Copyright 2020 Creu Blanca + License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + **********************************************************************************/ + +odoo.define("mail_preview_base.preview", function (require) { + "use strict"; + + const DocumentViewer = require("@mail/js/document_viewer")[Symbol.for("default")]; + var basic_fields = require("web.basic_fields"); + var registry = require("web.field_registry"); + + DocumentViewer.include({ + init: function (parent, attachments) { + this._super.apply(this, arguments); + var self = this; + _.forEach(this.attachment, function (attachment) { + if ( + attachment.mimetype === "application/pdf" || + attachment.type === "text" + ) { + attachment.source_url = self._getContentUrl(attachment); + } else { + attachment.source_url = self._getImageUrl(attachment); + } + }); + this.attachment = this.attachment.concat( + _.filter(attachments, function (attachment) { + return self._checkAttachment(attachment); + }) + ); + }, + + /* + This function is a hook, it will allow to define new kind of + records + */ + _checkAttachment: function () { + return false; + }, + _getContentUrl: function (attachment) { + return ( + "/web/content/" + + attachment.id + + "?filename=" + + window.encodeURIComponent(attachment.name) + ); + }, + _getImageUrl: function (attachment) { + return "/web/image/" + attachment.id; + }, + _hasPreview: function (type, attachment) { + return ( + type === "image" || + type === "video" || + attachment.mimetype === "application/pdf" + ); + }, + }); + + var FieldPreviewViewer = DocumentViewer.extend({ + init: function (parent, attachments, activeAttachmentID, model, field) { + this.fieldModelName = model; + this.fieldName = field; + this._super.apply(this, arguments); + this.modelName = model; + }, + _onDownload: function (e) { + e.preventDefault(); + window.location = + "/web/content/" + + this.fieldModelName + + "/" + + this.activeAttachment.id + + "/" + + this.fieldName + + "/" + + "datas" + + "?download=true"; + }, + _getContentUrl: function (attachment) { + return ( + "/web/content/" + + this.fieldModelName + + "/" + + attachment.id + + "/" + + this.fieldName + + "?filename=" + + window.encodeURIComponent(attachment.name) + ); + }, + _getImageUrl: function (attachment) { + return ( + "/web/image/" + + this.fieldModelName + + "/" + + attachment.id + + "/" + + this.fieldName + ); + }, + }); + + var FieldPreviewBinary = basic_fields.FieldBinaryFile.extend({ + events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, { + "click .preview_file": "_previewFile", + }), + init: function () { + this._super.apply(this, arguments); + this.mimetype_value = this.recordData.mimetype; + }, + _previewFile: function (event) { + event.stopPropagation(); + event.preventDefault(); + var attachmentViewer = new FieldPreviewViewer( + this, + [this.attachment], + this.res_id, + this.model, + this.name + ); + attachmentViewer.appendTo($("body")); + }, + _renderReadonly: function () { + this._super.apply(this, arguments); + if (this.value) { + this.attachment = { + mimetype: this.mimetype_value, + id: this.res_id, + fileType: this.mimetype_value, + name: this.recordData.name, + }; + var mimetype = this.mimetype_value; + var type = mimetype.split("/").shift(); + if (DocumentViewer.prototype._hasPreview(type, this.attachment)) { + this.$el.prepend( + $("").addClass("fa fa-search preview_file") + ); + } + } + }, + }); + + registry.add("preview_binary", FieldPreviewBinary); + + return { + FieldPreviewViewer: FieldPreviewViewer, + FieldPreviewBinary: FieldPreviewBinary, + DocumentViewer: DocumentViewer, + }; +}); diff --git a/mail_preview_base/static/src/scss/preview.scss b/mail_preview_base/static/src/scss/preview.scss new file mode 100644 index 0000000000..377cd24991 --- /dev/null +++ b/mail_preview_base/static/src/scss/preview.scss @@ -0,0 +1,3 @@ +.o_form_view .o_form_uri > span:nth-child(2) { + color: $o-brand-primary; +} diff --git a/mail_preview_base/static/src/xml/preview.xml b/mail_preview_base/static/src/xml/preview.xml new file mode 100644 index 0000000000..9600a8402f --- /dev/null +++ b/mail_preview_base/static/src/xml/preview.xml @@ -0,0 +1,36 @@ + + + + + #{widget.activeAttachment.source_url}?unique=1&signature=#{widget.activeAttachment.checksum} + + + /web/static/lib/pdfjs/web/viewer.html?file=#{widget.activeAttachment.source_url} + + + #{widget.activeAttachment.source_url} + + + #{widget.activeAttachment.source_url} + + + + + + + + + + diff --git a/mail_preview_base/views/ir_attachment_view.xml b/mail_preview_base/views/ir_attachment_view.xml new file mode 100644 index 0000000000..69bb003d57 --- /dev/null +++ b/mail_preview_base/views/ir_attachment_view.xml @@ -0,0 +1,13 @@ + + + + ir.attachment + + 99 + + + preview_binary + + + + diff --git a/setup/mail_preview_base/odoo/addons/mail_preview_base b/setup/mail_preview_base/odoo/addons/mail_preview_base new file mode 120000 index 0000000000..3584d6fc6e --- /dev/null +++ b/setup/mail_preview_base/odoo/addons/mail_preview_base @@ -0,0 +1 @@ +../../../../mail_preview_base \ No newline at end of file diff --git a/setup/mail_preview_base/setup.py b/setup/mail_preview_base/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/mail_preview_base/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)