New File Uploading
This commit is contained in:
parent
8cbd03392b
commit
c1bc00d8bf
5 changed files with 72 additions and 34 deletions
|
|
@ -1,3 +1,4 @@
|
|||
frappe.provide("frappe.ui");
|
||||
|
||||
frappe.listview_settings['File'] = {
|
||||
hide_name_column: true,
|
||||
|
|
@ -26,8 +27,10 @@ frappe.listview_settings['File'] = {
|
|||
},
|
||||
onload: function(doclist) {
|
||||
doclist.filter_area = doclist.wrapper.find(".show_filters");
|
||||
|
||||
doclist.breadcrumb = $('<ol class="breadcrumb for-file-list"></ol>')
|
||||
.insertBefore(doclist.filter_area);
|
||||
|
||||
doclist.page.add_menu_item(__("Create Folder"), function() {
|
||||
var d = frappe.prompt(__("Name"), function(values) {
|
||||
if((values.value.indexOf("/") > -1)){
|
||||
|
|
@ -84,6 +87,18 @@ frappe.listview_settings['File'] = {
|
|||
|
||||
frappe.utils.set_title(doclist.current_folder_name);
|
||||
},
|
||||
set_primary_action:function(doclist){
|
||||
|
||||
doclist.page.clear_primary_action();
|
||||
doclist.page.set_primary_action(__("New File"), function() {
|
||||
|
||||
|
||||
dialog = frappe.ui.get_upload_dialog({
|
||||
"data":{"folder": doclist.current_folder, "from_form": 1}
|
||||
})
|
||||
|
||||
}, "octicon octicon-plus");
|
||||
},
|
||||
post_render_item: function(list, row, data) {
|
||||
if(data.is_folder) {
|
||||
$(row).find(".list-id").attr("href", "#List/File/" + data.name);
|
||||
|
|
|
|||
|
|
@ -145,28 +145,12 @@ frappe.ui.form.Attachments = Class.extend({
|
|||
},
|
||||
new_attachment: function(fieldname) {
|
||||
var me = this;
|
||||
if(!this.dialog) {
|
||||
this.dialog = new frappe.ui.Dialog({
|
||||
title: __('Upload Attachment'),
|
||||
});
|
||||
if(!this.dialog){
|
||||
this.dialog = frappe.ui.get_upload_dialog({"data": me.get_args(),
|
||||
"callback": me.attachment_uploaded, "curr": me,
|
||||
"max_width": me.frm.cscript ? me.frm.cscript.attachment_max_width : null,
|
||||
"max_height": me.frm.cscript ? me.frm.cscript.attachment_max_height : null});
|
||||
}
|
||||
this.dialog.show();
|
||||
this.fieldname = fieldname;
|
||||
|
||||
$(this.dialog.body).empty();
|
||||
frappe.upload.make({
|
||||
parent: this.dialog.body,
|
||||
args: this.get_args(),
|
||||
callback: function(attachment, r) {
|
||||
me.attachment_uploaded(attachment, r);
|
||||
},
|
||||
onerror: function() {
|
||||
me.dialog.hide();
|
||||
},
|
||||
btn: this.dialog.set_primary_action(__("Attach")),
|
||||
max_width: this.frm.cscript ? this.frm.cscript.attachment_max_width : null,
|
||||
max_height: this.frm.cscript ? this.frm.cscript.attachment_max_height : null,
|
||||
});
|
||||
},
|
||||
get_args: function() {
|
||||
return {
|
||||
|
|
@ -213,3 +197,34 @@ frappe.ui.form.Attachments = Class.extend({
|
|||
this.refresh();
|
||||
}
|
||||
});
|
||||
|
||||
frappe.ui.get_upload_dialog = function(opts){
|
||||
dialog = new frappe.ui.Dialog({
|
||||
title: __('Upload Attachment'),
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
|
||||
$(dialog.body).empty();
|
||||
|
||||
frappe.upload.make({
|
||||
parent: dialog.body,
|
||||
args: opts.data,
|
||||
callback: function(attachment, r) {
|
||||
if(opts.callback){
|
||||
opts.callback.call(opts.curr, attachment, r);
|
||||
}
|
||||
else{
|
||||
dialog.hide();
|
||||
}
|
||||
},
|
||||
onerror: function() {
|
||||
dialog.hide();
|
||||
},
|
||||
btn: dialog.set_primary_action(__("Attach")),
|
||||
max_width: opts.max_width,
|
||||
max_height: opts.max_height,
|
||||
});
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,8 +118,12 @@ frappe.ui.Listing = Class.extend({
|
|||
set_primary_action: function() {
|
||||
var me = this;
|
||||
if(this.new_doctype) {
|
||||
this.page.set_primary_action(__("New"), function() {
|
||||
me.make_new_doc(); }, "octicon octicon-plus");
|
||||
if(this.listview.settings.set_primary_action){
|
||||
this.listview.settings.set_primary_action(this);
|
||||
} else {
|
||||
this.page.set_primary_action(__("New"), function() {
|
||||
me.make_new_doc(me.new_doctype); }, "octicon octicon-plus");
|
||||
}
|
||||
} else {
|
||||
this.page.clear_primary_action();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ frappe.upload = {
|
|||
}
|
||||
return;
|
||||
}
|
||||
console.log(args)
|
||||
|
||||
var dataurl = null;
|
||||
var _upload_file = function() {
|
||||
|
|
|
|||
|
|
@ -21,20 +21,21 @@ def upload():
|
|||
# get record details
|
||||
dt = frappe.form_dict.doctype
|
||||
dn = frappe.form_dict.docname
|
||||
folder = frappe.form_dict.folder
|
||||
file_url = frappe.form_dict.file_url
|
||||
filename = frappe.form_dict.filename
|
||||
|
||||
if not filename and not file_url:
|
||||
frappe.msgprint(_("Please select a file or url"),
|
||||
raise_exception=True)
|
||||
|
||||
print folder
|
||||
# save
|
||||
if filename:
|
||||
filedata = save_uploaded(dt, dn)
|
||||
filedata = save_uploaded(dt, dn, folder)
|
||||
elif file_url:
|
||||
filedata = save_url(file_url, dt, dn)
|
||||
|
||||
filedata = save_url(file_url, dt, dn, folder)
|
||||
|
||||
comment = {}
|
||||
if dt and dn:
|
||||
comment = frappe.get_doc(dt, dn).add_comment("Attachment",
|
||||
_("Added {0}").format("<a href='{file_url}' target='_blank'>{file_name}</a>".format(**filedata.as_dict())))
|
||||
|
|
@ -43,17 +44,17 @@ def upload():
|
|||
"name": filedata.name,
|
||||
"file_name": filedata.file_name,
|
||||
"file_url": filedata.file_url,
|
||||
"comment": comment.as_dict()
|
||||
"comment": comment.as_dict() if comment else {}
|
||||
}
|
||||
|
||||
def save_uploaded(dt, dn):
|
||||
def save_uploaded(dt, dn, folder):
|
||||
fname, content = get_uploaded_content()
|
||||
if content:
|
||||
return save_file(fname, content, dt, dn);
|
||||
return save_file(fname, content, dt, dn, folder);
|
||||
else:
|
||||
raise Exception
|
||||
|
||||
def save_url(file_url, dt, dn):
|
||||
def save_url(file_url, dt, dn, folder):
|
||||
# if not (file_url.startswith("http://") or file_url.startswith("https://")):
|
||||
# frappe.msgprint("URL must start with 'http://' or 'https://'")
|
||||
# return None, None
|
||||
|
|
@ -62,7 +63,8 @@ def save_url(file_url, dt, dn):
|
|||
"doctype": "File",
|
||||
"file_url": file_url,
|
||||
"attached_to_doctype": dt,
|
||||
"attached_to_name": dn
|
||||
"attached_to_name": dn,
|
||||
"folder": folder
|
||||
})
|
||||
f.flags.ignore_permissions = True
|
||||
try:
|
||||
|
|
@ -128,7 +130,8 @@ def get_random_filename(extn=None, content_type=None):
|
|||
|
||||
return random_string(7) + (extn or "")
|
||||
|
||||
def save_file(fname, content, dt, dn, decode=False):
|
||||
def save_file(fname, content, dt, dn, folder=None,decode=False):
|
||||
print [folder]
|
||||
if decode:
|
||||
if isinstance(content, unicode):
|
||||
content = content.encode("utf-8")
|
||||
|
|
@ -151,6 +154,7 @@ def save_file(fname, content, dt, dn, decode=False):
|
|||
"doctype": "File",
|
||||
"attached_to_doctype": dt,
|
||||
"attached_to_name": dn,
|
||||
"folder": folder,
|
||||
"file_size": file_size,
|
||||
"content_hash": content_hash,
|
||||
})
|
||||
|
|
@ -290,4 +294,3 @@ def get_file_name(fname, optional_suffix):
|
|||
partial, extn = f[0], "." + f[1]
|
||||
return '{partial}{suffix}{extn}'.format(partial=partial, extn=extn, suffix=optional_suffix)
|
||||
return fname
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue