[FIX] - attachment_preview: Migrated the module to 9.0 version and fixed some errors displaying the attachments files.

This commit is contained in:
Carlos Quiros 2016-10-19 11:31:17 -06:00
parent 54499e0ed7
commit 32874e128b
7 changed files with 101 additions and 82 deletions

View File

@ -12,6 +12,8 @@ For filetype recognition, you'll get the best results by installing
``python-magic``:: ``python-magic``::
sudo apt-get install python-magic sudo apt-get install python-magic
or
pip install file-magic
Usage Usage
===== =====
@ -47,6 +49,8 @@ Contributors
------------ ------------
* Holger Brunn <hbrunn@therp.nl> * Holger Brunn <hbrunn@therp.nl>
* Carlos Quirós <carlos.quiros@clearcorp.co.cr>
* Glen Sojo <glen.sojo@clearcorp.co.cr>
Maintainer Maintainer
---------- ----------

View File

@ -38,9 +38,9 @@
"test": [ "test": [
], ],
"auto_install": False, "auto_install": False,
'installable': False, 'installable': True,
"application": False, "application": False,
"external_dependencies": { "external_dependencies": {
'python': [], 'python': ['file-magic'],
}, },
} }

View File

@ -22,10 +22,11 @@ import collections
import os.path import os.path
import mimetypes import mimetypes
import base64 import base64
from openerp.osv.orm import Model from openerp import models
class IrAttachment(Model): class IrAttachment(models.Model):
_inherit = 'ir.attachment' _inherit = 'ir.attachment'
def get_binary_extension( def get_binary_extension(
@ -59,8 +60,9 @@ class IrAttachment(Model):
'data:;base64,' + this[binary_field], strict=False) 'data:;base64,' + this[binary_field], strict=False)
extension = mimetypes.guess_extension( extension = mimetypes.guess_extension(
mimetype.split(';')[0], strict=False) mimetype.split(';')[0], strict=False)
docdata = {1: this[binary_field],
result[this.id] = (extension or '').lstrip('.').lower() 0: (extension or '').lstrip('.').lower()}
result[this.id] = docdata
return result if isinstance(ids, collections.Iterable) else result[ids] return result if isinstance(ids, collections.Iterable) else result[ids]
def get_attachment_extension(self, cr, uid, ids, context=None): def get_attachment_extension(self, cr, uid, ids, context=None):

View File

@ -74,8 +74,6 @@ c;a?(e.title||(e.title=a.replace(/^.*[\\\/]/,"")),e.documentUrl=a,b(a,function(b
//]]> //]]>
</script> </script>
<!-- load some small tweaks for the Odoo integration /-->
<script src="../../src/js/viewerjs_tweaks.js" type="text/javascript" charset="utf-8"></script>
</head> </head>
<body> <body>

View File

@ -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; position: absolute;
right: 18px; right: 18px;
top: 0px; top: 0px;
display: none; 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; display: inherit;
} }
@ -18,9 +23,16 @@
-moz-border-radius: 2px; -moz-border-radius: 2px;
-webkit-border-radius: 2px; -webkit-border-radius: 2px;
border-radius: 2px; border-radius: 2px;
} }*/
.openerp .oe-binary-preview .openerp .oe-binary-preview
{ {
cursor: pointer; cursor: pointer;
vertical-align: middle; vertical-align: middle;
} }
.openerp .oe-control-panel .oe-cp-sidebar .oe_sidebar_delete_item {
padding: 0;
position: absolute;
right: 10px;
}

View File

@ -19,13 +19,18 @@
// //
//############################################################################ //############################################################################
openerp.attachment_preview = function(instance) odoo.define('attachment_preview', function (require) {
{ "use strict";
var _t = instance.web._t;
openerp.attachment_preview.show_preview = function( var core = require('web.core');
attachment_id, attachment_url, attachment_extension, var session = require('web.session');
attachment_title) 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 || '') + var url = (window.location.origin || '') +
'/attachment_preview/static/lib/ViewerJS/index.html' + '/attachment_preview/static/lib/ViewerJS/index.html' +
'?type=' + encodeURIComponent(attachment_extension) + '?type=' + encodeURIComponent(attachment_extension) +
@ -34,8 +39,8 @@ openerp.attachment_preview = function(instance)
attachment_url.replace(window.location.origin, '') attachment_url.replace(window.location.origin, '')
window.open(url); window.open(url);
}; };
openerp.attachment_preview.can_preview = function(extension)
{ function can_preview(extension) {
return jQuery.inArray( return jQuery.inArray(
extension, extension,
[ [
@ -43,18 +48,18 @@ openerp.attachment_preview = function(instance)
'fods', 'ots' 'fods', 'ots'
]) > -1; ]) > -1;
}; };
instance.web.Sidebar.include(
{ Sidebar.include({
on_attachments_loaded: function(attachments) on_attachments_loaded: function(attachments) {
{ this._super(attachments);
var result = this._super.apply(this, arguments);
this.$el.find('.oe-sidebar-attachment-preview') this.$('.oe_sidebar_attachment_preview')
.click(this.on_attachment_preview); .click(this.on_attachment_preview);
this.update_preview_buttons(); this.update_preview_buttons();
return result; // return _super.apply(self, arguments);
}, },
on_attachment_preview: function(e)
{ on_attachment_preview: function(e){
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
var self = this, var self = this,
@ -65,28 +70,28 @@ openerp.attachment_preview = function(instance)
attachment_title = $target.attr('data-original-title'); attachment_title = $target.attr('data-original-title');
if(attachment_extension) if(attachment_extension)
{ {
openerp.attachment_preview.show_preview( show_preview(
attachment_id, attachment_url, attachment_extension, attachment_id, attachment_url, attachment_extension,
attachment_title); attachment_title);
} }
else else
{ {
(new instance.web.Model('ir.attachment')).call( (new Model('ir.attachment')).call(
'get_attachment_extension', [attachment_id], {}) 'get_attachment_extension', [attachment_id], {})
.then(function(extension) .then(function(extension)
{ {
openerp.attachment_preview.show_preview( show_preview(
attachment_id, attachment_url, extension); attachment_id, attachment_url, extension[0], attachment_title);
}); });
} }
}, },
update_preview_buttons: function()
{ update_preview_buttons: function(){
var self = this; var self = this;
return (new instance.web.Model('ir.attachment')).call( return (new Model('ir.attachment')).call(
'get_attachment_extension', 'get_attachment_extension',
[ [
this.$el.find('.oe-sidebar-attachment-preview') this.$('span.oe_sidebar_attachment_preview')
.map(function() .map(function()
{ {
return parseInt(jQuery(this).attr('data-id')); return parseInt(jQuery(this).attr('data-id'));
@ -99,11 +104,11 @@ openerp.attachment_preview = function(instance)
_(extensions).each(function(extension, id) _(extensions).each(function(extension, id)
{ {
var $element = jQuery( var $element = jQuery(
'a.oe-sidebar-attachment-preview[data-id="' 'span.oe_sidebar_attachment_preview[data-id="'
+ 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 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), var deferred = this._super.apply(this, arguments),
self = this; self = this;
deferred.then(function() deferred.then(function()
{ {
var $elements = self.$el.find('.oe-binary-preview'); var $elements = self.$('.oe_binary_preview');
if(!$elements.length) if(!$elements.length)
{ {
return; return;
@ -132,13 +137,13 @@ openerp.attachment_preview = function(instance)
var $target = jQuery(e.currentTarget), var $target = jQuery(e.currentTarget),
attachment_id = parseInt($target.attr('data-id')), attachment_id = parseInt($target.attr('data-id')),
attachment_extension = $target.attr('data-extension'); attachment_extension = $target.attr('data-extension');
openerp.attachment_preview.show_preview( show_preview(
attachment_id, attachment_id,
$target.siblings('a').attr('href'), $target.siblings('a').attr('href'),
attachment_extension, attachment_extension,
$target.attr('alt')); $target.attr('alt'));
}); });
return (new instance.web.Model('ir.attachment')).call( return (new Model('ir.attachment')).call(
'get_binary_extension', 'get_binary_extension',
[ [
$elements.attr('data-model'), $elements.attr('data-model'),
@ -158,9 +163,9 @@ openerp.attachment_preview = function(instance)
{ {
var $element = $elements.filter( var $element = $elements.filter(
'[data-id="' + id + '"]'); '[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 else
{ {
@ -172,32 +177,29 @@ openerp.attachment_preview = function(instance)
return deferred; return deferred;
} }
}); });
instance.web.list.Binary.include(
{ var ColumnBinary = core.list_widget_registry.get('field.binary').include({
_format: function (row_data, options) _format: function (row_data, options)
{ {
var link = this._super.apply(this, arguments); var link = this._super.apply(this, arguments);
link += _.template( link += (
'<img class="oe-binary-preview" title="<%-preview_text%>" alt="<%-preview_text%>" data-id="<%-preview_id%>" data-model="<%-preview_model%>" data-field="<%-preview_field%>" data-filename="<%-preview_filename%>" src="/web/static/src/img/icons/gtk-print-preview.png" />', '<a class="oe_binary_preview" title="'+_.str.sprintf(_t('Preview %s'), this.string)+
{ '" alt="'+_.str.sprintf(_t('Preview %s'), this.string)+'"'+
preview_id: options.id, ' data-id="'+options.id+'" data-model="'+options.model+'"'+
preview_text: _.str.sprintf(_t('Preview %s'), this.string), ' data-field="'+this.id+'" data-filename="'+(this.filename || '')+'"'+
preview_model: options.model, ' src="/web/static/src/img/icons/gtk-print-preview.png" ><i class="fa fa-search"/></a>');
preview_field: this.id,
preview_filename: this.filename || '',
});
return link; return link;
} }
}); });
instance.web.form.FieldBinaryFile.include(
{ var FieldBinaryFile = core.form_widget_registry.get('binary').include({
render_value: function() render_value: function()
{ {
this._super.apply(this, arguments); this._super();
if(this.get("effective_readonly") && this.get('value')) if(this.get("effective_readonly") && this.get('value'))
{ {
var self = this; var self = this;
(new instance.web.Model('ir.attachment')).call( (new Model('ir.attachment')).call(
'get_binary_extension', [ 'get_binary_extension', [
this.view.dataset.model, this.view.dataset.model,
this.view.datarecord.id ? [this.view.datarecord.id] : [], this.view.datarecord.id ? [this.view.datarecord.id] : [],
@ -209,22 +211,21 @@ openerp.attachment_preview = function(instance)
{ {
_(extensions).each(function(extension) _(extensions).each(function(extension)
{ {
var $element = self.$el.find('.oe-binary-preview'); var $element = self.$('.oe_binary_preview');
if(openerp.attachment_preview.can_preview(extension)) $element.unbind( "click" );
if(can_preview(extension[0]))
{ {
$element.click(function() $element.click(function()
{ {
openerp.attachment_preview.show_preview( show_preview(
null, null,
_.str.sprintf(
'/web/binary/saveas?session_id=%s&model=%s&field=%s&id=%d', ('data:application/'+extension[0]+';base64,'+
instance.session.session_id, extension[1]),
self.view.dataset.model, extension[0],
self.name,
self.view.datarecord.id),
extension,
_.str.sprintf(_t('Preview %s'), self.field.string)); _.str.sprintf(_t('Preview %s'), self.field.string));
}); });
$element.attr('title', _.str.sprintf(_t('Preview %s'), self.field.string)); $element.attr('title', _.str.sprintf(_t('Preview %s'), self.field.string));
} }
else else
@ -236,8 +237,8 @@ openerp.attachment_preview = function(instance)
} }
else else
{ {
this.$el.find('.oe-binary-preview').remove(); this.$('.oe_binary_preview').remove();
}; };
}, },
}); });
} });

View File

@ -1,15 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<templates> <templates>
<t t-extend="Sidebar"> <t t-extend="Sidebar">
<t t-jquery="a.oe_sidebar_delete_item" t-operation="before"> <t t-jquery="span.oe_sidebar_delete_item" t-operation="before">
<a t-if="section.name == 'files' and !item.callback and item.url" href="#" class="oe-sidebar-attachment-preview" t-att-data-id="item.id" t-att-data-url="item.url" t-attf-title="Preview #{item.name}"> <span t-if="section.name == 'files' and !item.callback and item.url" class="oe_sidebar_attachment_preview" t-att-data-id="item.id" t-att-data-url="item.url" t-attf-title="Preview #{item.name}">
<img t-att-src='_s + "/web/static/src/img/icons/gtk-print-preview.png"' width="12" height="12" border="0"/> <i class="fa fa-search"/>
</a> </span>
</t> </t>
</t> </t>
<t t-extend="FieldBinaryFile"> <t t-extend="FieldBinaryFile">
<t t-jquery="a.oe_form_uri" t-operation="after"> <t t-jquery="a.oe_form_uri" t-operation="after">
<img class="oe-binary-preview" t-att-src='_s + "/web/static/src/img/icons/gtk-print-preview.png"'/> <span class="oe_binary_preview">
<i class="fa fa-search"/>
</span>
</t> </t>
</t> </t>
</templates> </templates>