From 8694cd339fda95d292ed9be7e676b72651d7a1aa Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 21 Dec 2011 14:59:11 +0530 Subject: [PATCH] Changes in file_manager to fix attach picture issue in Profile Settings --- js/legacy/webpage/uploader.js | 4 ++-- js/legacy/wnf.compressed.js | 3 ++- py/webnotes/handler.py | 17 ++++++++++++----- py/webnotes/utils/file_manager.py | 23 +++++++++++++---------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/js/legacy/webpage/uploader.js b/js/legacy/webpage/uploader.js index 446a7f4b2f..3af0c487bb 100644 --- a/js/legacy/webpage/uploader.js +++ b/js/legacy/webpage/uploader.js @@ -23,7 +23,7 @@ Uploader = function(parent, args, callback) { // file data var inp_fdata = $a_input($a(ul_form,'span'),'file',{name:'filedata'},{marginLeft:'7px'}); - var inp = $a_input($a(ul_form,'span'),'hidden',{name:'cmd'}); inp.value = 'uploadfile'; + if(!('cmd' in args)) { var inp = $a_input($a(ul_form,'span'),'hidden',{name:'cmd'}); inp.value = 'uploadfile'; } var inp = $a_input($a(ul_form,'span'),'hidden',{name:'uploader_id'}); inp.value = id; var inp = $a_input($a(ul_form,'span'),'submit',null,{marginLeft:'7px'}); inp.value = 'Upload'; @@ -39,4 +39,4 @@ Uploader = function(parent, args, callback) { function upload_callback(id, fid) { uploaders[id].callback(fid); -} \ No newline at end of file +} diff --git a/js/legacy/wnf.compressed.js b/js/legacy/wnf.compressed.js index 88365f6951..96dcd9eabd 100644 --- a/js/legacy/wnf.compressed.js +++ b/js/legacy/wnf.compressed.js @@ -738,7 +738,8 @@ function loadscript(src,call_back){set_loading();var script=$a('head','script'); script.onreadystatechange=function(){if(this.readyState=='complete'||this.readyState=='loaded'){hide_loading();call_back();}}} var doc_browser_page;function loaddocbrowser(dt,label,fields){wn.require('lib/js/legacy/widgets/form/fields.js');wn.require('lib/js/legacy/webpage/docbrowser.js');dt=get_label_doctype(dt);if(!doc_browser_page) doc_browser_page=new ItemBrowserPage();doc_browser_page.show(dt,label,fields);nav_obj.open_notify('List',dt,'');} -var uploaders={};var upload_frame_count=0;Uploader=function(parent,args,callback){var id='frame'+upload_frame_count;upload_frame_count++;this.callback=callback;var div=$a(parent,'div');div.innerHTML='';var div=$a(parent,'div');div.innerHTML='
';var ul_form=div.childNodes[0];var f_list=[];var inp_fdata=$a_input($a(ul_form,'span'),'file',{name:'filedata'},{marginLeft:'7px'});var inp=$a_input($a(ul_form,'span'),'hidden',{name:'cmd'});inp.value='uploadfile';var inp=$a_input($a(ul_form,'span'),'hidden',{name:'uploader_id'});inp.value=id;var inp=$a_input($a(ul_form,'span'),'submit',null,{marginLeft:'7px'});inp.value='Upload';$y(inp,{width:'80px'});for(var key in args){var inp=$a_input($a(ul_form,'span'),'hidden',{name:key});inp.value=args[key];} +var uploaders={};var upload_frame_count=0;Uploader=function(parent,args,callback){var id='frame'+upload_frame_count;upload_frame_count++;this.callback=callback;var div=$a(parent,'div');div.innerHTML='';var div=$a(parent,'div');div.innerHTML='
';var ul_form=div.childNodes[0];var f_list=[];var inp_fdata=$a_input($a(ul_form,'span'),'file',{name:'filedata'},{marginLeft:'7px'});if(!('cmd'in args)){var inp=$a_input($a(ul_form,'span'),'hidden',{name:'cmd'});inp.value='uploadfile';} +var inp=$a_input($a(ul_form,'span'),'hidden',{name:'uploader_id'});inp.value=id;var inp=$a_input($a(ul_form,'span'),'submit',null,{marginLeft:'7px'});inp.value='Upload';$y(inp,{width:'80px'});for(var key in args){var inp=$a_input($a(ul_form,'span'),'hidden',{name:key});inp.value=args[key];} uploaders[id]=this;} function upload_callback(id,fid){uploaders[id].callback(fid);} var pages=[];var stylesheets=[];function Page(page_name,content){var me=this;this.name=page_name;this.onshow=function(){set_title(me.doc.page_title?me.doc.page_title:me.name);try{if(pscript['onshow_'+me.name])pscript['onshow_'+me.name]();}catch(e){submit_error(e);} diff --git a/py/webnotes/handler.py b/py/webnotes/handler.py index f4a9228fd9..ef4532f3af 100755 --- a/py/webnotes/handler.py +++ b/py/webnotes/handler.py @@ -116,12 +116,19 @@ def uploadfile(): fid, fname = webnotes.utils.file_manager.save_uploaded() # do something with the uploaded file - if fid and webnotes.form_dict.get('server_obj'): - from webnotes.model.code import get_obj - getattr(get_obj(webnotes.form_dict.get('server_obj')), webnotes.form_dict.get('method'))(fid, fname) - - # return the upload if fid: + if webnotes.form_dict.get('server_obj'): + from webnotes.model.code import get_obj + getattr(get_obj(webnotes.form_dict.get('server_obj')), webnotes.form_dict.get('method'))(fid, fname) + + elif webnotes.form_dict.get('modulename'): + # calls a python module to handle the script + __import__(webnotes.form_dict['modulename']) + import sys + moduleobj = sys.modules[webnotes.form_dict['modulename']] + getattr(moduleobj, webnotes.form_dict['method'])(fid, fname) + + webnotes.response['result'] = '' # File upload (from scripts) diff --git a/py/webnotes/utils/file_manager.py b/py/webnotes/utils/file_manager.py index 2b824c1641..460005d7e5 100644 --- a/py/webnotes/utils/file_manager.py +++ b/py/webnotes/utils/file_manager.py @@ -1,5 +1,6 @@ +import webnotes + def upload(): - import webnotes form = webnotes.form # get record details @@ -47,7 +48,6 @@ def add_file_list(dt, dn, fname, fid): """ udpate file_list attribute of the record """ - import webnotes try: # get the old file_list fl = webnotes.conn.get_value(dt, dn, 'file_list') or '' @@ -69,11 +69,19 @@ window.parent.msgprint("Error while uploading: %s"); """ % str(e) +def remove_all(dt, dn): + """remove all files in a transaction""" + file_list = webnotes.conn.get_value(dt, dn, 'file_list') or '' + for afile in file_list.split('\n'): + if afile: + fname, fid = afile.split(',') + remove_file_list(dt, dn, fid) + delete_file(fid) + def remove_file_list(dt, dn, fid): """ Remove fid from the give file_list """ - import webnotes # get the old file_list fl = webnotes.conn.get_value(dt, dn, 'file_list') or '' @@ -105,7 +113,6 @@ def make_thumbnail(blob, size): def save_uploaded(js_okay='window.parent.msgprint("File Upload Successful")', js_fail=''): - import webnotes import webnotes.utils webnotes.response['type'] = 'iframe' @@ -144,7 +151,6 @@ def save_uploaded(js_okay='window.parent.msgprint("File Upload Successful")', js # ------------------------------------------------------- def save_file(fname, content, module=None): - import webnotes from webnotes.model.doc import Document # some browsers return the full path @@ -167,7 +173,7 @@ def save_file(fname, content, module=None): # ------------------------------------------------------- def write_file(fid, content): - import webnotes, os, webnotes.defs + import os, webnotes.defs # test size max_file_size = 1000000 @@ -195,12 +201,11 @@ def write_file(fid, content): # ------------------------------------------------------- def get_file_system_name(fname): # get system name from File Data table - import webnotes return webnotes.conn.sql("select name, file_name from `tabFile Data` where name=%s or file_name=%s", (fname, fname)) # ------------------------------------------------------- def delete_file(fname, verbose=0): - import webnotes, os + import os for f in get_file_system_name(fname): webnotes.conn.sql("delete from `tabFile Data` where name=%s", f[0]) @@ -219,7 +224,6 @@ def delete_file(fname, verbose=0): # ------------------------------------------------------- def get_file(fname): - import webnotes in_fname = fname # from the "File" table @@ -248,7 +252,6 @@ def get_file(fname): # ------------------------------------------------------- def convert_to_files(verbose=0): - import webnotes # nfiles fl = webnotes.conn.sql("select name from `tabFile Data`")