From 76e7285a2f304e8bbe00083906904de000f9f74f Mon Sep 17 00:00:00 2001 From: tarteo Date: Wed, 11 Jul 2018 14:13:08 +0200 Subject: [PATCH] [MIG] attachment_preview: Migration to 11.0 [FIX] Lint [FIX] lint and flake [ADD] tests [ADD] tests [ADD] tests [ADD] Tests [ADD] Package python-magic [ADD] Tests [FIX] Lint --- attachment_preview/README.rst | 81 +- attachment_preview/__init__.py | 25 +- attachment_preview/__manifest__.py | 43 +- attachment_preview/i18n/de.po | 28 + attachment_preview/i18n/es.po | 28 + attachment_preview/i18n/it.po | 28 + attachment_preview/i18n/nl.po | 24 +- attachment_preview/i18n/pt_PT.po | 28 + attachment_preview/i18n/sl.po | 24 +- attachment_preview/i18n/zh_CN.po | 28 + attachment_preview/model/__init__.py | 21 - attachment_preview/model/ir_attachment.py | 68 - attachment_preview/models/__init__.py | 4 + attachment_preview/models/ir_attachment.py | 75 + attachment_preview/readme/CONTRIBUTORS.rst | 2 + attachment_preview/readme/CREDITS.rst | 4 + attachment_preview/readme/DESCRIPTION.rst | 3 + attachment_preview/readme/INSTALL.rst | 4 + attachment_preview/readme/USAGE.rst | 2 + .../static/lib/ViewerJS/compatibility.js | 29 +- .../static/lib/ViewerJS/index.html | 60 +- attachment_preview/static/lib/ViewerJS/pdf.js | 391 +++-- .../static/lib/ViewerJS/pdf.worker.js | 447 +++-- .../static/lib/ViewerJS/pdfjsversion.js | 2 +- .../static/lib/ViewerJS/text_layer_builder.js | 90 +- .../static/lib/ViewerJS/ui_utils.js | 99 +- .../static/lib/ViewerJS/webodf.js | 1468 ++++++++++------- .../static/src/css/attachment_preview.css | 33 +- .../static/src/js/attachment_preview.js | 350 ++-- .../static/src/js/viewerjs_tweaks.js | 41 +- .../static/src/xml/attachment_preview.xml | 15 +- attachment_preview/templates/assets.xml | 9 + attachment_preview/tests/__init__.py | 4 + .../tests/test_attachment_preview.py | 46 + .../view/attachment_preview.xml | 11 - 35 files changed, 2241 insertions(+), 1374 deletions(-) create mode 100644 attachment_preview/i18n/de.po create mode 100644 attachment_preview/i18n/es.po create mode 100644 attachment_preview/i18n/it.po create mode 100644 attachment_preview/i18n/pt_PT.po create mode 100644 attachment_preview/i18n/zh_CN.po delete mode 100644 attachment_preview/model/__init__.py delete mode 100644 attachment_preview/model/ir_attachment.py create mode 100644 attachment_preview/models/__init__.py create mode 100644 attachment_preview/models/ir_attachment.py create mode 100644 attachment_preview/readme/CONTRIBUTORS.rst create mode 100644 attachment_preview/readme/CREDITS.rst create mode 100644 attachment_preview/readme/DESCRIPTION.rst create mode 100644 attachment_preview/readme/INSTALL.rst create mode 100644 attachment_preview/readme/USAGE.rst create mode 100644 attachment_preview/templates/assets.xml create mode 100644 attachment_preview/tests/__init__.py create mode 100644 attachment_preview/tests/test_attachment_preview.py delete mode 100644 attachment_preview/view/attachment_preview.xml diff --git a/attachment_preview/README.rst b/attachment_preview/README.rst index 4f6feebe..197d487f 100644 --- a/attachment_preview/README.rst +++ b/attachment_preview/README.rst @@ -1,15 +1,44 @@ +=================== Preview attachments =================== +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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/11.0/attachment_preview + :alt: OCA/knowledge +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/knowledge-11-0/knowledge-11-0-attachment_preview + :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/11.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + This addon allows to preview attachments supported by http://viewerjs.org. Currently, that's most Libreoffice files and PDFs. +**Table of contents** + +.. contents:: + :local: + Installation ============ For filetype recognition, you'll get the best results by installing -``python-magic``:: +``python-magic``: sudo apt-get install python-magic @@ -19,28 +48,52 @@ Usage The module adds a little print preview icon right of download links for attachments or binary fields. +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 +~~~~~~~ + +* Therp BV +* Onestein + +Contributors +~~~~~~~~~~~~ + +* Holger Brunn +* Dennis Sluijk + +Other credits +~~~~~~~~~~~~~ + Addon icon ---------- * courtesy of http://commons.wikimedia.org/wiki/Crystal_Clear -Contributors ------------- - -* Holger Brunn - -Maintainer ----------- - -.. image:: http://odoo-community.org/logo.png - :alt: Odoo Community Association - :target: http://odoo-community.org +Maintainers +~~~~~~~~~~~ This module is maintained by the OCA. -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. +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org -To contribute to this module, please visit http://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_preview/__init__.py b/attachment_preview/__init__.py index fcf92314..5b89c089 100644 --- a/attachment_preview/__init__.py +++ b/attachment_preview/__init__.py @@ -1,21 +1,4 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2014 Therp BV (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -from . import model +# Copyright 2014 Therp BV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/attachment_preview/__manifest__.py b/attachment_preview/__manifest__.py index 01012f0d..7020cd32 100644 --- a/attachment_preview/__manifest__.py +++ b/attachment_preview/__manifest__.py @@ -1,46 +1,23 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2014 Therp BV (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# Copyright 2014 Therp BV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + { "name": "Preview attachments", - "version": "8.0.1.1.0", - "author": "Therp BV,Odoo Community Association (OCA)", + "version": "11.0.1.2.0", + "author": "Therp BV," + "Onestein," + "Odoo Community Association (OCA)", "license": "AGPL-3", - "complexity": "normal", - 'summary': 'Preview attachments supported by Viewer.js', + "summary": 'Preview attachments supported by Viewer.js', "category": "Knowledge Management", "depends": [ 'web', ], "data": [ - "view/attachment_preview.xml", + "templates/assets.xml", ], "qweb": [ 'static/src/xml/attachment_preview.xml', ], - "test": [ - ], - "auto_install": False, - 'installable': False, - "application": False, - "external_dependencies": { - 'python': [], - }, + "installable": True, } diff --git a/attachment_preview/i18n/de.po b/attachment_preview/i18n/de.po new file mode 100644 index 00000000..73082d23 --- /dev/null +++ b/attachment_preview/i18n/de.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_preview +# +# Translators: +# Rudolf Schnapka , 2015 +msgid "" +msgstr "" +"Project-Id-Version: knowledge (8.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-12-08 08:32+0000\n" +"PO-Revision-Date: 2015-11-25 09:41+0000\n" +"Last-Translator: Rudolf Schnapka \n" +"Language-Team: German (http://www.transifex.com/oca/OCA-knowledge-8-0/language/de/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Language: de\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: attachment_preview +#. openerp-web +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 +#, python-format +msgid "Preview %s" +msgstr "Vorschau %s" diff --git a/attachment_preview/i18n/es.po b/attachment_preview/i18n/es.po new file mode 100644 index 00000000..24947668 --- /dev/null +++ b/attachment_preview/i18n/es.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_preview +# +# Translators: +# Antonio Trueba, 2016 +msgid "" +msgstr "" +"Project-Id-Version: knowledge (8.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-12-19 10:18+0000\n" +"PO-Revision-Date: 2016-01-25 18:15+0000\n" +"Last-Translator: Antonio Trueba\n" +"Language-Team: Spanish (http://www.transifex.com/oca/OCA-knowledge-8-0/language/es/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Language: es\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: attachment_preview +#. openerp-web +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 +#, python-format +msgid "Preview %s" +msgstr "Previsualizar %s" diff --git a/attachment_preview/i18n/it.po b/attachment_preview/i18n/it.po new file mode 100644 index 00000000..c42a26da --- /dev/null +++ b/attachment_preview/i18n/it.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_preview +# +# Translators: +# Paolo Valier, 2016 +msgid "" +msgstr "" +"Project-Id-Version: knowledge (8.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-03-05 11:50+0000\n" +"PO-Revision-Date: 2016-03-13 16:56+0000\n" +"Last-Translator: Paolo Valier\n" +"Language-Team: Italian (http://www.transifex.com/oca/OCA-knowledge-8-0/language/it/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Language: it\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: attachment_preview +#. openerp-web +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 +#, python-format +msgid "Preview %s" +msgstr "Anteprima %s" diff --git a/attachment_preview/i18n/nl.po b/attachment_preview/i18n/nl.po index 62ab7a2a..b828640f 100644 --- a/attachment_preview/i18n/nl.po +++ b/attachment_preview/i18n/nl.po @@ -1,25 +1,27 @@ # Translation of Odoo Server. # This file contains the translation of the following modules: -# * attachment_preview -# +# * attachment_preview +# +# Translators: msgid "" msgstr "" -"Project-Id-Version: Odoo Server 8.0\n" +"Project-Id-Version: knowledge (8.0)\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-15 11:04+0000\n" -"PO-Revision-Date: 2015-08-03 13:36+0200\n" -"Last-Translator: <>\n" -"Language-Team: \n" +"POT-Creation-Date: 2015-12-08 08:32+0000\n" +"PO-Revision-Date: 2015-11-07 12:44+0000\n" +"Last-Translator: OCA Transbot \n" +"Language-Team: Dutch (http://www.transifex.com/oca/OCA-knowledge-8-0/language/nl/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: \n" +"Content-Transfer-Encoding: \n" "Language: nl\n" -"X-Generator: Poedit 1.8.2\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" #. module: attachment_preview #. openerp-web -#: code:multiple:42 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 #, python-format msgid "Preview %s" msgstr "Voorbeeld %s" diff --git a/attachment_preview/i18n/pt_PT.po b/attachment_preview/i18n/pt_PT.po new file mode 100644 index 00000000..f78c3d69 --- /dev/null +++ b/attachment_preview/i18n/pt_PT.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_preview +# +# Translators: +# Pedro Castro Silva , 2016 +msgid "" +msgstr "" +"Project-Id-Version: knowledge (8.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-05-07 16:07+0000\n" +"PO-Revision-Date: 2016-05-22 22:19+0000\n" +"Last-Translator: Pedro Castro Silva \n" +"Language-Team: Portuguese (Portugal) (http://www.transifex.com/oca/OCA-knowledge-8-0/language/pt_PT/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Language: pt_PT\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: attachment_preview +#. openerp-web +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 +#, python-format +msgid "Preview %s" +msgstr "Antever %s" diff --git a/attachment_preview/i18n/sl.po b/attachment_preview/i18n/sl.po index 5e646c51..a7442443 100644 --- a/attachment_preview/i18n/sl.po +++ b/attachment_preview/i18n/sl.po @@ -1,26 +1,28 @@ # Translation of Odoo Server. # This file contains the translation of the following modules: -# * attachment_preview -# -# Matjaž Mozetič , 2015. +# * attachment_preview +# +# Translators: +# Matjaž Mozetič , 2015 msgid "" msgstr "" -"Project-Id-Version: Odoo Server 8.0\n" +"Project-Id-Version: knowledge (8.0)\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-01-15 11:04+0000\n" -"PO-Revision-Date: 2015-08-03 13:36+0200\n" +"POT-Creation-Date: 2015-12-08 08:32+0000\n" +"PO-Revision-Date: 2015-11-08 05:36+0000\n" "Last-Translator: Matjaž Mozetič \n" -"Language-Team: Slovenian \n" +"Language-Team: Slovenian (http://www.transifex.com/oca/OCA-knowledge-8-0/language/sl/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: \n" +"Content-Transfer-Encoding: \n" "Language: sl\n" -"X-Generator: Poedit 1.8.2\n" +"Plural-Forms: nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);\n" #. module: attachment_preview #. openerp-web -#: code:multiple:42 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 #, python-format msgid "Preview %s" msgstr "Predogled %s" diff --git a/attachment_preview/i18n/zh_CN.po b/attachment_preview/i18n/zh_CN.po new file mode 100644 index 00000000..bc2b5a19 --- /dev/null +++ b/attachment_preview/i18n/zh_CN.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * attachment_preview +# +# Translators: +# ITGeeker , 2017 +msgid "" +msgstr "" +"Project-Id-Version: knowledge (8.0)\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2016-07-31 02:38+0000\n" +"PO-Revision-Date: 2017-04-19 01:16+0000\n" +"Last-Translator: ITGeeker \n" +"Language-Team: Chinese (China) (http://www.transifex.com/oca/OCA-knowledge-8-0/language/zh_CN/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Language: zh_CN\n" +"Plural-Forms: nplurals=1; plural=0;\n" + +#. module: attachment_preview +#. openerp-web +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:184 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:226 +#: code:addons/attachment_preview/static/src/js/attachment_preview.js:228 +#, python-format +msgid "Preview %s" +msgstr "预览 %s" diff --git a/attachment_preview/model/__init__.py b/attachment_preview/model/__init__.py deleted file mode 100644 index 4466fe6d..00000000 --- a/attachment_preview/model/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2014 Therp BV (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -from . import ir_attachment diff --git a/attachment_preview/model/ir_attachment.py b/attachment_preview/model/ir_attachment.py deleted file mode 100644 index b00d3474..00000000 --- a/attachment_preview/model/ir_attachment.py +++ /dev/null @@ -1,68 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2014 Therp BV (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## -import collections -import os.path -import mimetypes -import base64 -from openerp.osv.orm import Model - - -class IrAttachment(Model): - _inherit = 'ir.attachment' - - def get_binary_extension( - self, cr, uid, model, ids, binary_field, filename_field=None, - context=None): - result = {} - for this in self.pool[model].browse( - cr, uid, - ids if isinstance(ids, collections.Iterable) else [ids], - context=context): - if not this.id: - result[this.id] = False - continue - extension = '' - if filename_field and this[filename_field]: - filename, extension = os.path.splitext(this[filename_field]) - if not this[binary_field]: - result[this.id] = False - continue - if not extension: - try: - import magic - ms = magic.open( - hasattr(magic, 'MAGIC_MIME_TYPE') and - magic.MAGIC_MIME_TYPE or magic.MAGIC_MIME) - ms.load() - mimetype = ms.buffer( - base64.b64decode(this[binary_field])) - except ImportError: - (mimetype, encoding) = mimetypes.guess_type( - 'data:;base64,' + this[binary_field], strict=False) - extension = mimetypes.guess_extension( - mimetype.split(';')[0], strict=False) - - result[this.id] = (extension or '').lstrip('.').lower() - return result if isinstance(ids, collections.Iterable) else result[ids] - - def get_attachment_extension(self, cr, uid, ids, context=None): - return self.get_binary_extension( - cr, uid, self._name, ids, 'datas', 'datas_fname', context=context) diff --git a/attachment_preview/models/__init__.py b/attachment_preview/models/__init__.py new file mode 100644 index 00000000..22aa81fb --- /dev/null +++ b/attachment_preview/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2014 Therp BV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import ir_attachment diff --git a/attachment_preview/models/ir_attachment.py b/attachment_preview/models/ir_attachment.py new file mode 100644 index 00000000..e1beaf55 --- /dev/null +++ b/attachment_preview/models/ir_attachment.py @@ -0,0 +1,75 @@ +# Copyright 2014 Therp BV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import collections +import logging +import mimetypes +import os.path + +from odoo import models, api + +_logger = logging.getLogger(__name__) + + +class IrAttachment(models.Model): + _inherit = 'ir.attachment' + + @api.model + def get_binary_extension(self, model, ids, binary_field, + filename_field=None): + result = {} + ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids] + + # First pass: load fields in bin_size mode to avoid loading big files + # unnecessarily. + if filename_field: + for this in self.env[model].with_context( + bin_size=True).browse(ids_to_browse): + if not this.id: + result[this.id] = False + continue + extension = '' + if this[filename_field]: + filename, extension = os.path.splitext( + this[filename_field]) + if this[binary_field] and extension: + result[this.id] = extension + _logger.debug('Got extension %s from filename %s', + extension, this[filename_field]) + # Second pass for all attachments which have to be loaded fully + # to get the extension from the content + ids_to_browse = [_id for _id in ids_to_browse if _id not in result] + for this in self.env[model].with_context( + bin_size=True).browse(ids_to_browse): + if this[binary_field] is None: + result[this.id] = False + continue + try: + import magic + if model == self._name and binary_field == 'datas'\ + and this.store_fname: + mimetype = magic.from_file( + this._full_path(this.store_fname), mime=True) + _logger.debug('Magic determined mimetype %s from file %s', + mimetype, this.store_fname) + else: + mimetype = magic.from_buffer( + this[binary_field], mime=True) + _logger.debug('Magic determined mimetype %s from buffer', + mimetype) + except ImportError: + (mimetype, encoding) = mimetypes.guess_type( + 'data:;base64,' + this[binary_field], strict=False) + _logger.debug('Mimetypes guessed type %s from buffer', + mimetype) + extension = mimetypes.guess_extension( + mimetype.split(';')[0], strict=False) + result[this.id] = extension + for _id in result: + result[_id] = (result[_id] or '').lstrip('.').lower() + return result if isinstance(ids, collections.Iterable) else result[ids] + + @api.model + def get_attachment_extension(self, ids): + return self.get_binary_extension( + self._name, ids, 'datas', 'datas_fname') diff --git a/attachment_preview/readme/CONTRIBUTORS.rst b/attachment_preview/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..81c9fb26 --- /dev/null +++ b/attachment_preview/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Holger Brunn +* Dennis Sluijk diff --git a/attachment_preview/readme/CREDITS.rst b/attachment_preview/readme/CREDITS.rst new file mode 100644 index 00000000..07bb3a4e --- /dev/null +++ b/attachment_preview/readme/CREDITS.rst @@ -0,0 +1,4 @@ +Addon icon +---------- + +* courtesy of http://commons.wikimedia.org/wiki/Crystal_Clear diff --git a/attachment_preview/readme/DESCRIPTION.rst b/attachment_preview/readme/DESCRIPTION.rst new file mode 100644 index 00000000..f1b31ab8 --- /dev/null +++ b/attachment_preview/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This addon allows to preview attachments supported by http://viewerjs.org. + +Currently, that's most Libreoffice files and PDFs. diff --git a/attachment_preview/readme/INSTALL.rst b/attachment_preview/readme/INSTALL.rst new file mode 100644 index 00000000..eb26fced --- /dev/null +++ b/attachment_preview/readme/INSTALL.rst @@ -0,0 +1,4 @@ +For filetype recognition, you'll get the best results by installing +``python-magic``: + +sudo apt-get install python-magic diff --git a/attachment_preview/readme/USAGE.rst b/attachment_preview/readme/USAGE.rst new file mode 100644 index 00000000..e1783956 --- /dev/null +++ b/attachment_preview/readme/USAGE.rst @@ -0,0 +1,2 @@ +The module adds a little print preview icon right of download links for +attachments or binary fields. diff --git a/attachment_preview/static/lib/ViewerJS/compatibility.js b/attachment_preview/static/lib/ViewerJS/compatibility.js index 967e312a..06f54bff 100644 --- a/attachment_preview/static/lib/ViewerJS/compatibility.js +++ b/attachment_preview/static/lib/ViewerJS/compatibility.js @@ -447,20 +447,10 @@ if (typeof PDFJS === 'undefined') { // Checks if navigator.language is supported (function checkNavigatorLanguage() { - if ('language' in navigator && - /^[a-z]+(-[A-Z]+)?$/.test(navigator.language)) { + if ('language' in navigator) { return; } - function formatLocale(locale) { - var split = locale.split(/[-_]/); - split[0] = split[0].toLowerCase(); - if (split.length > 1) { - split[1] = split[1].toUpperCase(); - } - return split.join('-'); - } - var language = navigator.language || navigator.userLanguage || 'en-US'; - PDFJS.locale = formatLocale(language); + PDFJS.locale = navigator.userLanguage || 'en-US'; })(); (function checkRangeRequests() { @@ -479,7 +469,10 @@ if (typeof PDFJS === 'undefined') { var regex = /Android\s[0-2][^\d]/; var isOldAndroid = regex.test(navigator.userAgent); - if (isSafari || isOldAndroid) { + // Range requests are broken in Chrome 39 and 40, https://crbug.com/442318 + var isChromeWithRangeBug = /Chrome\/(39|40)\./.test(navigator.userAgent); + + if (isSafari || isOldAndroid || isChromeWithRangeBug) { PDFJS.disableRange = true; PDFJS.disableStream = true; } @@ -572,3 +565,13 @@ if (typeof PDFJS === 'undefined') { PDFJS.maxCanvasPixels = 5242880; } })(); + +// Disable fullscreen support for certain problematic configurations. +// Support: IE11+ (when embedded). +(function checkFullscreenSupport() { + var isEmbeddedIE = (navigator.userAgent.indexOf('Trident') >= 0 && + window.parent !== window); + if (isEmbeddedIE) { + PDFJS.disableFullscreen = true; + } +})(); diff --git a/attachment_preview/static/lib/ViewerJS/index.html b/attachment_preview/static/lib/ViewerJS/index.html index c7496ceb..c79df466 100644 --- a/attachment_preview/static/lib/ViewerJS/index.html +++ b/attachment_preview/static/lib/ViewerJS/index.html @@ -39,37 +39,37 @@ along with ViewerJS. If not, see . + + + + diff --git a/attachment_preview/tests/__init__.py b/attachment_preview/tests/__init__.py new file mode 100644 index 00000000..7aec7faa --- /dev/null +++ b/attachment_preview/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 Onestein +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_attachment_preview diff --git a/attachment_preview/tests/test_attachment_preview.py b/attachment_preview/tests/test_attachment_preview.py new file mode 100644 index 00000000..17e731b7 --- /dev/null +++ b/attachment_preview/tests/test_attachment_preview.py @@ -0,0 +1,46 @@ +# Copyright 2018 Onestein +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import base64 +from odoo.tests.common import TransactionCase + + +class TestAttachmentPreview(TransactionCase): + def test_get_extension(self): + attachment = self.env['ir.attachment'].create({ + 'datas': base64.b64encode(b'from this, to that.'), + 'name': 'doc.txt', + 'datas_fname': 'doc.txt' + }) + attachment2 = self.env['ir.attachment'].create({ + 'datas': base64.b64encode(b'Png'), + 'name': 'image.png', + 'datas_fname': 'image.png' + }) + res = self.env['ir.attachment'].get_attachment_extension( + attachment.id + ) + self.assertEqual(res, 'txt') + + res = self.env['ir.attachment'].get_attachment_extension( + [attachment.id, attachment2.id] + ) + self.assertEqual(res[attachment.id], 'txt') + self.assertEqual(res[attachment2.id], 'png') + + res2 = self.env['ir.attachment'].get_binary_extension( + 'ir.attachment', + attachment.id, + 'datas' + ) + self.assertTrue(res2) + + module = self.env['ir.module.module'].search([]).filtered( + lambda m: m.icon_image + )[0] + res3 = self.env['ir.attachment'].get_binary_extension( + 'ir.module.module', + module.id, + 'icon_image' + ) + self.assertTrue(res3) diff --git a/attachment_preview/view/attachment_preview.xml b/attachment_preview/view/attachment_preview.xml deleted file mode 100644 index a2d6dcf1..00000000 --- a/attachment_preview/view/attachment_preview.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - -