mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-18 13:06:34 -06:00
[MIG] attachment_preview: Migration to 13.0
This commit is contained in:
parent
c6e0419f98
commit
6203fdd7bf
@ -3,13 +3,15 @@
|
|||||||
|
|
||||||
{
|
{
|
||||||
"name": "Preview attachments",
|
"name": "Preview attachments",
|
||||||
"version": "12.0.1.0.2",
|
"version": "13.0.1.0.0",
|
||||||
"author": "Therp BV," "Onestein," "Odoo Community Association (OCA)",
|
"author": "Therp BV," "Onestein," "Odoo Community Association (OCA)",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"summary": "Preview attachments supported by Viewer.js",
|
"summary": "Preview attachments supported by Viewer.js",
|
||||||
"category": "Knowledge Management",
|
"category": "Knowledge Management",
|
||||||
"depends": ["web", "mail"],
|
"depends": ["web", "mail"],
|
||||||
"data": ["templates/assets.xml",],
|
"data": ["templates/assets.xml"],
|
||||||
"qweb": ["static/src/xml/attachment_preview.xml",],
|
"qweb": ["static/src/xml/attachment_preview.xml"],
|
||||||
|
"website": "https://github.com/OCA/knowledge",
|
||||||
|
"external_dependencies": {"python": ["python-magic"]},
|
||||||
"installable": True,
|
"installable": True,
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,7 @@ class IrAttachment(models.Model):
|
|||||||
for this in (
|
for this in (
|
||||||
self.env[model].with_context(bin_size=True).browse(ids_to_browse)
|
self.env[model].with_context(bin_size=True).browse(ids_to_browse)
|
||||||
):
|
):
|
||||||
if not this.id:
|
|
||||||
result[this.id] = False
|
result[this.id] = False
|
||||||
continue
|
|
||||||
extension = ""
|
extension = ""
|
||||||
if this[filename_field]:
|
if this[filename_field]:
|
||||||
filename, extension = os.path.splitext(this[filename_field])
|
filename, extension = os.path.splitext(this[filename_field])
|
||||||
@ -42,9 +40,7 @@ class IrAttachment(models.Model):
|
|||||||
# to get the extension from the content
|
# to get the extension from the content
|
||||||
ids_to_browse = [_id for _id in ids_to_browse if _id not in result]
|
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):
|
for this in self.env[model].with_context(bin_size=True).browse(ids_to_browse):
|
||||||
if not this[binary_field]:
|
|
||||||
result[this.id] = False
|
result[this.id] = False
|
||||||
continue
|
|
||||||
try:
|
try:
|
||||||
import magic
|
import magic
|
||||||
|
|
||||||
@ -62,7 +58,7 @@ class IrAttachment(models.Model):
|
|||||||
_logger.debug("Magic determined mimetype %s from buffer", mimetype)
|
_logger.debug("Magic determined mimetype %s from buffer", mimetype)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
(mimetype, encoding) = mimetypes.guess_type(
|
(mimetype, encoding) = mimetypes.guess_type(
|
||||||
"data:;base64," + this[binary_field], strict=False
|
"data:;base64," + this[binary_field].decode("utf-8"), strict=False
|
||||||
)
|
)
|
||||||
_logger.debug("Mimetypes guessed type %s from buffer", mimetype)
|
_logger.debug("Mimetypes guessed type %s from buffer", mimetype)
|
||||||
extension = mimetypes.guess_extension(mimetype.split(";")[0], strict=False)
|
extension = mimetypes.guess_extension(mimetype.split(";")[0], strict=False)
|
||||||
@ -73,4 +69,4 @@ class IrAttachment(models.Model):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_attachment_extension(self, ids):
|
def get_attachment_extension(self, ids):
|
||||||
return self.get_binary_extension(self._name, ids, "datas", "datas_fname")
|
return self.get_binary_extension(self._name, ids, "datas", "name")
|
||||||
|
@ -6,13 +6,15 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
|
|
||||||
var core = require("web.core");
|
var core = require("web.core");
|
||||||
var _t = core._t;
|
var _t = core._t;
|
||||||
var qweb = core.qweb;
|
|
||||||
var Chatter = require("mail.Chatter");
|
var Chatter = require("mail.Chatter");
|
||||||
var basic_fields = require("web.basic_fields");
|
var basic_fields = require("web.basic_fields");
|
||||||
var FormRenderer = require("web.FormRenderer");
|
var FormRenderer = require("web.FormRenderer");
|
||||||
var FormController = require("web.FormController");
|
|
||||||
var Widget = require("web.Widget");
|
var Widget = require("web.Widget");
|
||||||
|
|
||||||
|
var active_attachment_id = 0;
|
||||||
|
var active_attachment_index = 0;
|
||||||
|
var first_click = true;
|
||||||
|
|
||||||
var AttachmentPreviewMixin = {
|
var AttachmentPreviewMixin = {
|
||||||
canPreview: function(extension) {
|
canPreview: function(extension) {
|
||||||
return (
|
return (
|
||||||
@ -96,7 +98,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
var res = this._super.apply(this, arguments);
|
var res = this._super.apply(this, arguments);
|
||||||
var self = this;
|
var self = this;
|
||||||
if (this.getParent().$el.hasClass("attachment_preview")) {
|
if (this.getParent().$el.hasClass("attachment_preview")) {
|
||||||
this._fetchAttachments().done(function() {
|
this._fetchAttachments().then(function() {
|
||||||
self._openAttachmentBox();
|
self._openAttachmentBox();
|
||||||
self.getPreviewableAttachments().done(function(atts) {
|
self.getPreviewableAttachments().done(function(atts) {
|
||||||
self.updatePreviewButtons(self.previewableAttachments);
|
self.updatePreviewButtons(self.previewableAttachments);
|
||||||
@ -118,6 +120,11 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
attachment_extension = $target.attr("data-extension"),
|
attachment_extension = $target.attr("data-extension"),
|
||||||
attachment_title = $target.attr("data-original-title");
|
attachment_title = $target.attr("data-original-title");
|
||||||
|
|
||||||
|
if (attachment_id !== active_attachment_id) {
|
||||||
|
active_attachment_id = attachment_id;
|
||||||
|
first_click = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (attachment_extension) {
|
if (attachment_extension) {
|
||||||
this.showPreview(
|
this.showPreview(
|
||||||
attachment_id,
|
attachment_id,
|
||||||
@ -147,16 +154,15 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
var self = this;
|
var self = this;
|
||||||
var deferred = $.Deferred();
|
var deferred = $.Deferred();
|
||||||
|
|
||||||
var $items = this.$el.find(".o_attachment_preview");
|
|
||||||
var attachments = _.object(
|
var attachments = _.object(
|
||||||
$items.map(function() {
|
this.attachments.map(function(attachment) {
|
||||||
return parseInt($(this).attr("data-id"), 10);
|
return attachment.id;
|
||||||
}),
|
}),
|
||||||
$items.map(function() {
|
this.attachments.map(function(attachment) {
|
||||||
return {
|
return {
|
||||||
url: $(this).attr("data-url"),
|
url: attachment.url,
|
||||||
extension: $(this).attr("data-extension"),
|
extension: attachment.filename.split(".")[1],
|
||||||
title: $(this).attr("data-original-title"),
|
title: attachment.name,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -173,7 +179,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
function(extensions) {
|
function(extensions) {
|
||||||
var reviewableAttachments = _.map(
|
var reviewableAttachments = _.map(
|
||||||
_.keys(
|
_.keys(
|
||||||
_.pick(extensions, function(extension, id) {
|
_.pick(extensions, function(extension) {
|
||||||
return self.canPreview(extension);
|
return self.canPreview(extension);
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
@ -226,7 +232,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
this._super.apply(this, arguments);
|
this._super.apply(this, arguments);
|
||||||
|
|
||||||
if (this.recordData.id) {
|
if (this.recordData.id) {
|
||||||
this._getBinaryExtension().done(function(extension) {
|
this._getBinaryExtension().then(function(extension) {
|
||||||
if (self.canPreview(extension)) {
|
if (self.canPreview(extension)) {
|
||||||
self._renderPreviewButton(extension);
|
self._renderPreviewButton(extension);
|
||||||
}
|
}
|
||||||
@ -284,6 +290,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
start: function() {
|
start: function() {
|
||||||
|
first_click = true;
|
||||||
var res = this._super.apply(this, arguments);
|
var res = this._super.apply(this, arguments);
|
||||||
this.$overlay = this.$el.find(".attachment_preview_overlay");
|
this.$overlay = this.$el.find(".attachment_preview_overlay");
|
||||||
this.$iframe = this.$el.find(".attachment_preview_iframe");
|
this.$iframe = this.$el.find(".attachment_preview_iframe");
|
||||||
@ -337,6 +344,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
hide: function() {
|
hide: function() {
|
||||||
|
first_click = true;
|
||||||
this.$el.addClass("d-none");
|
this.$el.addClass("d-none");
|
||||||
this.trigger("hidden");
|
this.trigger("hidden");
|
||||||
},
|
},
|
||||||
@ -356,7 +364,18 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var att = this.attachments[this.activeIndex];
|
if (first_click) {
|
||||||
|
for (var i in this.attachments) {
|
||||||
|
if (this.attachments[i].id === active_attachment_id.toString()) {
|
||||||
|
active_attachment_index = i;
|
||||||
|
first_click = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
active_attachment_index = this.activeIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
var att = this.attachments[active_attachment_index];
|
||||||
this.$iframe.attr("src", att.previewUrl);
|
this.$iframe.attr("src", att.previewUrl);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -411,7 +430,7 @@ odoo.define("attachment_preview", function(require) {
|
|||||||
return this._super.apply(this, arguments);
|
return this._super.apply(this, arguments);
|
||||||
},
|
},
|
||||||
|
|
||||||
_onAttachmentPreview: function(event) {
|
_onAttachmentPreview: function() {
|
||||||
this.showAttachmentPreviewWidget();
|
this.showAttachmentPreviewWidget();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
// This file contains tweaks for viewerjs itself and is not meant to be run in
|
// This file contains tweaks for viewerjs itself and is not meant to be run in
|
||||||
// OpenERP's context
|
// OpenERP's context
|
||||||
(function(original_Viewer) {
|
(function(original_Viewer) {
|
||||||
|
"use strict";
|
||||||
window.Viewer = function(plugin, parameters) {
|
window.Viewer = function(plugin, parameters) {
|
||||||
if (!plugin) {
|
if (!plugin) {
|
||||||
// eslint-disable-next-line no-alert
|
// eslint-disable-next-line no-alert
|
||||||
|
@ -9,18 +9,10 @@ from odoo.tests.common import TransactionCase
|
|||||||
class TestAttachmentPreview(TransactionCase):
|
class TestAttachmentPreview(TransactionCase):
|
||||||
def test_get_extension(self):
|
def test_get_extension(self):
|
||||||
attachment = self.env["ir.attachment"].create(
|
attachment = self.env["ir.attachment"].create(
|
||||||
{
|
{"datas": base64.b64encode(b"from this, to that."), "name": "doc.txt"}
|
||||||
"datas": base64.b64encode(b"from this, to that."),
|
|
||||||
"name": "doc.txt",
|
|
||||||
"datas_fname": "doc.txt",
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
attachment2 = self.env["ir.attachment"].create(
|
attachment2 = self.env["ir.attachment"].create(
|
||||||
{
|
{"datas": base64.b64encode(b"Png"), "name": "image.png"}
|
||||||
"datas": base64.b64encode(b"Png"),
|
|
||||||
"name": "image.png",
|
|
||||||
"datas_fname": "image.png",
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
res = self.env["ir.attachment"].get_attachment_extension(attachment.id)
|
res = self.env["ir.attachment"].get_attachment_extension(attachment.id)
|
||||||
self.assertEqual(res, "txt")
|
self.assertEqual(res, "txt")
|
||||||
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# generated from manifests external_dependencies
|
||||||
|
python-magic
|
Loading…
Reference in New Issue
Block a user