[IMP] mail: attachment

bzr revid: chm@openerp.com-20121009132824-dsl7f02hxfpypc0w
This commit is contained in:
Christophe Matthieu 2012-10-09 15:28:24 +02:00
parent 52f8ea85c8
commit 2e4a6953c4
4 changed files with 144 additions and 31 deletions

View File

@ -349,10 +349,10 @@ class mail_message(osv.Model):
attachment = self.pool.get('ir.attachment') attachment = self.pool.get('ir.attachment')
attachment_ids = attachment.search(cr, uid, [('res_model','=',''),('create_uid','=',uid)]) attachment_ids = attachment.search(cr, uid, [('res_model','=',''),('create_uid','=',uid)])
if len(attachment_ids): if len(attachment_ids):
attachment_list = [{'id': attach[0], 'name': attach[1]} for attach in attachment.name_get(cr, uid, attachment_ids, context=context)] attachment_list = [{'id': attach.id, 'name': attach.name, 'date': attach.create_date} for attach in attachment.browse(cr, uid, attachment_ids, context=context)]
return attachment_list return attachment_list
#------------------------------------------------------ #------------------------------------------------------
# Email api # Email api
#------------------------------------------------------ #------------------------------------------------------

View File

@ -391,7 +391,25 @@
} }
.openerp .oe_mail_msg_content ul.oe_mail_msg_attachments li { .openerp .oe_mail_msg_content ul.oe_mail_msg_attachments li {
float: none; float: none;
height: 20px;
line-height: 20px;
margin: 0; margin: 0;
padding: 0; padding: 0;
list-style-type: square; list-style-type: square;
} }
.openerp .oe_mail_msg_content ul.oe_mail_msg_attachments .oe_upload_in_process {
float: right;
width: 200px;
height: 16px;
}
.openerp .oe_mail_msg_content ul.oe_mail_msg_attachments .oe_upload_in_process div {
float: left;
width: 38px;
height: 16px;
margin-right: 2px;
background: #66FF66;
}
.openerp .oe_mail_msg_content ul.oe_mail_msg_attachments .oe_upload_in_process span {
color: #aaaaaa;
position: absolute;
}

View File

@ -114,6 +114,8 @@ openerp.mail = function(session) {
this.parent_thread= parent.messages!= undefined ? parent : false; this.parent_thread= parent.messages!= undefined ? parent : false;
this.ds_attachment = new session.web.DataSetSearch(this, 'ir.attachment');
this.fileupload_id = _.uniqueId('oe_fileupload_temp'); this.fileupload_id = _.uniqueId('oe_fileupload_temp');
$(window).on(self.fileupload_id, self.on_attachment_loaded); $(window).on(self.fileupload_id, self.on_attachment_loaded);
}, },
@ -148,12 +150,16 @@ openerp.mail = function(session) {
var $target = $(event.target); var $target = $(event.target);
if ($target.val() !== '') { if ($target.val() !== '') {
var filename = $target.val().replace(/.*[\\\/]/,'');
// if the files exits for this answer, delete the file before upload // if the files exits for this answer, delete the file before upload
var attachments=[]; var attachments=[];
for(var i in this.attachment_ids){ for(var i in this.attachment_ids){
if(this.attachment_ids[i].url == $target.val()){ if((this.attachment_ids[i].filename || this.attachment_ids[i].name) == filename){
var ds_attachment = new session.web.DataSetSearch(this, 'ir.attachment'); if(this.attachment_ids[i].upload){
ds_attachment.unlink([this.attachment_ids.id]); return false;
}
this.ds_attachment.unlink([this.attachment_ids[i].id]);
} else { } else {
attachments.push(this.attachment_ids[i]); attachments.push(this.attachment_ids[i]);
} }
@ -161,22 +167,87 @@ openerp.mail = function(session) {
this.attachment_ids = attachments; this.attachment_ids = attachments;
// submit file // submit file
session.web.blockUI(); //session.web.blockUI();
var form = self.$('form.oe_form_binary_form'); self.$('form.oe_form_binary_form').submit();
form.submit(); //self.submit_ajax_attachment();
$target.after($target.clone(true)).remove();
this.$(".oe_attachment_file").hide();
this.attachment_ids.push({
'id': 0,
'name': filename,
'filename': filename,
'url': '',
'upload': true
});
this.display_attachments();
} }
}, },
on_attachment_loaded: function (event, result) {
session.web.unblockUI(); submit_ajax_attachment: function(){
this.attachment_ids.push({ var self=this;
'id': result.id, var $form = self.$('form.oe_form_binary_form');
'name': '', var filename = this.$('input.oe_form_binary_file').val().replace(/.*[\\\/]/,'');
'filename': result.filename,
'url': mail.ChatterUtils.get_attachment_url(this.session, result) // create form data
var fomdata = new FormData();
$.each($form.find('input'), function(i, field) {
var $field=$(field);
if($field.attr('type')!='file'){
fomdata.append($field.attr('name'), $field.val());
} else {
fomdata.append($field.attr('name'), field.files[0]);
}
}); });
this.$('input.oe_form_binary_file').empty();
var progress=function(event) {
console.log('xhr progress :', Math.floor(event.loaded / event.total*100), event);
self.$("span[name='"+filename+"'] div:lt("+Math.floor(event.loaded / event.total*5)+")").show();
};
$.ajax({
url: $form.attr("action"),
data: fomdata,
cache: false,
contentType: false,
processData: false,
type: 'POST',
enctype: 'multipart/form-data',
xhr: function() {
// custom xhr
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
// for handling the progress of the upload
myXhr.upload.addEventListener('progress', progress, false);
}
myXhr.addEventListener('progress', progress, false);
return myXhr;
},
success: function(data){
$iframe=$('<iframe style="display:none;"/>').html(data);
$iframe.appendTo(self.$el);
$iframe.remove();
}
});
},
on_attachment_loaded: function (event, result) {
//session.web.unblockUI();
for(var i in this.attachment_ids){
if(this.attachment_ids[i].filename == result.filename && this.attachment_ids[i].upload){
this.attachment_ids[i]={
'id': result.id,
'name': result.name,
'filename': result.filename,
'url': mail.ChatterUtils.get_attachment_url(this.session, result)
};
}
}
this.display_attachments(); this.display_attachments();
var $input = this.$('input.oe_form_binary_file');
$input.after($input.clone(true)).remove();
this.$(".oe_attachment_file").show();
}, },
/* unlink the file on the server and reload display /* unlink the file on the server and reload display
*/ */
@ -190,8 +261,7 @@ openerp.mail = function(session) {
attachments.push(this.attachment_ids[i]); attachments.push(this.attachment_ids[i]);
} }
else { else {
var ds_attachment = new session.web.DataSetSearch(this, 'ir.attachment'); this.ds_attachment.unlink([attachment_id]);
ds_attachment.unlink([attachment_id]);
} }
} }
this.attachment_ids = attachments; this.attachment_ids = attachments;
@ -199,10 +269,21 @@ openerp.mail = function(session) {
} }
}, },
/* to avoid having unsorted file on the server.
we will show the users files of the first message post
*/
set_free_attachments: function(){ set_free_attachments: function(){
var self=this; var self=this;
this.parent_thread.ds_message.call('user_free_attachment').then(function(attachments){ this.parent_thread.ds_message.call('user_free_attachment').then(function(attachments){
self.attachment_ids=attachments; this.attachment_ids=[];
for(var i in attachments){
self.attachment_ids[i]={
'id': attachments[i].id,
'name': attachments[i].name,
'filename': attachments[i].filename,
'url': mail.ChatterUtils.get_attachment_url(self.session, attachments[i])
};
}
self.display_attachments(); self.display_attachments();
}); });
}, },
@ -246,7 +327,7 @@ openerp.mail = function(session) {
event.stopPropagation(); event.stopPropagation();
this.$('textarea').val(""); this.$('textarea').val("");
this.$('input[data-id]').remove(); this.$('input[data-id]').remove();
this.attachment_ids=[]; //this.attachment_ids=[];
this.display_attachments(); this.display_attachments();
if(!this.options.thread.show_header_compose || !this.options.thread.display_on_flat){ if(!this.options.thread.show_header_compose || !this.options.thread.display_on_flat){
this.$el.hide(); this.$el.hide();
@ -261,20 +342,22 @@ openerp.mail = function(session) {
var body = comment_node.val(); var body = comment_node.val();
comment_node.val(''); comment_node.val('');
} }
if(body.match(/\S+/)) {
var attachments=[]; var attachments=[];
for(var i in this.attachment_ids){ for(var i in this.attachment_ids){
attachments.push(this.attachment_ids[i].id); if(this.attachment_ids[i].upload){
session.web.dialog($('<div>' + session.web.qweb.render('CrashManager.warning', {message: 'Please, wait while the file is uploading.'}) + '</div>'));
return false;
} }
attachments.push(this.attachment_ids[i].id);
}
if(body.match(/\S+/)) {
this.parent_thread.ds_thread.call('message_post_api', [ this.parent_thread.ds_thread.call('message_post_api', [
this.context.default_res_id, body, false, 'comment', false, this.context.default_parent_id, attachments]) this.context.default_res_id, body, false, 'comment', false, this.context.default_parent_id, attachments])
.then(this.parent_thread.proxy('switch_new_message')); .then(this.parent_thread.proxy('switch_new_message'));
this.on_cancel(); this.on_cancel();
} return true;
else {
return false;
} }
}, },
}); });

View File

@ -58,8 +58,21 @@
<ul t-attf-class="oe_mail_msg_attachments #{widget.attachment_ids[0] and widget.options.thread.show_attachment_link?'':'oe_hidden'}"> <ul t-attf-class="oe_mail_msg_attachments #{widget.attachment_ids[0] and widget.options.thread.show_attachment_link?'':'oe_hidden'}">
<t t-foreach="widget.attachment_ids" t-as="attachment"> <t t-foreach="widget.attachment_ids" t-as="attachment">
<li> <li>
<a t-att-href="attachment.url"><t t-raw="attachment.name || attachment.filename"/></a> <span t-if="(attachment.upload or attachment.percent_loaded&lt;100)" t-attf-title="{(attachment.name || attachment.filename) + (attachment.date?' \n('+attachment.date+')':'' )}" t-attf-name="{attachment.name || attachment.filename}">
<t t-if="widget.options.thread.show_attachment_delete"> <div class="oe_upload_in_process">
<span>...wait upload...</span>
<div t-attf-style="{attachment.percent_loaded&gt;0?'':'display:none;'}"/>
<div t-attf-style="{attachment.percent_loaded&gt;20?'':'display:none;'}"/>
<div t-attf-style="{attachment.percent_loaded&gt;40?'':'display:none;'}"/>
<div t-attf-style="{attachment.percent_loaded&gt;60?'':'display:none;'}"/>
<div t-attf-style="{attachment.percent_loaded&gt;80?'':'display:none;'}"/>
</div>
<t t-raw="attachment.name || attachment.filename"/>
</span>
<a t-if="(!attachment.upload or attachment.percent_loaded&gt;=100)" t-att-href="attachment.url" t-attf-title="{(attachment.name || attachment.filename) + (attachment.date?' \n('+attachment.date+')':'' )}">
<t t-raw="attachment.name || attachment.filename"/>
</a>
<t t-if="widget.options.thread.show_attachment_delete and (!attachment.upload or attachment.percent_loaded&gt;=100)">
<a class="oe_right oe_mail_attachment_delete" title="Delete this attachmentt" t-attf-data-id="{attachment.id}">x</a> <a class="oe_right oe_mail_attachment_delete" title="Delete this attachmentt" t-attf-data-id="{attachment.id}">x</a>
</t> </t>
@ -68,7 +81,6 @@
</ul> </ul>
</t> </t>
<t t-name="mail.thread.message.private"> <t t-name="mail.thread.message.private">
<div> <div>
<span class="oe_placeholder_checkbox_private"/> <span class="oe_placeholder_checkbox_private"/>