From 82927b81b3ac13409e30b8c346d6757b48e5162c Mon Sep 17 00:00:00 2001 From: vancouver29 Date: Mon, 1 Aug 2022 13:54:06 +0200 Subject: [PATCH] [IMP] : black, isort, prettier --- attachment_preview/models/ir_attachment.py | 2 +- .../static/src/js/attachment_preview.js | 348 ++++++++++-------- .../static/src/js/viewerjs_tweaks.js | 8 +- .../static/src/scss/attachment_preview.scss | 2 +- .../static/src/xml/attachment_preview.xml | 50 ++- attachment_preview/templates/assets.xml | 19 +- .../tests/test_attachment_preview.py | 1 + .../odoo/addons/attachment_preview | 1 + setup/attachment_preview/setup.py | 6 + 9 files changed, 264 insertions(+), 173 deletions(-) create mode 120000 setup/attachment_preview/odoo/addons/attachment_preview create mode 100644 setup/attachment_preview/setup.py diff --git a/attachment_preview/models/ir_attachment.py b/attachment_preview/models/ir_attachment.py index 0b88b0e7..cad170cf 100644 --- a/attachment_preview/models/ir_attachment.py +++ b/attachment_preview/models/ir_attachment.py @@ -6,7 +6,7 @@ import logging import mimetypes import os.path -from odoo import models, api +from odoo import api, models _logger = logging.getLogger(__name__) diff --git a/attachment_preview/static/src/js/attachment_preview.js b/attachment_preview/static/src/js/attachment_preview.js index 85413877..78ed690b 100644 --- a/attachment_preview/static/src/js/attachment_preview.js +++ b/attachment_preview/static/src/js/attachment_preview.js @@ -1,41 +1,69 @@ /* Copyright 2014 Therp BV () * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ -odoo.define('attachment_preview', function (require) { - 'use strict'; +odoo.define("attachment_preview", function(require) { + "use strict"; - var core = require('web.core'); + var core = require("web.core"); var _t = core._t; var qweb = core.qweb; - var Chatter = require('mail.Chatter'); - var basic_fields = require('web.basic_fields'); - var FormRenderer = require('web.FormRenderer'); - var FormController = require('web.FormController'); - var Widget = require('web.Widget'); + var Chatter = require("mail.Chatter"); + var basic_fields = require("web.basic_fields"); + var FormRenderer = require("web.FormRenderer"); + var FormController = require("web.FormController"); + var Widget = require("web.Widget"); var AttachmentPreviewMixin = { - canPreview: function (extension) { - return $.inArray( - extension, - ['odt', 'odp', 'ods', 'fodt', 'pdf', 'ott', 'fodp', 'otp', - 'fods', 'ots', - ]) > -1; + canPreview: function(extension) { + return ( + $.inArray(extension, [ + "odt", + "odp", + "ods", + "fodt", + "pdf", + "ott", + "fodp", + "otp", + "fods", + "ots", + ]) > -1 + ); }, - getUrl: function (attachment_id, attachment_url, attachment_extension, attachment_title) { - var url = (window.location.origin || '') + - '/attachment_preview/static/lib/ViewerJS/index.html' + - '?type=' + encodeURIComponent(attachment_extension) + - '&title=' + encodeURIComponent(attachment_title) + - '#' + - attachment_url.replace(window.location.origin, ''); + getUrl: function( + attachment_id, + attachment_url, + attachment_extension, + attachment_title + ) { + var url = + (window.location.origin || "") + + "/attachment_preview/static/lib/ViewerJS/index.html" + + "?type=" + + encodeURIComponent(attachment_extension) + + "&title=" + + encodeURIComponent(attachment_title) + + "#" + + attachment_url.replace(window.location.origin, ""); return url; }, - showPreview: function (attachment_id, attachment_url, attachment_extension, attachment_title, split_screen) { - var url = this.getUrl(attachment_id, attachment_url, attachment_extension, attachment_title); + showPreview: function( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen + ) { + var url = this.getUrl( + attachment_id, + attachment_url, + attachment_extension, + attachment_title + ); if (split_screen) { - this.trigger_up('onAttachmentPreview', {url: url}); + this.trigger_up("onAttachmentPreview", {url: url}); } else { window.open(url); } @@ -45,30 +73,32 @@ odoo.define('attachment_preview', function (require) { Chatter.include(AttachmentPreviewMixin); Chatter.include({ events: _.extend({}, Chatter.prototype.events, { - 'click .o_attachment_preview': '_onPreviewAttachment', + "click .o_attachment_preview": "_onPreviewAttachment", }), previewableAttachments: null, - _openAttachmentBox: function () { + _openAttachmentBox: function() { var res = this._super.apply(this, arguments); - this.getPreviewableAttachments().done(function (atts) { - this.previewableAttachments = atts; - this.updatePreviewButtons(atts); - this.getParent().attachmentPreviewWidget.setAttachments(atts); - }.bind(this)); + this.getPreviewableAttachments().done( + function(atts) { + this.previewableAttachments = atts; + this.updatePreviewButtons(atts); + this.getParent().attachmentPreviewWidget.setAttachments(atts); + }.bind(this) + ); return res; }, - update: function () { + update: function() { var res = this._super.apply(this, arguments); var self = this; - if (this.getParent().$el.hasClass('attachment_preview')) { - this._fetchAttachments().done(function () { + if (this.getParent().$el.hasClass("attachment_preview")) { + this._fetchAttachments().done(function() { self._openAttachmentBox(); - self.getPreviewableAttachments().done(function (atts) { + self.getPreviewableAttachments().done(function(atts) { self.updatePreviewButtons(self.previewableAttachments); self.previewableAttachments = atts; self.getParent().attachmentPreviewWidget.setAttachments(atts); @@ -78,83 +108,106 @@ odoo.define('attachment_preview', function (require) { return res; }, - _onPreviewAttachment: function (event) { + _onPreviewAttachment: function(event) { event.preventDefault(); var self = this, $target = $(event.currentTarget), - split_screen = $target.attr('data-target') !== 'new', - attachment_id = parseInt($target.attr('data-id'), 10), - attachment_url = $target.attr('data-url'), - attachment_extension = $target.attr('data-extension'), - attachment_title = $target.attr('data-original-title'); + split_screen = $target.attr("data-target") !== "new", + attachment_id = parseInt($target.attr("data-id"), 10), + attachment_url = $target.attr("data-url"), + attachment_extension = $target.attr("data-extension"), + attachment_title = $target.attr("data-original-title"); if (attachment_extension) { - this.showPreview(attachment_id, attachment_url, attachment_extension, attachment_title, split_screen); + this.showPreview( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen + ); } else { this._rpc({ - model: 'ir.attachment', - method: 'get_attachment_extension', + model: "ir.attachment", + method: "get_attachment_extension", args: [attachment_id], - }).then(function (extension) { - self.showPreview(attachment_id, attachment_url, extension, null, split_screen); + }).then(function(extension) { + self.showPreview( + attachment_id, + attachment_url, + extension, + null, + split_screen + ); }); } }, - getPreviewableAttachments: function () { + getPreviewableAttachments: function() { var self = this; var deferred = $.Deferred(); - var $items = this.$el.find('.o_attachment_preview'); - var attachments = _.object($items.map(function () { - return parseInt($(this).attr('data-id'), 10); - }), $items.map(function () { - return { - url: $(this).attr('data-url'), - extension: $(this).attr('data-extension'), - title: $(this).attr('data-original-title'), - }; - })); + var $items = this.$el.find(".o_attachment_preview"); + var attachments = _.object( + $items.map(function() { + return parseInt($(this).attr("data-id"), 10); + }), + $items.map(function() { + return { + url: $(this).attr("data-url"), + extension: $(this).attr("data-extension"), + title: $(this).attr("data-original-title"), + }; + }) + ); this._rpc({ - model: 'ir.attachment', - method: 'get_attachment_extension', + model: "ir.attachment", + method: "get_attachment_extension", args: [ - _.map(_.keys(attachments), function (id) { + _.map(_.keys(attachments), function(id) { return parseInt(id, 10); }), ], - }).then(function (extensions) { - var reviewableAttachments = _.map(_.keys(_.pick(extensions, function (extension, id) { - return self.canPreview(extension); - })), function (id) { - return { - id: id, - url: attachments[id].url, - extension: extensions[id], - title: attachments[id].title, - previewUrl: self.getUrl( - id, - attachments[id].url, - extensions[id], - id + ' - ' + attachments[id].title + }).then( + function(extensions) { + var reviewableAttachments = _.map( + _.keys( + _.pick(extensions, function(extension, id) { + return self.canPreview(extension); + }) ), - }; - }); - deferred.resolve(reviewableAttachments); - }, function () { - deferred.reject(); - }); + function(id) { + return { + id: id, + url: attachments[id].url, + extension: extensions[id], + title: attachments[id].title, + previewUrl: self.getUrl( + id, + attachments[id].url, + extensions[id], + id + " - " + attachments[id].title + ), + }; + } + ); + deferred.resolve(reviewableAttachments); + }, + function() { + deferred.reject(); + } + ); return deferred.promise(); }, - updatePreviewButtons: function (previewableAttachments) { - this.$el.find('.o_attachment_preview').each(function () { + updatePreviewButtons: function(previewableAttachments) { + this.$el.find(".o_attachment_preview").each(function() { var $this = $(this); - var id = $this.attr('data-id'); + var id = $this.attr("data-id"); var att = _.findWhere(previewableAttachments, {id: id}); if (att) { - $this.attr('data-extension', att.extension); + $this.attr("data-extension", att.extension); } else { $this.remove(); } @@ -165,15 +218,15 @@ odoo.define('attachment_preview', function (require) { basic_fields.FieldBinaryFile.include(AttachmentPreviewMixin); basic_fields.FieldBinaryFile.include({ events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, { - 'click .fa-search': '_onPreview', + "click .fa-search": "_onPreview", }), - _renderReadonly: function () { + _renderReadonly: function() { var self = this; this._super.apply(this, arguments); if (this.recordData.id) { - this._getBinaryExtension().done(function (extension) { + this._getBinaryExtension().done(function(extension) { if (self.canPreview(extension)) { self._renderPreviewButton(extension); } @@ -181,39 +234,37 @@ odoo.define('attachment_preview', function (require) { } }, - _renderPreviewButton: function (extension) { + _renderPreviewButton: function(extension) { this.$previewBtn = $(""); - this.$previewBtn.addClass('fa fa-search mr-2'); - this.$previewBtn.attr('href', 'javascript:void(0)'); - this.$previewBtn.attr('title', _.str.sprintf(_t('Preview %s'), this.field.string)); - this.$previewBtn.attr('data-extension', extension); - this.$el.find('.fa-download').before(this.$previewBtn); + this.$previewBtn.addClass("fa fa-search mr-2"); + this.$previewBtn.attr("href", "javascript:void(0)"); + this.$previewBtn.attr( + "title", + _.str.sprintf(_t("Preview %s"), this.field.string) + ); + this.$previewBtn.attr("data-extension", extension); + this.$el.find(".fa-download").before(this.$previewBtn); }, - _getBinaryExtension: function () { + _getBinaryExtension: function() { return this._rpc({ - model: 'ir.attachment', - method: 'get_binary_extension', - args: [ - this.model, - this.recordData.id, - this.name, - this.attrs.filename, - ], + model: "ir.attachment", + method: "get_binary_extension", + args: [this.model, this.recordData.id, this.name, this.attrs.filename], }); }, - _onPreview: function (event) { + _onPreview: function(event) { this.showPreview( null, _.str.sprintf( - '/web/content?model=%s&field=%s&id=%d', + "/web/content?model=%s&field=%s&id=%d", this.model, this.name, this.recordData.id ), - $(event.currentTarget).attr('data-extension'), - _.str.sprintf(_t('Preview %s'), this.field.string), + $(event.currentTarget).attr("data-extension"), + _.str.sprintf(_t("Preview %s"), this.field.string), false ); event.stopPropagation(); @@ -221,38 +272,38 @@ odoo.define('attachment_preview', function (require) { }); var AttachmentPreviewWidget = Widget.extend({ - template: 'attachment_preview.AttachmentPreviewWidget', + template: "attachment_preview.AttachmentPreviewWidget", activeIndex: 0, attachments: null, events: { - 'click .attachment_preview_close': '_onCloseClick', - 'click .attachment_preview_previous': '_onPreviousClick', - 'click .attachment_preview_next': '_onNextClick', - 'click .attachment_preview_popout': '_onPopoutClick', + "click .attachment_preview_close": "_onCloseClick", + "click .attachment_preview_previous": "_onPreviousClick", + "click .attachment_preview_next": "_onNextClick", + "click .attachment_preview_popout": "_onPopoutClick", }, - start: function () { + start: function() { var res = this._super.apply(this, arguments); - this.$overlay = this.$el.find('.attachment_preview_overlay'); - this.$iframe = this.$el.find('.attachment_preview_iframe'); - this.$current = this.$el.find('.attachment_preview_current'); + this.$overlay = this.$el.find(".attachment_preview_overlay"); + this.$iframe = this.$el.find(".attachment_preview_iframe"); + this.$current = this.$el.find(".attachment_preview_current"); return res; }, - _onCloseClick: function () { + _onCloseClick: function() { this.hide(); }, - _onPreviousClick: function () { + _onPreviousClick: function() { this.previous(); }, - _onNextClick: function () { + _onNextClick: function() { this.next(); }, - _onPopoutClick: function () { + _onPopoutClick: function() { if (!this.attachments[this.activeIndex]) { return; } @@ -260,7 +311,7 @@ odoo.define('attachment_preview', function (require) { window.open(this.attachments[this.activeIndex].previewUrl); }, - next: function () { + next: function() { var index = this.activeIndex + 1; if (index >= this.attachments.length) { index = 0; @@ -270,7 +321,7 @@ odoo.define('attachment_preview', function (require) { this.loadPreview(); }, - previous: function () { + previous: function() { var index = this.activeIndex - 1; if (index < 0) { index = this.attachments.length - 1; @@ -280,32 +331,36 @@ odoo.define('attachment_preview', function (require) { this.loadPreview(); }, - show: function () { - this.$el.removeClass('d-none'); - this.trigger('shown'); + show: function() { + this.$el.removeClass("d-none"); + this.trigger("shown"); }, - hide: function () { - this.$el.addClass('d-none'); - this.trigger('hidden'); + hide: function() { + this.$el.addClass("d-none"); + this.trigger("hidden"); }, - updatePaginator: function () { - var value = _.str.sprintf('%s / %s', this.activeIndex + 1, this.attachments.length); + updatePaginator: function() { + var value = _.str.sprintf( + "%s / %s", + this.activeIndex + 1, + this.attachments.length + ); this.$current.html(value); }, - loadPreview: function () { + loadPreview: function() { if (this.attachments.length === 0) { - this.$iframe.attr('src', 'about:blank'); + this.$iframe.attr("src", "about:blank"); return; } var att = this.attachments[this.activeIndex]; - this.$iframe.attr('src', att.previewUrl); + this.$iframe.attr("src", att.previewUrl); }, - setAttachments: function (attachments) { + setAttachments: function(attachments) { this.attachments = attachments; this.activeIndex = 0; this.updatePaginator(); @@ -315,31 +370,35 @@ odoo.define('attachment_preview', function (require) { FormRenderer.include({ custom_events: _.extend({}, FormRenderer.prototype.custom_events, { - onAttachmentPreview: '_onAttachmentPreview', + onAttachmentPreview: "_onAttachmentPreview", }), attachmentPreviewWidget: null, - init: function () { + init: function() { var res = this._super.apply(this, arguments); this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); - this.attachmentPreviewWidget.on('hidden', this, this._attachmentPreviewWidgetHidden); + this.attachmentPreviewWidget.on( + "hidden", + this, + this._attachmentPreviewWidgetHidden + ); return res; }, - start: function () { + start: function() { var self = this; - return this._super.apply(this, arguments).then(function () { + return this._super.apply(this, arguments).then(function() { return self.attachmentPreviewWidget.insertAfter(self.$el); }); }, - _attachmentPreviewWidgetHidden: function () { - this.$el.removeClass('attachment_preview'); + _attachmentPreviewWidgetHidden: function() { + this.$el.removeClass("attachment_preview"); }, - showAttachmentPreviewWidget: function () { - this.$el.addClass('attachment_preview'); + showAttachmentPreviewWidget: function() { + this.$el.addClass("attachment_preview"); this.attachmentPreviewWidget.setAttachments( this.chatter.previewableAttachments @@ -347,17 +406,16 @@ odoo.define('attachment_preview', function (require) { this.attachmentPreviewWidget.show(); }, - on_detach_callback: function () { + on_detach_callback: function() { this.attachmentPreviewWidget.hide(); return this._super.apply(this, arguments); }, - _onAttachmentPreview: function (event) { + _onAttachmentPreview: function(event) { this.showAttachmentPreviewWidget(); }, }); - return { AttachmentPreviewMixin: AttachmentPreviewMixin, AttachmentPreviewWidget: AttachmentPreviewWidget, diff --git a/attachment_preview/static/src/js/viewerjs_tweaks.js b/attachment_preview/static/src/js/viewerjs_tweaks.js index cd8a0cdc..1e166df2 100644 --- a/attachment_preview/static/src/js/viewerjs_tweaks.js +++ b/attachment_preview/static/src/js/viewerjs_tweaks.js @@ -3,12 +3,12 @@ // This file contains tweaks for viewerjs itself and is not meant to be run in // OpenERP's context -(function (original_Viewer) { - window.Viewer = function (plugin, parameters) { +(function(original_Viewer) { + window.Viewer = function(plugin, parameters) { if (!plugin) { // eslint-disable-next-line no-alert - alert('Unsupported file type'); + alert("Unsupported file type"); } return original_Viewer(plugin, parameters); }; -}(window.Viewer)); +})(window.Viewer); diff --git a/attachment_preview/static/src/scss/attachment_preview.scss b/attachment_preview/static/src/scss/attachment_preview.scss index 9c2bf904..354cabe8 100644 --- a/attachment_preview/static/src/scss/attachment_preview.scss +++ b/attachment_preview/static/src/scss/attachment_preview.scss @@ -44,7 +44,7 @@ padding: 4px; float: left; width: 100%; - background-color: #FFF; + background-color: #fff; border-left: 1px solid $gray-800; button { diff --git a/attachment_preview/static/src/xml/attachment_preview.xml b/attachment_preview/static/src/xml/attachment_preview.xml index da3f10f5..a505f885 100644 --- a/attachment_preview/static/src/xml/attachment_preview.xml +++ b/attachment_preview/static/src/xml/attachment_preview.xml @@ -1,19 +1,23 @@ - + - + - + @@ -21,15 +25,25 @@
- - - + + +
- + - +
- +