[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``::
sudo apt-get install python-magic
or
pip install file-magic
Usage
=====
@ -47,6 +49,8 @@ Contributors
------------
* Holger Brunn <hbrunn@therp.nl>
* Carlos Quirós <carlos.quiros@clearcorp.co.cr>
* Glen Sojo <glen.sojo@clearcorp.co.cr>
Maintainer
----------

View File

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

View File

@ -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):

View File

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

View File

@ -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(
'<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" />',
{
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 += (
'<a class="oe_binary_preview" title="'+_.str.sprintf(_t('Preview %s'), this.string)+
'" alt="'+_.str.sprintf(_t('Preview %s'), this.string)+'"'+
' data-id="'+options.id+'" data-model="'+options.model+'"'+
' data-field="'+this.id+'" data-filename="'+(this.filename || '')+'"'+
' src="/web/static/src/img/icons/gtk-print-preview.png" ><i class="fa fa-search"/></a>');
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();
};
},
});
}
});

View File

@ -1,15 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<t t-extend="Sidebar">
<t t-jquery="a.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}">
<img t-att-src='_s + "/web/static/src/img/icons/gtk-print-preview.png"' width="12" height="12" border="0"/>
</a>
<t t-jquery="span.oe_sidebar_delete_item" t-operation="before">
<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}">
<i class="fa fa-search"/>
</span>
</t>
</t>
<t t-extend="FieldBinaryFile">
<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>
</templates>