diff --git a/attachment_preview/README.rst b/attachment_preview/README.rst index 6dcebbd4..74eecb02 100644 --- a/attachment_preview/README.rst +++ b/attachment_preview/README.rst @@ -12,6 +12,8 @@ For filetype recognition, you'll get the best results by installing ``python-magic``:: sudo apt-get install python-magic +or +pip install file-magic Usage ===== @@ -47,6 +49,8 @@ Contributors ------------ * Holger Brunn +* Carlos QuirĂ³s +* Glen Sojo Maintainer ---------- diff --git a/attachment_preview/__openerp__.py b/attachment_preview/__openerp__.py index 01012f0d..509ef44b 100644 --- a/attachment_preview/__openerp__.py +++ b/attachment_preview/__openerp__.py @@ -38,9 +38,9 @@ "test": [ ], "auto_install": False, - 'installable': False, + 'installable': True, "application": False, "external_dependencies": { - 'python': [], + 'python': ['file-magic'], }, } diff --git a/attachment_preview/model/ir_attachment.py b/attachment_preview/model/ir_attachment.py index b00d3474..0c84a8b3 100644 --- a/attachment_preview/model/ir_attachment.py +++ b/attachment_preview/model/ir_attachment.py @@ -22,10 +22,11 @@ import collections import os.path import mimetypes import base64 -from openerp.osv.orm import Model +from openerp import models -class IrAttachment(Model): +class IrAttachment(models.Model): + _inherit = 'ir.attachment' def get_binary_extension( @@ -59,8 +60,9 @@ class IrAttachment(Model): 'data:;base64,' + this[binary_field], strict=False) extension = mimetypes.guess_extension( mimetype.split(';')[0], strict=False) - - result[this.id] = (extension or '').lstrip('.').lower() + docdata = {1: this[binary_field], + 0: (extension or '').lstrip('.').lower()} + result[this.id] = docdata return result if isinstance(ids, collections.Iterable) else result[ids] def get_attachment_extension(self, cr, uid, ids, context=None): diff --git a/attachment_preview/static/lib/ViewerJS/index.html b/attachment_preview/static/lib/ViewerJS/index.html index c79df466..2eb1fd47 100644 --- a/attachment_preview/static/lib/ViewerJS/index.html +++ b/attachment_preview/static/lib/ViewerJS/index.html @@ -74,8 +74,6 @@ c;a?(e.title||(e.title=a.replace(/^.*[\\\/]/,"")),e.documentUrl=a,b(a,function(b //]]> - - diff --git a/attachment_preview/static/src/css/attachment_preview.css b/attachment_preview/static/src/css/attachment_preview.css index 90f25f6e..40fbfc5f 100644 --- a/attachment_preview/static/src/css/attachment_preview.css +++ b/attachment_preview/static/src/css/attachment_preview.css @@ -1,11 +1,16 @@ -.openerp .oe_sidebar .oe_dropdown_menu li .oe-sidebar-attachment-preview +/*.openerp .oe_sidebar .oe_dropdown_menu li .oe_sidebar_attachment_preview { position: absolute; right: 18px; top: 0px; display: none; +}*/ +.openerp .oe-control-panel .oe-cp-sidebar .oe_sidebar_attachment_preview { + padding: 0; + position: absolute; + right: 24px; } -.openerp .oe_sidebar .oe_dropdown_menu li:hover .oe-sidebar-attachment-preview +/*.openerp .oe_sidebar .oe_dropdown_menu li:hover .oe-sidebar-attachment-preview { display: inherit; } @@ -18,9 +23,16 @@ -moz-border-radius: 2px; -webkit-border-radius: 2px; border-radius: 2px; -} +}*/ .openerp .oe-binary-preview { cursor: pointer; vertical-align: middle; } + + +.openerp .oe-control-panel .oe-cp-sidebar .oe_sidebar_delete_item { + padding: 0; + position: absolute; + right: 10px; +} \ No newline at end of file diff --git a/attachment_preview/static/src/js/attachment_preview.js b/attachment_preview/static/src/js/attachment_preview.js index 2f3d7b46..38ba3dd5 100644 --- a/attachment_preview/static/src/js/attachment_preview.js +++ b/attachment_preview/static/src/js/attachment_preview.js @@ -19,13 +19,18 @@ // //############################################################################ -openerp.attachment_preview = function(instance) -{ - var _t = instance.web._t; - openerp.attachment_preview.show_preview = function( - attachment_id, attachment_url, attachment_extension, - attachment_title) - { +odoo.define('attachment_preview', function (require) { + "use strict"; + + var core = require('web.core'); + var session = require('web.session'); + var Model = require('web.Model'); + var Sidebar = require('web.Sidebar'); + var ListView = require('web.ListView'); + + var _t = core._t; + + function show_preview(attachment_id, attachment_url, attachment_extension, attachment_title) { var url = (window.location.origin || '') + '/attachment_preview/static/lib/ViewerJS/index.html' + '?type=' + encodeURIComponent(attachment_extension) + @@ -34,8 +39,8 @@ openerp.attachment_preview = function(instance) attachment_url.replace(window.location.origin, '') window.open(url); }; - openerp.attachment_preview.can_preview = function(extension) - { + + function can_preview(extension) { return jQuery.inArray( extension, [ @@ -43,18 +48,18 @@ openerp.attachment_preview = function(instance) 'fods', 'ots' ]) > -1; }; - instance.web.Sidebar.include( - { - on_attachments_loaded: function(attachments) - { - var result = this._super.apply(this, arguments); - this.$el.find('.oe-sidebar-attachment-preview') + + Sidebar.include({ + on_attachments_loaded: function(attachments) { + this._super(attachments); + + this.$('.oe_sidebar_attachment_preview') .click(this.on_attachment_preview); this.update_preview_buttons(); - return result; + // return _super.apply(self, arguments); }, - on_attachment_preview: function(e) - { + + on_attachment_preview: function(e){ e.preventDefault(); e.stopPropagation(); var self = this, @@ -65,28 +70,28 @@ openerp.attachment_preview = function(instance) attachment_title = $target.attr('data-original-title'); if(attachment_extension) { - openerp.attachment_preview.show_preview( + show_preview( attachment_id, attachment_url, attachment_extension, attachment_title); } else { - (new instance.web.Model('ir.attachment')).call( + (new Model('ir.attachment')).call( 'get_attachment_extension', [attachment_id], {}) .then(function(extension) { - openerp.attachment_preview.show_preview( - attachment_id, attachment_url, extension); + show_preview( + attachment_id, attachment_url, extension[0], attachment_title); }); } }, - update_preview_buttons: function() - { + + update_preview_buttons: function(){ var self = this; - return (new instance.web.Model('ir.attachment')).call( + return (new Model('ir.attachment')).call( 'get_attachment_extension', [ - this.$el.find('.oe-sidebar-attachment-preview') + this.$('span.oe_sidebar_attachment_preview') .map(function() { return parseInt(jQuery(this).attr('data-id')); @@ -99,11 +104,11 @@ openerp.attachment_preview = function(instance) _(extensions).each(function(extension, id) { var $element = jQuery( - 'a.oe-sidebar-attachment-preview[data-id="' + 'span.oe_sidebar_attachment_preview[data-id="' + id + '"]'); - if(openerp.attachment_preview.can_preview(extension)) + if(can_preview(extension[0])) { - $element.attr('data-extension', extension); + $element.attr('data-extension', extension[0]); } else { @@ -113,15 +118,15 @@ openerp.attachment_preview = function(instance) }); }, }); - instance.web.ListView.include( + + ListView.include( { - reload_content: function() - { + reload_content: function() { var deferred = this._super.apply(this, arguments), - self = this; + self = this; deferred.then(function() { - var $elements = self.$el.find('.oe-binary-preview'); + var $elements = self.$('.oe_binary_preview'); if(!$elements.length) { return; @@ -132,13 +137,13 @@ openerp.attachment_preview = function(instance) var $target = jQuery(e.currentTarget), attachment_id = parseInt($target.attr('data-id')), attachment_extension = $target.attr('data-extension'); - openerp.attachment_preview.show_preview( + show_preview( attachment_id, $target.siblings('a').attr('href'), attachment_extension, $target.attr('alt')); }); - return (new instance.web.Model('ir.attachment')).call( + return (new Model('ir.attachment')).call( 'get_binary_extension', [ $elements.attr('data-model'), @@ -158,9 +163,9 @@ openerp.attachment_preview = function(instance) { var $element = $elements.filter( '[data-id="' + id + '"]'); - if(openerp.attachment_preview.can_preview(extension)) + if(can_preview(extension[0])) { - $element.attr('data-extension', extension); + $element.attr('data-extension', extension[0]); } else { @@ -172,32 +177,29 @@ openerp.attachment_preview = function(instance) return deferred; } }); - instance.web.list.Binary.include( - { - _format: function (row_data, options) + + var ColumnBinary = core.list_widget_registry.get('field.binary').include({ + _format: function (row_data, options) { var link = this._super.apply(this, arguments); - link += _.template( - '<%-preview_text%>', - { - preview_id: options.id, - preview_text: _.str.sprintf(_t('Preview %s'), this.string), - preview_model: options.model, - preview_field: this.id, - preview_filename: this.filename || '', - }); + link += ( + ''); return link; } }); - instance.web.form.FieldBinaryFile.include( - { + + var FieldBinaryFile = core.form_widget_registry.get('binary').include({ render_value: function() { - this._super.apply(this, arguments); + this._super(); if(this.get("effective_readonly") && this.get('value')) { var self = this; - (new instance.web.Model('ir.attachment')).call( + (new Model('ir.attachment')).call( 'get_binary_extension', [ this.view.dataset.model, this.view.datarecord.id ? [this.view.datarecord.id] : [], @@ -209,22 +211,21 @@ openerp.attachment_preview = function(instance) { _(extensions).each(function(extension) { - var $element = self.$el.find('.oe-binary-preview'); - if(openerp.attachment_preview.can_preview(extension)) + var $element = self.$('.oe_binary_preview'); + $element.unbind( "click" ); + if(can_preview(extension[0])) { $element.click(function() { - openerp.attachment_preview.show_preview( + show_preview( null, - _.str.sprintf( - '/web/binary/saveas?session_id=%s&model=%s&field=%s&id=%d', - instance.session.session_id, - self.view.dataset.model, - self.name, - self.view.datarecord.id), - extension, + + ('data:application/'+extension[0]+';base64,'+ + extension[1]), + extension[0], _.str.sprintf(_t('Preview %s'), self.field.string)); }); + $element.attr('title', _.str.sprintf(_t('Preview %s'), self.field.string)); } else @@ -236,8 +237,8 @@ openerp.attachment_preview = function(instance) } else { - this.$el.find('.oe-binary-preview').remove(); + this.$('.oe_binary_preview').remove(); }; }, }); -} +}); diff --git a/attachment_preview/static/src/xml/attachment_preview.xml b/attachment_preview/static/src/xml/attachment_preview.xml index c72dc3bb..70c797cf 100644 --- a/attachment_preview/static/src/xml/attachment_preview.xml +++ b/attachment_preview/static/src/xml/attachment_preview.xml @@ -1,15 +1,17 @@ - - - - + + + + - + + +