fixed attachments - now not reqd to save after attaching
This commit is contained in:
parent
bcf6471612
commit
a1c43e51bb
5 changed files with 102 additions and 30 deletions
|
|
@ -97,7 +97,7 @@ class DocList:
|
|||
"""
|
||||
from webnotes.model.meta import is_single
|
||||
|
||||
if (not is_single(self.doc.doctype)) and (not self.doc.fields.get('__islocal')):
|
||||
if (not is_single(self.doc.doctype)) and (not cint(self.doc.fields.get('__islocal'))):
|
||||
tmp = webnotes.conn.sql("""
|
||||
SELECT modified FROM `tab%s` WHERE name="%s" for update"""
|
||||
% (self.doc.doctype, self.doc.name))
|
||||
|
|
|
|||
|
|
@ -7,20 +7,86 @@ def upload():
|
|||
dn = form.getvalue('docname')
|
||||
at_id = form.getvalue('at_id')
|
||||
|
||||
webnotes.response['type'] = 'iframe'
|
||||
if not webnotes.form['filedata'].filename:
|
||||
webnotes.response['result'] = """
|
||||
<script type='text/javascript'>
|
||||
window.parent.frms['%s'].attachments.dialog.hide();
|
||||
window.parent.msgprint("Please select a file!");
|
||||
</script>""" % dt
|
||||
return
|
||||
|
||||
# save
|
||||
fid, fname = save_uploaded()
|
||||
|
||||
if fid:
|
||||
# save it in the form
|
||||
updated = add_file_list(dt, dn, fname, fid)
|
||||
|
||||
if fid and updated:
|
||||
# refesh the form!
|
||||
# with the new modified timestamp
|
||||
webnotes.response['result'] = """
|
||||
<script type='text/javascript'>
|
||||
window.parent.wn.widgets.form.file_upload_done('%s', '%s', '%s', '%s', '%s');
|
||||
window.parent.frms['%s'].show_doc('%s');
|
||||
window.parent.wn.widgets.form.file_upload_done('%(dt)s', '%(dn)s', '%(fid)s', '%(fname)s', '%(at_id)s', '%(mod)s');
|
||||
window.parent.frms['%(dt)s'].show_doc('%(dn)s');
|
||||
</script>
|
||||
""" % (dt, dn, fid, fname.replace("'", "\\'"), at_id, dt, dn)
|
||||
""" % {
|
||||
'dt': dt,
|
||||
'dn': dn,
|
||||
'fid': fid,
|
||||
'fname': fname.replace("'", "\\'"),
|
||||
'at_id': at_id,
|
||||
'mod': webnotes.conn.get_value(dt, dn, 'modified')
|
||||
}
|
||||
|
||||
# -------------------------------------------------------
|
||||
|
||||
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.sql("select file_list from `tab%s` where name=%s" % (dt, '%s'), dn)[0][0] or ''
|
||||
if fl:
|
||||
fl += '\n'
|
||||
|
||||
# add new file id
|
||||
fl += fname + ',' + fid
|
||||
|
||||
# save
|
||||
webnotes.conn.set_value(dt, dn, 'file_list', fl)
|
||||
|
||||
return True
|
||||
|
||||
except Exception, e:
|
||||
webnotes.response['result'] = """
|
||||
<script type='text/javascript'>
|
||||
window.parent.msgprint("Error while uploading: %s");
|
||||
</script>""" % str(e)
|
||||
|
||||
|
||||
def remove_file_list(dt, dn, fid):
|
||||
"""
|
||||
Remove fid from the give file_list
|
||||
"""
|
||||
import webnotes
|
||||
|
||||
# get the old file_list
|
||||
fl = webnotes.conn.sql("select file_list from `tab%s` where name=%s" % (dt, '%s'), dn)[0][0] or ''
|
||||
new_fl = []
|
||||
fl = fl.split('\n')
|
||||
for f in fl:
|
||||
if f.split(',')[1]!=fid:
|
||||
new_fl.append(f)
|
||||
|
||||
# update the file_list
|
||||
webnotes.conn.set_value(dt, dn, 'file_list', '\n'.join(new_fl))
|
||||
|
||||
# return the new timestamp
|
||||
return webnotes.conn.get_value(dt, dn, 'modified')
|
||||
|
||||
def make_thumbnail(blob, size):
|
||||
from PIL import Image
|
||||
import cStringIO
|
||||
|
|
|
|||
|
|
@ -197,6 +197,7 @@ def runserverobj():
|
|||
doclist = DocList()
|
||||
doclist.from_compressed(form.getvalue('docs'), dn)
|
||||
so = doclist.make_obj()
|
||||
doclist.check_if_latest()
|
||||
|
||||
check_guest_access(so.doc)
|
||||
|
||||
|
|
@ -289,6 +290,9 @@ def remove_attach():
|
|||
fid = webnotes.form.getvalue('fid')
|
||||
webnotes.utils.file_manager.delete_file(fid, verbose=1)
|
||||
|
||||
# remove from dt dn
|
||||
return str(webnotes.utils.file_manager.remove_file_list(webnotes.form.getvalue('dt'), webnotes.form.getvalue('dn'), fid))
|
||||
|
||||
# Get Fields - Counterpart to $c_get_fields
|
||||
#===========================================================================================
|
||||
def get_fields():
|
||||
|
|
|
|||
|
|
@ -474,7 +474,9 @@ for(var i=0;i<cl.length;i++){this.render_one_comment(cl[i]);}}else{this.msg.inne
|
|||
this.render_one_comment=function(det){$a(this.wrapper,'div','social sidebar-comment-text','',det.comment);$a(this.wrapper,'div','sidebar-comment-info','',comment_when(det.creation)+' by '+det.comment_by_fullname);}
|
||||
this.add_comment=function(){if(!this.input.value)return;this.btn.set_working();wn.widgets.form.comments.add(this.input,me.doctype,me.docname,function(){me.btn.done_working();me.make_body();});}
|
||||
this.refresh();}
|
||||
wn.widgets.form.sidebar.Attachments=function(parent,sidebar,doctype,docname){var me=this;this.frm=sidebar.form;this.make=function(){if(this.wrapper)this.wrapper.innerHTML='';else this.wrapper=$a(parent,'div','sidebar-comment-wrapper');this.attach_wrapper=$a(this.wrapper,'div');var n=this.frm.doc.file_list?this.frm.doc.file_list.split('\n').length:0;if(n<this.frm.meta.max_attachments||!this.frm.meta.max_attachments){this.btn=$btn($a(this.wrapper,'div','sidebar-comment-message'),'Add',function(){me.add_attachment()});}
|
||||
wn.widgets.form.sidebar.Attachments=function(parent,sidebar,doctype,docname){var me=this;this.frm=sidebar.form;this.make=function(){if(this.wrapper)this.wrapper.innerHTML='';else this.wrapper=$a(parent,'div','sidebar-comment-wrapper');this.attach_wrapper=$a(this.wrapper,'div');if(this.frm.doc.__islocal){this.attach_wrapper.innerHTML='Attachments can be uploaded after saving'
|
||||
return;}
|
||||
var n=this.frm.doc.file_list?this.frm.doc.file_list.split('\n').length:0;if(n<this.frm.meta.max_attachments||!this.frm.meta.max_attachments){this.btn=$btn($a(this.wrapper,'div','sidebar-comment-message'),'Add',function(){me.add_attachment()});}
|
||||
this.render();}
|
||||
this.render=function(){this.attach_wrapper.innerHTML=''
|
||||
var doc=locals[me.frm.doctype][me.frm.docname];var fl=doc.file_list?doc.file_list.split('\n'):[];for(var i=0;i<fl.length;i++){new wn.widgets.form.sidebar.Attachment(this.attach_wrapper,fl[i],me.frm)}}
|
||||
|
|
@ -486,12 +488,11 @@ this.make();}
|
|||
wn.widgets.form.sidebar.Attachment=function(parent,filedet,frm){filedet=filedet.split(',')
|
||||
this.filename=filedet[0];this.fileid=filedet[1];this.frm=frm;var me=this;this.wrapper=$a(parent,'div','sidebar-comment-message');this.remove_fileid=function(){var doc=locals[me.frm.doctype][me.frm.docname];var fl=doc.file_list.split('\n');new_fl=[];for(var i=0;i<fl.length;i++){if(fl[i].split(',')[1]!=me.fileid)new_fl.push(fl[i]);}
|
||||
doc.file_list=new_fl.join('\n');}
|
||||
this.ln=$a(this.wrapper,'a','link_type',{fontSize:'11px'},this.filename);this.ln.href=outUrl+'?cmd=get_file&fname='+this.fileid;this.ln.target='_blank';this.del=$a(this.wrapper,'span','link_type',{marginLeft:'3px'},'[x]');this.del.onclick=function(){var yn=confirm("The document will be saved after the attachment is deleted for the changes to be permanent. Proceed?")
|
||||
if(yn){var callback=function(r,rt){$dh(me.wrapper);me.remove_fileid();var ret=me.frm.save('Save');if(ret=='Error')
|
||||
msgprint("error:The document was not saved. To make the removal permanent, you must save the document before closing.");}
|
||||
$c('webnotes.widgets.form.remove_attach',args={'fid':me.fileid},callback);}}}
|
||||
wn.widgets.form.file_upload_done=function(doctype,docname,fileid,filename,at_id){var at_id=cint(at_id);var doc=locals[doctype][docname];if(doc.file_list){var fl=doc.file_list.split('\n')
|
||||
this.ln=$a(this.wrapper,'a','link_type',{fontSize:'11px'},this.filename);this.ln.href=outUrl+'?cmd=get_file&fname='+this.fileid;this.ln.target='_blank';this.del=$a(this.wrapper,'span','link_type',{marginLeft:'3px'},'[x]');this.del.onclick=function(){var yn=confirm("Are you sure you want to delete the attachment?")
|
||||
if(yn){var callback=function(r,rt){locals[me.frm.doctype][me.frm.docname].modified=r.message;$dh(me.wrapper);me.remove_fileid();frm.refresh();}
|
||||
$c('webnotes.widgets.form.remove_attach',args={'fid':me.fileid,dt:me.frm.doctype,dn:me.frm.docname},callback);}}}
|
||||
wn.widgets.form.file_upload_done=function(doctype,docname,fileid,filename,at_id,new_timestamp){var at_id=cint(at_id);var doc=locals[doctype][docname];if(doc.file_list){var fl=doc.file_list.split('\n')
|
||||
fl.push(filename+','+fileid)
|
||||
doc.file_list=fl.join('\n');}
|
||||
else
|
||||
doc.file_list=filename+','+fileid;var frm=frms[doctype];frm.attachments.dialog.hide();frm.attachments.render();var do_save=confirm('File Uploaded Sucessfully. You must save this document for the uploaded file to be registred. Save this document now?');if(do_save){var ret=frm.save('Save');if(ret=='Error')msgprint("error:The document was not saved. To make the attachment permanent, you must save the document before closing.");}else{msgprint("error:The document was not saved. To make the attachment permanent, you must save the document before closing.");}}
|
||||
doc.file_list=filename+','+fileid;doc.modified=new_timestamp;var frm=frms[doctype];frm.attachments.dialog.hide();msgprint('File Uploaded Sucessfully.');frm.refresh();}
|
||||
|
|
@ -9,6 +9,12 @@ wn.widgets.form.sidebar.Attachments = function(parent, sidebar, doctype, docname
|
|||
// attachment
|
||||
this.attach_wrapper = $a(this.wrapper, 'div');
|
||||
|
||||
// no attachments if file is unsaved
|
||||
if(this.frm.doc.__islocal) {
|
||||
this.attach_wrapper.innerHTML = 'Attachments can be uploaded after saving'
|
||||
return;
|
||||
}
|
||||
|
||||
// no of attachments
|
||||
var n = this.frm.doc.file_list ? this.frm.doc.file_list.split('\n').length : 0;
|
||||
|
||||
|
|
@ -90,25 +96,24 @@ wn.widgets.form.sidebar.Attachment = function(parent, filedet, frm) {
|
|||
// remove
|
||||
this.del = $a(this.wrapper, 'span', 'link_type', {marginLeft:'3px'}, '[x]');
|
||||
this.del.onclick = function() {
|
||||
var yn = confirm("The document will be saved after the attachment is deleted for the changes to be permanent. Proceed?")
|
||||
var yn = confirm("Are you sure you want to delete the attachment?")
|
||||
if(yn) {
|
||||
var callback = function(r, rt) {
|
||||
// update timestamp
|
||||
locals[me.frm.doctype][me.frm.docname].modified = r.message;
|
||||
$dh(me.wrapper);
|
||||
me.remove_fileid();
|
||||
var ret=me.frm.save('Save');
|
||||
if(ret=='Error')
|
||||
msgprint("error:The document was not saved. To make the removal permanent, you must save the document before closing.");
|
||||
frm.refresh();
|
||||
}
|
||||
|
||||
$c('webnotes.widgets.form.remove_attach', args = {'fid': me.fileid }, callback );
|
||||
|
||||
$c('webnotes.widgets.form.remove_attach',
|
||||
args = {'fid': me.fileid, dt: me.frm.doctype, dn: me.frm.docname }, callback );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// this function will be called after the upload is done
|
||||
// from webnotes.utils.file_manager
|
||||
wn.widgets.form.file_upload_done = function(doctype, docname, fileid, filename, at_id) {
|
||||
wn.widgets.form.file_upload_done = function(doctype, docname, fileid, filename, at_id, new_timestamp) {
|
||||
|
||||
var at_id = cint(at_id);
|
||||
|
||||
|
|
@ -122,16 +127,12 @@ wn.widgets.form.file_upload_done = function(doctype, docname, fileid, filename,
|
|||
else
|
||||
doc.file_list = filename + ',' + fileid;
|
||||
|
||||
// update timestamp
|
||||
doc.modified = new_timestamp;
|
||||
|
||||
// update file_list
|
||||
var frm = frms[doctype];
|
||||
frm.attachments.dialog.hide();
|
||||
frm.attachments.render();
|
||||
|
||||
var do_save = confirm('File Uploaded Sucessfully. You must save this document for the uploaded file to be registred. Save this document now?');
|
||||
if(do_save) {
|
||||
var ret = frm.save('Save');
|
||||
if(ret=='Error')msgprint("error:The document was not saved. To make the attachment permanent, you must save the document before closing.");
|
||||
} else {
|
||||
msgprint("error:The document was not saved. To make the attachment permanent, you must save the document before closing.");
|
||||
}
|
||||
msgprint('File Uploaded Sucessfully.');
|
||||
frm.refresh();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue