Add attachment from email and copy attachments to Communication Record (#2412)

* Carry over file attachments in email communications

* [fix] style
This commit is contained in:
Rushabh Mehta 2016-12-05 14:59:00 +05:30 committed by GitHub
parent d8a58de07c
commit c67d3bfe01
4 changed files with 76 additions and 13 deletions

View file

@ -65,7 +65,13 @@ def make(doctype=None, name=None, content=None, subject=None, sent_or_received =
# if no reference given, then send it against the communication
comm.db_set(dict(reference_doctype='Communication', reference_name=comm.name))
if isinstance(attachments, basestring):
attachments = json.loads(attachments)
# if not committed, delayed task doesn't find the communication
if attachments:
add_attachments(comm.name, attachments)
frappe.db.commit()
if cint(send_email):
@ -320,6 +326,20 @@ def get_cc(doc, recipients=None, fetched_from_email_account=False):
return cc
def add_attachments(name, attachments):
'''Add attachments to the given Communiction'''
from frappe.utils.file_manager import save_url
# loop through attachments
for a in attachments:
attach = frappe.db.get_value("File", {"name":a},
["file_name", "file_url", "is_private"], as_dict=1)
# save attachments to new doc
save_url(attach.file_url, attach.file_name, "Communication", name,
"Home/Attachments", attach.is_private)
def filter_email_list(doc, email_list, exclude, is_cc=False):
# temp variables
filtered = []

View file

@ -63,7 +63,7 @@ frappe.ui.form.Attachments = Class.extend({
var me = this;
var $attach = $(repl('<li class="attachment-row">\
<a class="close" data-owner="%(owner)s">&times;</a>\
<a class="close">&times;</a>\
%(lock_icon)s\
<a href="%(file_url)s" target="_blank" title="%(file_name)s" \
class="ellipsis" style="max-width: calc(100% - 43px);">\
@ -228,13 +228,14 @@ frappe.ui.get_upload_dialog = function(opts){
'method': 'frappe.client.get_value',
'args': {
'doctype': 'File',
'fieldname': ['file_url','file_name'],
'fieldname': ['file_url','file_name','is_private'],
'filters': {
'name': dialog.get_value("file")
}
},
callback: function(r){
dialog.$wrapper.find('[name="file_url"]').val(r.message.file_url);
dialog.$wrapper.find('.private-file input').prop('checked', r.message.is_private);
opts.args.filename = r.message.file_name
}
});

View file

@ -31,10 +31,10 @@ frappe.views.CommunicationComposer = Class.extend({
});
// reset attachment list
me.setup_attach();
me.render_attach();
// check latest added
checked_items.push(attachment.file_name);
checked_items.push(attachment.name);
$.each(checked_items, function(i, filename) {
wrapper.find('[data-file-name="'+ filename +'"]').prop("checked", true);
@ -258,15 +258,56 @@ frappe.views.CommunicationComposer = Class.extend({
},
setup_attach: function() {
if (!cur_frm) return;
var fields = this.dialog.fields_dict;
var attach = $(fields.select_attachments.wrapper);
var files = cur_frm.get_files();
var me = this
me.attachments = []
var args = {
args: {
from_form: 1,folder:"Home/Attachments"
},
callback: function(attachment, r) { me.attachments.push(attachment); },
max_width: null,
max_height: null
};
if(me.frm) {
args = {
args: (me.frm.attachments.get_args
? me.frm.attachments.get_args()
: { from_form: 1,folder:"Home/Attachments" }),
callback: function (attachment, r) {
me.frm.attachments.attachment_uploaded(attachment, r)
},
max_width: me.frm.cscript ? me.frm.cscript.attachment_max_width : null,
max_height: me.frm.cscript ? me.frm.cscript.attachment_max_height : null
}
}
$("<h6 class='text-muted add-attachment' style='margin-top: 12px; cursor:pointer;'>"
+__("Select Attachments")+"</h6><div class='attach-list'></div>\
<p class='add-more-attachments'>\
<a class='text-muted small'><i class='octicon octicon-plus' style='font-size: 12px'></i> "
+__("Add Attachment")+"</a></p>").appendTo(attach.empty())
attach.find(".add-more-attachments a").on('click',this,function() {
me.upload = frappe.ui.get_upload_dialog(args);
})
me.render_attach()
},
render_attach:function(){
var fields = this.dialog.fields_dict;
var attach = $(fields.select_attachments.wrapper).find(".attach-list").empty();
if (cur_frm){
var files = cur_frm.get_files();
}else {
var files = this.attachments
}
if(files.length) {
$("<h6 class='text-muted' style='margin-top: 12px;'>"
+__("Add Attachments")+"</h6>").appendTo(attach.empty());
$.each(files, function(i, f) {
if (!f.file_name) return;
f.file_url = frappe.urllib.get_full_url(f.file_url);

View file

@ -35,7 +35,7 @@ def upload():
if frappe.form_dict.filedata:
filedata = save_uploaded(dt, dn, folder, is_private)
elif file_url:
filedata = save_url(file_url, filename, dt, dn, folder)
filedata = save_url(file_url, filename, dt, dn, folder, is_private)
comment = {}
if dt and dn:
@ -60,7 +60,7 @@ def save_uploaded(dt, dn, folder, is_private):
else:
raise Exception
def save_url(file_url, filename, dt, dn, folder):
def save_url(file_url, filename, dt, dn, folder, is_private):
# if not (file_url.startswith("http://") or file_url.startswith("https://")):
# frappe.msgprint("URL must start with 'http://' or 'https://'")
# return None, None
@ -70,10 +70,11 @@ def save_url(file_url, filename, dt, dn, folder):
f = frappe.get_doc({
"doctype": "File",
"file_url": file_url,
"fieldname": filename,
"file_name": filename,
"attached_to_doctype": dt,
"attached_to_name": dn,
"folder": folder
"folder": folder,
"is_private": is_private
})
f.flags.ignore_permissions = True
try: