From c1bc00d8bfa0230a5ec8916690a80b343c8be01b Mon Sep 17 00:00:00 2001 From: saurabh Date: Fri, 4 Sep 2015 14:13:52 +0530 Subject: [PATCH] New File Uploading --- frappe/core/doctype/file/file_list.js | 15 +++++ .../js/frappe/form/footer/attachments.js | 57 ++++++++++++------- frappe/public/js/frappe/ui/listing.js | 8 ++- frappe/public/js/frappe/upload.js | 1 + frappe/utils/file_manager.py | 25 ++++---- 5 files changed, 72 insertions(+), 34 deletions(-) diff --git a/frappe/core/doctype/file/file_list.js b/frappe/core/doctype/file/file_list.js index 98ca0a6abe..d53f0b59b5 100644 --- a/frappe/core/doctype/file/file_list.js +++ b/frappe/core/doctype/file/file_list.js @@ -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 = $('') .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); diff --git a/frappe/public/js/frappe/form/footer/attachments.js b/frappe/public/js/frappe/form/footer/attachments.js index 5672588dae..de8c3e3356 100644 --- a/frappe/public/js/frappe/form/footer/attachments.js +++ b/frappe/public/js/frappe/form/footer/attachments.js @@ -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; +} diff --git a/frappe/public/js/frappe/ui/listing.js b/frappe/public/js/frappe/ui/listing.js index 2dac9d834e..40a72c35c3 100644 --- a/frappe/public/js/frappe/ui/listing.js +++ b/frappe/public/js/frappe/ui/listing.js @@ -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(); } diff --git a/frappe/public/js/frappe/upload.js b/frappe/public/js/frappe/upload.js index 97d115deff..50ec5b15be 100644 --- a/frappe/public/js/frappe/upload.js +++ b/frappe/public/js/frappe/upload.js @@ -71,6 +71,7 @@ frappe.upload = { } return; } + console.log(args) var dataurl = null; var _upload_file = function() { diff --git a/frappe/utils/file_manager.py b/frappe/utils/file_manager.py index 57ea19e5d2..31d68b6195 100644 --- a/frappe/utils/file_manager.py +++ b/frappe/utils/file_manager.py @@ -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("{file_name}".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 -